69 lines
2.0 KiB
C#
69 lines
2.0 KiB
C#
using Indotalent.ConfigBackEnd.Exceptions;
|
|
using Indotalent.ConfigBackEnd.Extensions;
|
|
using Indotalent.Infrastructure.Database;
|
|
using MediatR;
|
|
using Microsoft.EntityFrameworkCore;
|
|
|
|
namespace Indotalent.Features.Setting.Tax.Cqrs;
|
|
|
|
public class CreateTaxRequest
|
|
{
|
|
public string? Code { get; set; }
|
|
public string? Name { get; set; }
|
|
public decimal PercentageValue { get; set; }
|
|
public string? Category { get; set; }
|
|
public string? Description { get; set; }
|
|
}
|
|
|
|
public class CreateTaxResponse
|
|
{
|
|
public string? Id { get; set; }
|
|
public string? Code { get; set; }
|
|
}
|
|
|
|
public record CreateTaxCommand(CreateTaxRequest Data) : IRequest<CreateTaxResponse>;
|
|
|
|
public class CreateTaxHandler : IRequestHandler<CreateTaxCommand, CreateTaxResponse>
|
|
{
|
|
private readonly AppDbContext _context;
|
|
|
|
public CreateTaxHandler(AppDbContext context) => _context = context;
|
|
|
|
public async Task<CreateTaxResponse> Handle(CreateTaxCommand request, CancellationToken cancellationToken)
|
|
{
|
|
var isExists = await _context.Tax
|
|
.AnyAsync(x => x.Code == request.Data.Code, cancellationToken);
|
|
|
|
if (isExists)
|
|
{
|
|
throw new AlreadyExistsException("Tax", request.Data.Code ?? string.Empty);
|
|
}
|
|
|
|
var entityName = nameof(Data.Entities.Tax);
|
|
|
|
var autoNo = await _context.GenerateAutoNumberAsync(
|
|
entityName: entityName,
|
|
prefixTemplate: $"{entityName.ToShortNameConsonant(3)}/{{Year}}/",
|
|
ct: cancellationToken
|
|
);
|
|
|
|
var entity = new Data.Entities.Tax
|
|
{
|
|
AutoNumber = autoNo,
|
|
Code = request.Data.Code,
|
|
Name = request.Data.Name,
|
|
PercentageValue = request.Data.PercentageValue,
|
|
Category = request.Data.Category,
|
|
Description = request.Data.Description
|
|
};
|
|
|
|
_context.Tax.Add(entity);
|
|
await _context.SaveChangesAsync(cancellationToken);
|
|
|
|
return new CreateTaxResponse
|
|
{
|
|
Id = entity.Id,
|
|
Code = entity.Code
|
|
};
|
|
}
|
|
} |