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