using Indotalent.ConfigBackEnd.Exceptions; using Indotalent.Infrastructure.Database; using MediatR; using Microsoft.EntityFrameworkCore; namespace Indotalent.Features.Payroll.Payrolls.Cqrs; public class DeletePayrollProcessByIdRequest { public string? Id { get; set; } } public class DeletePayrollProcessByIdResponse { public string? Id { get; set; } } public record DeletePayrollProcessByIdCommand(DeletePayrollProcessByIdRequest Data) : IRequest; public class DeletePayrollProcessByIdHandler : IRequestHandler { private readonly AppDbContext _context; public DeletePayrollProcessByIdHandler(AppDbContext context) => _context = context; public async Task Handle(DeletePayrollProcessByIdCommand request, CancellationToken cancellationToken) { var entity = await _context.PayrollProcess .FirstOrDefaultAsync(x => x.Id == request.Data.Id, cancellationToken); if (entity == null) { throw new NotFoundException("Payroll Process", request.Data.Id ?? string.Empty); } _context.PayrollProcess.Remove(entity); await _context.SaveChangesAsync(cancellationToken); return new DeletePayrollProcessByIdResponse { Id = entity.Id }; } }