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.Deduction.Cqrs;
|
||||
|
||||
public class CreateDeductionRequest
|
||||
{
|
||||
public string? Code { get; set; }
|
||||
public string? Name { get; set; }
|
||||
public string? Description { get; set; }
|
||||
public string? Category { get; set; }
|
||||
public bool PreTax { get; set; }
|
||||
public string? CalculationMethod { get; set; }
|
||||
public string? Status { get; set; } = "Active";
|
||||
}
|
||||
|
||||
public class CreateDeductionResponse
|
||||
{
|
||||
public string? Id { get; set; }
|
||||
public string? Code { get; set; }
|
||||
}
|
||||
|
||||
public record CreateDeductionCommand(CreateDeductionRequest Data) : IRequest<CreateDeductionResponse>;
|
||||
|
||||
public class CreateDeductionHandler : IRequestHandler<CreateDeductionCommand, CreateDeductionResponse>
|
||||
{
|
||||
private readonly AppDbContext _context;
|
||||
|
||||
public CreateDeductionHandler(AppDbContext context) => _context = context;
|
||||
|
||||
public async Task<CreateDeductionResponse> Handle(CreateDeductionCommand request, CancellationToken cancellationToken)
|
||||
{
|
||||
var isExists = await _context.Deduction
|
||||
.AnyAsync(x => x.Code == request.Data.Code, cancellationToken);
|
||||
|
||||
if (isExists)
|
||||
{
|
||||
throw new AlreadyExistsException("Deduction Component", request.Data.Code ?? string.Empty);
|
||||
}
|
||||
|
||||
var entityName = nameof(Data.Entities.Deduction);
|
||||
|
||||
var autoNo = await _context.GenerateAutoNumberAsync(
|
||||
entityName: entityName,
|
||||
prefixTemplate: $"{entityName.ToShortNameConsonant(3)}/{{Year}}/",
|
||||
ct: cancellationToken
|
||||
);
|
||||
|
||||
var entity = new Data.Entities.Deduction
|
||||
{
|
||||
AutoNumber = autoNo,
|
||||
Code = request.Data.Code,
|
||||
Name = request.Data.Name,
|
||||
Description = request.Data.Description,
|
||||
Category = request.Data.Category,
|
||||
PreTax = request.Data.PreTax,
|
||||
CalculationMethod = request.Data.CalculationMethod,
|
||||
Status = request.Data.Status
|
||||
};
|
||||
|
||||
_context.Deduction.Add(entity);
|
||||
await _context.SaveChangesAsync(cancellationToken);
|
||||
|
||||
return new CreateDeductionResponse
|
||||
{
|
||||
Id = entity.Id,
|
||||
Code = entity.Code
|
||||
};
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user