Api/Netina.Repository/Handlers/Accounting/CreateOrUpdatePaymentComman...

60 lines
2.7 KiB
C#
Raw Normal View History

2024-08-09 21:55:16 +03:30
using Netina.Domain.Entities.Accounting;
2024-04-16 20:01:34 +03:30
namespace Netina.Repository.Handlers.Accounting;
2024-08-09 21:55:16 +03:30
public class CreateOrUpdatePaymentCommandHandler(IRepositoryWrapper repositoryWrapper)
: IRequestHandler<CreateOrUpdatePaymentCommand, bool>
{
public async Task<bool> Handle(CreateOrUpdatePaymentCommand request, CancellationToken cancellationToken)
{
if (request.Id != null)
{
2024-08-09 21:55:16 +03:30
var ent = await repositoryWrapper.SetRepository<Payment>()
.TableNoTracking
.FirstOrDefaultAsync(p => p.Id == request.Id, cancellationToken);
if (ent == null)
throw new AppException("Payment not found", ApiResultStatusCode.NotFound);
var newEnt = Payment.Create(request.FactorNumber, request.Amount, request.Description, request.TransactionCode,
request.CardPan, request.Authority, request.Type, request.Status, request.OrderId, request.UserId);
newEnt.Id = ent.Id;
newEnt.CreatedAt = ent.CreatedAt == DateTime.MinValue ? DateTime.Now : ent.CreatedAt;
newEnt.CreatedBy = ent.CreatedBy;
2024-08-09 21:55:16 +03:30
repositoryWrapper.SetRepository<Payment>()
.Update(newEnt);
2024-08-09 21:55:16 +03:30
await repositoryWrapper.SaveChangesAsync(cancellationToken);
}
else
{
2024-08-09 21:55:16 +03:30
var orderPayment = await repositoryWrapper.SetRepository<Payment>()
.TableNoTracking
.FirstOrDefaultAsync(p => p.OrderId == request.OrderId && p.Type == request.Type,cancellationToken);
if (orderPayment != null)
{
var newEnt = Payment.Create(request.FactorNumber, request.Amount, request.Description, request.TransactionCode,
request.CardPan, request.Authority, request.Type, request.Status, request.OrderId, request.UserId);
newEnt.Id = orderPayment.Id;
newEnt.CreatedAt = orderPayment.CreatedAt == DateTime.MinValue ? DateTime.Now : orderPayment.CreatedAt;
2024-04-02 11:40:45 +03:30
newEnt.CreatedBy = orderPayment.CreatedBy;
2024-08-09 21:55:16 +03:30
repositoryWrapper.SetRepository<Payment>()
.Update(newEnt);
2024-08-09 21:55:16 +03:30
await repositoryWrapper.SaveChangesAsync(cancellationToken);
}
else
{
var payment = Payment.Create(request.FactorNumber, request.Amount, request.Description, request.TransactionCode,
request.CardPan, request.Authority, request.Type, request.Status, request.OrderId, request.UserId);
2024-08-09 21:55:16 +03:30
repositoryWrapper.SetRepository<Payment>()
.Add(payment);
2024-08-09 21:55:16 +03:30
await repositoryWrapper.SaveChangesAsync(cancellationToken);
}
}
return true;
}
}