46 lines
1.7 KiB
C#
46 lines
1.7 KiB
C#
using Indotalent.Infrastructure.Database;
|
|
using MediatR;
|
|
using Microsoft.EntityFrameworkCore;
|
|
using Indotalent.Shared.Utils;
|
|
|
|
namespace Indotalent.Features.Purchase.PurchaseRequisition.Cqrs;
|
|
|
|
public class UpdatePurchaseRequisitionItemRequest
|
|
{
|
|
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 UpdatePurchaseRequisitionItemCommand(UpdatePurchaseRequisitionItemRequest Data) : IRequest<bool>;
|
|
|
|
public class UpdatePurchaseRequisitionItemHandler : IRequestHandler<UpdatePurchaseRequisitionItemCommand, bool>
|
|
{
|
|
private readonly AppDbContext _context;
|
|
public UpdatePurchaseRequisitionItemHandler(AppDbContext context) => _context = context;
|
|
|
|
public async Task<bool> Handle(UpdatePurchaseRequisitionItemCommand request, CancellationToken cancellationToken)
|
|
{
|
|
var entity = await _context.PurchaseRequisitionItem
|
|
.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.PurchaseRequisitionId))
|
|
{
|
|
PurchaseRequisitionHelper.Recalculate(_context, entity.PurchaseRequisitionId);
|
|
}
|
|
|
|
return true;
|
|
}
|
|
} |