46 lines
1.6 KiB
C#
46 lines
1.6 KiB
C#
using Indotalent.Infrastructure.Database;
|
|
using MediatR;
|
|
using Microsoft.EntityFrameworkCore;
|
|
using Indotalent.Shared.Utils;
|
|
|
|
namespace Indotalent.Features.Purchase.PurchaseOrder.Cqrs;
|
|
|
|
public class UpdatePurchaseOrderItemRequest
|
|
{
|
|
public string? Id { get; set; }
|
|
public string? ProductId { get; set; }
|
|
public string? Summary { get; set; }
|
|
public decimal? UnitPrice { get; set; }
|
|
public double? Quantity { get; set; }
|
|
}
|
|
|
|
public record UpdatePurchaseOrderItemCommand(UpdatePurchaseOrderItemRequest Data) : IRequest<bool>;
|
|
|
|
public class UpdatePurchaseOrderItemHandler : IRequestHandler<UpdatePurchaseOrderItemCommand, bool>
|
|
{
|
|
private readonly AppDbContext _context;
|
|
public UpdatePurchaseOrderItemHandler(AppDbContext context) => _context = context;
|
|
|
|
public async Task<bool> Handle(UpdatePurchaseOrderItemCommand request, CancellationToken cancellationToken)
|
|
{
|
|
var entity = await _context.PurchaseOrderItem
|
|
.FirstOrDefaultAsync(x => x.Id == request.Data.Id, cancellationToken);
|
|
|
|
if (entity == null) return false;
|
|
|
|
entity.ProductId = request.Data.ProductId;
|
|
entity.Summary = request.Data.Summary;
|
|
entity.UnitPrice = request.Data.UnitPrice ?? 0;
|
|
entity.Quantity = request.Data.Quantity ?? 0;
|
|
entity.Total = (request.Data.UnitPrice ?? 0) * (decimal)(request.Data.Quantity ?? 0);
|
|
|
|
await _context.SaveChangesAsync(cancellationToken);
|
|
|
|
if (!string.IsNullOrEmpty(entity.PurchaseOrderId))
|
|
{
|
|
PurchaseOrderHelper.Recalculate(_context, entity.PurchaseOrderId);
|
|
}
|
|
|
|
return true;
|
|
}
|
|
} |