Files
blazor-saas-hrm/Features/Organization/Employee/Cqrs/CreateEmployeeDeductionHandler.cs
T
2026-07-21 14:22:06 +07:00

54 lines
1.9 KiB
C#

using Indotalent.ConfigBackEnd.Exceptions;
using Indotalent.ConfigBackEnd.Extensions;
using Indotalent.Infrastructure.Database;
using MediatR;
using Microsoft.EntityFrameworkCore;
namespace Indotalent.Features.Organization.Employee.Cqrs;
public class CreateEmployeeDeductionRequest
{
public string? EmployeeId { get; set; }
public string? DeductionId { get; set; }
public decimal Amount { get; set; }
public string? Description { get; set; }
public bool IsActive { get; set; } = true;
}
public class CreateEmployeeDeductionResponse
{
public string? Id { get; set; }
}
public record CreateEmployeeDeductionCommand(CreateEmployeeDeductionRequest Data) : IRequest<CreateEmployeeDeductionResponse>;
public class CreateEmployeeDeductionHandler : IRequestHandler<CreateEmployeeDeductionCommand, CreateEmployeeDeductionResponse>
{
private readonly AppDbContext _context;
public CreateEmployeeDeductionHandler(AppDbContext context) => _context = context;
public async Task<CreateEmployeeDeductionResponse> Handle(CreateEmployeeDeductionCommand request, CancellationToken cancellationToken)
{
var entityName = nameof(Data.Entities.EmployeeDeduction);
var autoNo = await _context.GenerateAutoNumberAsync(
entityName: entityName,
prefixTemplate: $"EDED/{{Year}}/",
ct: cancellationToken
);
var entity = new Data.Entities.EmployeeDeduction
{
AutoNumber = autoNo,
EmployeeId = request.Data.EmployeeId,
DeductionId = request.Data.DeductionId,
Amount = request.Data.Amount,
Description = request.Data.Description,
IsActive = request.Data.IsActive
};
_context.EmployeeDeduction.Add(entity);
await _context.SaveChangesAsync(cancellationToken);
return new CreateEmployeeDeductionResponse { Id = entity.Id };
}
}