Skip to content

Commit b526e73

Browse files
committed
added payment commands
1 parent 02a46c7 commit b526e73

File tree

13 files changed

+177
-7
lines changed

13 files changed

+177
-7
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
using Logistics.Shared.Enums;
2+
using MediatR;
3+
4+
namespace Logistics.Application.Tenant.Commands;
5+
6+
public class CreatePaymentCommand : IRequest<ResponseResult>
7+
{
8+
public PaymentMethod Method { get; set; }
9+
public decimal Amount { get; set; }
10+
public PaymentFor PaymentFor { get; set; }
11+
public string? Comment { get; set; }
12+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
namespace Logistics.Application.Tenant.Commands;
2+
3+
internal sealed class CreatePaymentHandler : RequestHandler<CreatePaymentCommand, ResponseResult>
4+
{
5+
private readonly ITenantRepository _tenantRepository;
6+
7+
public CreatePaymentHandler(ITenantRepository tenantRepository)
8+
{
9+
_tenantRepository = tenantRepository;
10+
}
11+
12+
protected override async Task<ResponseResult> HandleValidated(
13+
CreatePaymentCommand req, CancellationToken cancellationToken)
14+
{
15+
var payment = new Payment
16+
{
17+
Amount = req.Amount,
18+
Method = req.Method,
19+
PaymentFor = req.PaymentFor,
20+
Comment = req.Comment
21+
};
22+
23+
await _tenantRepository.AddAsync(payment);
24+
await _tenantRepository.UnitOfWork.CommitAsync();
25+
return ResponseResult.CreateSuccess();
26+
}
27+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
using FluentValidation;
2+
3+
namespace Logistics.Application.Tenant.Commands;
4+
5+
internal sealed class CreatePaymentValidator : AbstractValidator<CreatePaymentCommand>
6+
{
7+
public CreatePaymentValidator()
8+
{
9+
RuleFor(i => i.Amount).GreaterThan(0M);
10+
}
11+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
using MediatR;
2+
3+
namespace Logistics.Application.Tenant.Commands;
4+
5+
public class DeletePaymentCommand : IRequest<ResponseResult>
6+
{
7+
public string Id { get; set; } = default!;
8+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
namespace Logistics.Application.Tenant.Commands;
2+
3+
internal sealed class DeletePaymentHandler : RequestHandler<DeletePaymentCommand, ResponseResult>
4+
{
5+
private readonly ITenantRepository _tenantRepository;
6+
7+
public DeletePaymentHandler(ITenantRepository tenantRepository)
8+
{
9+
_tenantRepository = tenantRepository;
10+
}
11+
12+
protected override async Task<ResponseResult> HandleValidated(
13+
DeletePaymentCommand req, CancellationToken cancellationToken)
14+
{
15+
var payment = await _tenantRepository.GetAsync<Payment>(req.Id);
16+
17+
if (payment is null)
18+
return ResponseResult.CreateError($"Could not find a payment with ID {req.Id}");
19+
20+
_tenantRepository.Delete(payment);
21+
await _tenantRepository.UnitOfWork.CommitAsync();
22+
return ResponseResult.CreateSuccess();
23+
}
24+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
using FluentValidation;
2+
3+
namespace Logistics.Application.Tenant.Commands;
4+
5+
internal sealed class DeletePaymentValidator : AbstractValidator<DeletePaymentCommand>
6+
{
7+
public DeletePaymentValidator()
8+
{
9+
RuleFor(i => i.Id).NotEmpty();
10+
}
11+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
using Logistics.Shared.Enums;
2+
using MediatR;
3+
4+
namespace Logistics.Application.Tenant.Commands;
5+
6+
public class UpdatePaymentCommand : IRequest<ResponseResult>
7+
{
8+
public string Id { get; set; } = default!;
9+
public PaymentMethod? Method { get; set; }
10+
public decimal? Amount { get; set; }
11+
public PaymentStatus? Status { get; set; }
12+
public PaymentFor? PaymentFor { get; set; }
13+
public string? Comment { get; set; }
14+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
namespace Logistics.Application.Tenant.Commands;
2+
3+
internal sealed class UpdatePaymentHandler : RequestHandler<UpdatePaymentCommand, ResponseResult>
4+
{
5+
private readonly ITenantRepository _tenantRepository;
6+
7+
public UpdatePaymentHandler(ITenantRepository tenantRepository)
8+
{
9+
_tenantRepository = tenantRepository;
10+
}
11+
12+
protected override async Task<ResponseResult> HandleValidated(
13+
UpdatePaymentCommand req, CancellationToken cancellationToken)
14+
{
15+
var payment = await _tenantRepository.GetAsync<Payment>(req.Id);
16+
17+
if (payment is null)
18+
return ResponseResult.CreateError($"Could not find a payment with ID '{req.Id}'");
19+
20+
if (req.PaymentFor.HasValue && payment.PaymentFor != req.PaymentFor)
21+
{
22+
payment.PaymentFor = req.PaymentFor.Value;
23+
}
24+
if (req.Method.HasValue && payment.Method != req.Method)
25+
{
26+
payment.Method = req.Method.Value;
27+
}
28+
if (req.Status.HasValue && payment.Status != req.Status)
29+
{
30+
payment.SetStatus(req.Status.Value);
31+
}
32+
if (req.Amount.HasValue && payment.Amount != req.Amount)
33+
{
34+
payment.Amount = req.Amount.Value;
35+
}
36+
if (!string.IsNullOrEmpty(req.Comment) && payment.Comment != req.Comment)
37+
{
38+
payment.Comment = req.Comment;
39+
}
40+
41+
_tenantRepository.Update(payment);
42+
await _tenantRepository.UnitOfWork.CommitAsync();
43+
return ResponseResult.CreateSuccess();
44+
}
45+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
using FluentValidation;
2+
3+
namespace Logistics.Application.Tenant.Commands;
4+
5+
internal sealed class UpdatePaymentValidator : AbstractValidator<UpdatePaymentCommand>
6+
{
7+
public UpdatePaymentValidator()
8+
{
9+
RuleFor(i => i.Id).NotEmpty();
10+
}
11+
}

src/Core/Logistics.Domain/Entities/Payment.cs

+10
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,14 @@ public class Payment : Entity, ITenantEntity
1212
public PaymentStatus Status { get; set; }
1313
public PaymentFor PaymentFor { get; set; }
1414
public string? Comment { get; set; }
15+
16+
public void SetStatus(PaymentStatus status)
17+
{
18+
if (status == PaymentStatus.Paid)
19+
{
20+
PaymentDate = DateTime.UtcNow;
21+
}
22+
23+
Status = status;
24+
}
1525
}

src/Server/Logistics.API/Controllers/PaymentsController.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ public async Task<IActionResult> GetList([FromQuery] GetPaymentsQuery query)
4545
[ProducesResponseType(typeof(ResponseResult), StatusCodes.Status200OK)]
4646
[ProducesResponseType(typeof(ResponseResult), StatusCodes.Status400BadRequest)]
4747
[Authorize(Policy = Permissions.Payments.Create)]
48-
public async Task<IActionResult> Create([FromBody] CreateCustomerCommand request)
48+
public async Task<IActionResult> Create([FromBody] CreatePaymentCommand request)
4949
{
5050
var result = await _mediator.Send(request);
5151

@@ -59,7 +59,7 @@ public async Task<IActionResult> Create([FromBody] CreateCustomerCommand request
5959
[ProducesResponseType(typeof(ResponseResult), StatusCodes.Status200OK)]
6060
[ProducesResponseType(typeof(ResponseResult), StatusCodes.Status400BadRequest)]
6161
[Authorize(Policy = Permissions.Payments.Edit)]
62-
public async Task<IActionResult> Update(string id, [FromBody] UpdateCustomerCommand request)
62+
public async Task<IActionResult> Update(string id, [FromBody] UpdatePaymentCommand request)
6363
{
6464
request.Id = id;
6565
var result = await _mediator.Send(request);

src/Server/Logistics.API/Extensions/ApplicationExtensions.cs

-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
using Logistics.API.Hubs;
44
using Logistics.API.Middlewares;
55
using Logistics.API.Services;
6-
using Logistics.Application.Core;
76
using Logistics.Application.Tenant.Services;
87
using Logistics.Infrastructure.EF;
98
using Microsoft.AspNetCore.Mvc.Authorization;

src/Server/Logistics.API/Middlewares/ExceptionHandlingMiddleware.cs

+2-4
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
1-
using System;
2-
using System.Text;
1+
using System.Text;
32
using System.Text.Json;
4-
using Azure;
53
using FluentValidation;
64

75
namespace Logistics.API.Middlewares;
@@ -81,4 +79,4 @@ public static IApplicationBuilder UseCustomExceptionHandler(this IApplicationBui
8179
{
8280
return builder.UseMiddleware<ExceptionHandlingMiddleware>();
8381
}
84-
}
82+
}

0 commit comments

Comments
 (0)