73 lines
2.3 KiB
C#
73 lines
2.3 KiB
C#
using Indotalent.ConfigBackEnd.Exceptions;
|
|
using Indotalent.ConfigBackEnd.Extensions;
|
|
using Indotalent.Infrastructure.Database;
|
|
using MediatR;
|
|
using Microsoft.EntityFrameworkCore;
|
|
|
|
namespace Indotalent.Features.Payroll.Income.Cqrs;
|
|
|
|
public class CreateIncomeRequest
|
|
{
|
|
public string? Code { get; set; }
|
|
public string? Name { get; set; }
|
|
public string? Description { get; set; }
|
|
public string? Type { get; set; }
|
|
public bool IsTaxable { get; set; } = true;
|
|
public string? CalculationBase { get; set; }
|
|
public string? Status { get; set; } = "Active";
|
|
}
|
|
|
|
public class CreateIncomeResponse
|
|
{
|
|
public string? Id { get; set; }
|
|
public string? Code { get; set; }
|
|
}
|
|
|
|
public record CreateIncomeCommand(CreateIncomeRequest Data) : IRequest<CreateIncomeResponse>;
|
|
|
|
public class CreateIncomeHandler : IRequestHandler<CreateIncomeCommand, CreateIncomeResponse>
|
|
{
|
|
private readonly AppDbContext _context;
|
|
|
|
public CreateIncomeHandler(AppDbContext context) => _context = context;
|
|
|
|
public async Task<CreateIncomeResponse> Handle(CreateIncomeCommand request, CancellationToken cancellationToken)
|
|
{
|
|
var isExists = await _context.Income
|
|
.AnyAsync(x => x.Code == request.Data.Code, cancellationToken);
|
|
|
|
if (isExists)
|
|
{
|
|
throw new AlreadyExistsException("Income Component", request.Data.Code ?? string.Empty);
|
|
}
|
|
|
|
var entityName = nameof(Data.Entities.Income);
|
|
|
|
var autoNo = await _context.GenerateAutoNumberAsync(
|
|
entityName: entityName,
|
|
prefixTemplate: $"{entityName.ToShortNameConsonant(3)}/{{Year}}/",
|
|
ct: cancellationToken
|
|
);
|
|
|
|
var entity = new Data.Entities.Income
|
|
{
|
|
AutoNumber = autoNo,
|
|
Code = request.Data.Code,
|
|
Name = request.Data.Name,
|
|
Description = request.Data.Description,
|
|
Type = request.Data.Type,
|
|
IsTaxable = request.Data.IsTaxable,
|
|
CalculationBase = request.Data.CalculationBase,
|
|
Status = request.Data.Status
|
|
};
|
|
|
|
_context.Income.Add(entity);
|
|
await _context.SaveChangesAsync(cancellationToken);
|
|
|
|
return new CreateIncomeResponse
|
|
{
|
|
Id = entity.Id,
|
|
Code = entity.Code
|
|
};
|
|
}
|
|
} |