72 lines
2.3 KiB
C#
72 lines
2.3 KiB
C#
using Indotalent.ConfigBackEnd.Exceptions;
|
|
using Indotalent.Infrastructure.Database;
|
|
using MediatR;
|
|
using Microsoft.EntityFrameworkCore;
|
|
|
|
namespace Indotalent.Features.Payroll.Grade.Cqrs;
|
|
|
|
public class UpdateGradeRequest
|
|
{
|
|
public string? Id { get; set; }
|
|
public string? Code { get; set; }
|
|
public string? Name { get; set; }
|
|
public string? Description { get; set; }
|
|
public decimal SalaryFrom { get; set; }
|
|
public decimal SalaryTo { get; set; }
|
|
public bool IsOverTimeEligible { get; set; }
|
|
public string? Status { get; set; }
|
|
public DateTimeOffset? CreatedAt { get; set; }
|
|
public string? CreatedBy { get; set; }
|
|
public DateTimeOffset? UpdatedAt { get; set; }
|
|
public string? UpdatedBy { get; set; }
|
|
}
|
|
|
|
public class UpdateGradeResponse
|
|
{
|
|
public string? Id { get; set; }
|
|
public bool Success { get; set; }
|
|
}
|
|
|
|
public record UpdateGradeCommand(UpdateGradeRequest Data) : IRequest<UpdateGradeResponse>;
|
|
|
|
public class UpdateGradeHandler : IRequestHandler<UpdateGradeCommand, UpdateGradeResponse>
|
|
{
|
|
private readonly AppDbContext _context;
|
|
|
|
public UpdateGradeHandler(AppDbContext context) => _context = context;
|
|
|
|
public async Task<UpdateGradeResponse> Handle(UpdateGradeCommand request, CancellationToken cancellationToken)
|
|
{
|
|
var isExists = await _context.Grade
|
|
.AnyAsync(x => x.Code == request.Data.Code && x.Id != request.Data.Id, cancellationToken);
|
|
|
|
if (isExists)
|
|
{
|
|
throw new AlreadyExistsException("Salary Grade", request.Data.Code ?? string.Empty);
|
|
}
|
|
|
|
var entity = await _context.Grade
|
|
.FirstOrDefaultAsync(x => x.Id == request.Data.Id, cancellationToken);
|
|
|
|
if (entity == null)
|
|
{
|
|
return new UpdateGradeResponse { Id = request.Data.Id, Success = false };
|
|
}
|
|
|
|
entity.Code = request.Data.Code;
|
|
entity.Name = request.Data.Name;
|
|
entity.Description = request.Data.Description;
|
|
entity.SalaryFrom = request.Data.SalaryFrom;
|
|
entity.SalaryTo = request.Data.SalaryTo;
|
|
entity.IsOverTimeEligible = request.Data.IsOverTimeEligible;
|
|
entity.Status = request.Data.Status;
|
|
|
|
await _context.SaveChangesAsync(cancellationToken);
|
|
|
|
return new UpdateGradeResponse
|
|
{
|
|
Id = entity.Id,
|
|
Success = true
|
|
};
|
|
}
|
|
} |