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 CreateEmployeeIncomeRequest { public string? EmployeeId { get; set; } public string? IncomeId { get; set; } public decimal Amount { get; set; } public string? Description { get; set; } public bool IsActive { get; set; } = true; } public class CreateEmployeeIncomeResponse { public string? Id { get; set; } } public record CreateEmployeeIncomeCommand(CreateEmployeeIncomeRequest Data) : IRequest; public class CreateEmployeeIncomeHandler : IRequestHandler { private readonly AppDbContext _context; public CreateEmployeeIncomeHandler(AppDbContext context) => _context = context; public async Task Handle(CreateEmployeeIncomeCommand request, CancellationToken cancellationToken) { var entityName = nameof(Data.Entities.EmployeeIncome); var autoNo = await _context.GenerateAutoNumberAsync( entityName: entityName, prefixTemplate: $"EINC/{{Year}}/", ct: cancellationToken ); var entity = new Data.Entities.EmployeeIncome { AutoNumber = autoNo, EmployeeId = request.Data.EmployeeId, IncomeId = request.Data.IncomeId, Amount = request.Data.Amount, Description = request.Data.Description, IsActive = request.Data.IsActive }; _context.EmployeeIncome.Add(entity); await _context.SaveChangesAsync(cancellationToken); return new CreateEmployeeIncomeResponse { Id = entity.Id }; } }