initial commit
This commit is contained in:
@@ -0,0 +1,46 @@
|
||||
using Indotalent.Infrastructure.Database;
|
||||
using MediatR;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Indotalent.Shared.Utils;
|
||||
|
||||
namespace Indotalent.Features.Sales.SalesOrder.Cqrs;
|
||||
|
||||
public class UpdateSalesOrderItemRequest
|
||||
{
|
||||
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 UpdateSalesOrderItemCommand(UpdateSalesOrderItemRequest Data) : IRequest<bool>;
|
||||
|
||||
public class UpdateSalesOrderItemHandler : IRequestHandler<UpdateSalesOrderItemCommand, bool>
|
||||
{
|
||||
private readonly AppDbContext _context;
|
||||
public UpdateSalesOrderItemHandler(AppDbContext context) => _context = context;
|
||||
|
||||
public async Task<bool> Handle(UpdateSalesOrderItemCommand request, CancellationToken cancellationToken)
|
||||
{
|
||||
var entity = await _context.SalesOrderItem
|
||||
.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.SalesOrderId))
|
||||
{
|
||||
SalesOrderHelper.Recalculate(_context, entity.SalesOrderId);
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user