Files
2026-07-21 14:22:06 +07:00

61 lines
2.0 KiB
C#

using Indotalent.ConfigBackEnd.Extensions;
using Indotalent.Infrastructure.Database;
using MediatR;
namespace Indotalent.Features.Performance.Promotion.Cqrs;
public class CreatePromotionRequest
{
public string EmployeeId { get; set; } = string.Empty;
public string FromGrade { get; set; } = string.Empty;
public string ToGrade { get; set; } = string.Empty;
public DateTime? EffectiveDate { get; set; }
public string PromotionNote { get; set; } = string.Empty;
public string Status { get; set; } = string.Empty;
}
public class CreatePromotionResponse
{
public string? Id { get; set; }
public string? AutoNumber { get; set; }
}
public record CreatePromotionCommand(CreatePromotionRequest Data) : IRequest<CreatePromotionResponse>;
public class CreatePromotionHandler : IRequestHandler<CreatePromotionCommand, CreatePromotionResponse>
{
private readonly AppDbContext _context;
public CreatePromotionHandler(AppDbContext context) => _context = context;
public async Task<CreatePromotionResponse> Handle(CreatePromotionCommand request, CancellationToken cancellationToken)
{
var entityName = nameof(Data.Entities.Promotion);
var autoNo = await _context.GenerateAutoNumberAsync(
entityName: entityName,
prefixTemplate: $"{entityName.ToShortNameConsonant(3)}/{{Year}}/",
ct: cancellationToken
);
var entity = new Data.Entities.Promotion
{
AutoNumber = autoNo,
EmployeeId = request.Data.EmployeeId,
FromGrade = request.Data.FromGrade,
ToGrade = request.Data.ToGrade,
EffectiveDate = request.Data.EffectiveDate,
PromotionNote = request.Data.PromotionNote,
Status = request.Data.Status
};
_context.Promotion.Add(entity);
await _context.SaveChangesAsync(cancellationToken);
return new CreatePromotionResponse
{
Id = entity.Id,
AutoNumber = entity.AutoNumber
};
}
}