52 lines
1.6 KiB
C#
52 lines
1.6 KiB
C#
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<UpdatePayrollProcessResponse>;
|
|
|
|
public class UpdatePayrollProcessHandler : IRequestHandler<UpdatePayrollProcessCommand, UpdatePayrollProcessResponse>
|
|
{
|
|
private readonly AppDbContext _context;
|
|
|
|
public UpdatePayrollProcessHandler(AppDbContext context) => _context = context;
|
|
|
|
public async Task<UpdatePayrollProcessResponse> 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
|
|
};
|
|
}
|
|
} |