73 lines
2.4 KiB
C#
73 lines
2.4 KiB
C#
using Indotalent.ConfigBackEnd.Exceptions;
|
|
using Indotalent.ConfigBackEnd.Extensions;
|
|
using Indotalent.Infrastructure.Database;
|
|
using MediatR;
|
|
using Microsoft.EntityFrameworkCore;
|
|
|
|
namespace Indotalent.Features.Organization.Designation.Cqrs;
|
|
|
|
public class CreateDesignationRequest
|
|
{
|
|
public string? Code { get; set; }
|
|
public string? Name { get; set; }
|
|
public string? Description { get; set; }
|
|
public string? Level { get; set; }
|
|
public string OtherInformation1 { get; set; } = string.Empty;
|
|
public string OtherInformation2 { get; set; } = string.Empty;
|
|
public string OtherInformation3 { get; set; } = string.Empty;
|
|
}
|
|
|
|
public class CreateDesignationResponse
|
|
{
|
|
public string? Id { get; set; }
|
|
public string? Code { get; set; }
|
|
}
|
|
|
|
public record CreateDesignationCommand(CreateDesignationRequest Data) : IRequest<CreateDesignationResponse>;
|
|
|
|
public class CreateDesignationHandler : IRequestHandler<CreateDesignationCommand, CreateDesignationResponse>
|
|
{
|
|
private readonly AppDbContext _context;
|
|
|
|
public CreateDesignationHandler(AppDbContext context) => _context = context;
|
|
|
|
public async Task<CreateDesignationResponse> Handle(CreateDesignationCommand request, CancellationToken cancellationToken)
|
|
{
|
|
var isExists = await _context.Designation
|
|
.AnyAsync(x => x.Code == request.Data.Code, cancellationToken);
|
|
|
|
if (isExists)
|
|
{
|
|
throw new AlreadyExistsException("Designation", request.Data.Code ?? string.Empty);
|
|
}
|
|
|
|
var entityName = nameof(Data.Entities.Designation);
|
|
|
|
var autoNo = await _context.GenerateAutoNumberAsync(
|
|
entityName: entityName,
|
|
prefixTemplate: $"{entityName.ToShortNameConsonant(3)}/{{Year}}/",
|
|
ct: cancellationToken
|
|
);
|
|
|
|
var entity = new Data.Entities.Designation
|
|
{
|
|
AutoNumber = autoNo,
|
|
Code = request.Data.Code,
|
|
Name = request.Data.Name,
|
|
Description = request.Data.Description,
|
|
Level = request.Data.Level,
|
|
OtherInformation1 = request.Data.OtherInformation1,
|
|
OtherInformation2 = request.Data.OtherInformation2,
|
|
OtherInformation3 = request.Data.OtherInformation3
|
|
};
|
|
|
|
_context.Designation.Add(entity);
|
|
await _context.SaveChangesAsync(cancellationToken);
|
|
|
|
return new CreateDesignationResponse
|
|
{
|
|
Id = entity.Id,
|
|
Code = entity.Code
|
|
};
|
|
}
|
|
} |