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; public class DeletePurchaseOrderItemHandler : IRequestHandler { private readonly AppDbContext _context; public DeletePurchaseOrderItemHandler(AppDbContext context) => _context = context; public async Task 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; } }