initial commit
This commit is contained in:
@@ -0,0 +1,73 @@
|
||||
using Indotalent.ConfigBackEnd.Exceptions;
|
||||
using Indotalent.ConfigBackEnd.Extensions;
|
||||
using Indotalent.Infrastructure.Database;
|
||||
using MediatR;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace Indotalent.Features.Payroll.Grade.Cqrs;
|
||||
|
||||
public class CreateGradeRequest
|
||||
{
|
||||
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; } = "Active";
|
||||
}
|
||||
|
||||
public class CreateGradeResponse
|
||||
{
|
||||
public string? Id { get; set; }
|
||||
public string? Code { get; set; }
|
||||
}
|
||||
|
||||
public record CreateGradeCommand(CreateGradeRequest Data) : IRequest<CreateGradeResponse>;
|
||||
|
||||
public class CreateGradeHandler : IRequestHandler<CreateGradeCommand, CreateGradeResponse>
|
||||
{
|
||||
private readonly AppDbContext _context;
|
||||
|
||||
public CreateGradeHandler(AppDbContext context) => _context = context;
|
||||
|
||||
public async Task<CreateGradeResponse> Handle(CreateGradeCommand request, CancellationToken cancellationToken)
|
||||
{
|
||||
var isExists = await _context.Grade
|
||||
.AnyAsync(x => x.Code == request.Data.Code, cancellationToken);
|
||||
|
||||
if (isExists)
|
||||
{
|
||||
throw new AlreadyExistsException("Salary Grade", request.Data.Code ?? string.Empty);
|
||||
}
|
||||
|
||||
var entityName = nameof(Data.Entities.Grade);
|
||||
|
||||
var autoNo = await _context.GenerateAutoNumberAsync(
|
||||
entityName: entityName,
|
||||
prefixTemplate: $"{entityName.ToShortNameConsonant(3)}/{{Year}}/",
|
||||
ct: cancellationToken
|
||||
);
|
||||
|
||||
var entity = new Data.Entities.Grade
|
||||
{
|
||||
AutoNumber = autoNo,
|
||||
Code = request.Data.Code,
|
||||
Name = request.Data.Name,
|
||||
Description = request.Data.Description,
|
||||
SalaryFrom = request.Data.SalaryFrom,
|
||||
SalaryTo = request.Data.SalaryTo,
|
||||
IsOverTimeEligible = request.Data.IsOverTimeEligible,
|
||||
Status = request.Data.Status
|
||||
};
|
||||
|
||||
_context.Grade.Add(entity);
|
||||
await _context.SaveChangesAsync(cancellationToken);
|
||||
|
||||
return new CreateGradeResponse
|
||||
{
|
||||
Id = entity.Id,
|
||||
Code = entity.Code
|
||||
};
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user