Files
blazor-saas-crm/Features/Purchase/PurchaseOrder/Cqrs/DeletePurchaseOrderItemHandler.cs
T
2026-07-21 14:14:44 +07:00

34 lines
1.1 KiB
C#

using Indotalent.Infrastructure.Database;
using MediatR;
using Microsoft.EntityFrameworkCore;
using Indotalent.Shared.Utils;
namespace Indotalent.Features.Purchase.PurchaseOrder.Cqrs;
public record DeletePurchaseOrderItemCommand(string Id) : IRequest<bool>;
public class DeletePurchaseOrderItemHandler : IRequestHandler<DeletePurchaseOrderItemCommand, bool>
{
private readonly AppDbContext _context;
public DeletePurchaseOrderItemHandler(AppDbContext context) => _context = context;
public async Task<bool> Handle(DeletePurchaseOrderItemCommand request, CancellationToken cancellationToken)
{
var entity = await _context.PurchaseOrderItem
.FirstOrDefaultAsync(x => x.Id == request.Id, cancellationToken);
if (entity == null) return false;
var orderId = entity.PurchaseOrderId;
_context.PurchaseOrderItem.Remove(entity);
await _context.SaveChangesAsync(cancellationToken);
if (!string.IsNullOrEmpty(orderId))
{
PurchaseOrderHelper.Recalculate(_context, orderId);
}
return true;
}
}