using Indotalent.Infrastructure.Database; using MediatR; using Microsoft.EntityFrameworkCore; namespace Indotalent.Features.Purchase.GoodsReceive.Cqrs; public record DeleteGoodsReceiveByIdCommand(string Id) : IRequest; public class DeleteGoodsReceiveByIdHandler : IRequestHandler { private readonly AppDbContext _context; public DeleteGoodsReceiveByIdHandler(AppDbContext context) => _context = context; public async Task Handle(DeleteGoodsReceiveByIdCommand request, CancellationToken cancellationToken) { var entity = await _context.GoodsReceive .FirstOrDefaultAsync(x => x.Id == request.Id, cancellationToken); if (entity == null) return false; var transactions = await _context.InventoryTransaction .Where(x => x.ModuleId == entity.Id && x.ModuleName == nameof(Data.Entities.GoodsReceive)) .ToListAsync(cancellationToken); _context.InventoryTransaction.RemoveRange(transactions); _context.GoodsReceive.Remove(entity); await _context.SaveChangesAsync(cancellationToken); return true; } }