using Indotalent.ConfigBackEnd.Exceptions; using Indotalent.Infrastructure.Database; using MediatR; using Microsoft.EntityFrameworkCore; namespace Indotalent.Features.Payroll.Payrolls.Cqrs; public class UpdatePayrollProcessRequest { public string? Id { get; set; } public string? Status { get; set; } public string? Description { get; set; } public string? PeriodName { get; set; } } public class UpdatePayrollProcessResponse { public string? Id { get; set; } public string? Status { get; set; } } public record UpdatePayrollProcessCommand(UpdatePayrollProcessRequest Data) : IRequest; public class UpdatePayrollProcessHandler : IRequestHandler { private readonly AppDbContext _context; public UpdatePayrollProcessHandler(AppDbContext context) => _context = context; public async Task Handle(UpdatePayrollProcessCommand 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); } entity.Status = request.Data.Status; entity.Description = request.Data.Description; entity.PeriodName = request.Data.PeriodName; await _context.SaveChangesAsync(cancellationToken); return new UpdatePayrollProcessResponse { Id = entity.Id, Status = entity.Status }; } }