128 lines
6.0 KiB
C#
128 lines
6.0 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 CreateEmployeeRequest
|
|
{
|
|
public string Code { get; set; } = string.Empty;
|
|
public string FirstName { get; set; } = string.Empty;
|
|
public string MiddleName { get; set; } = string.Empty;
|
|
public string LastName { get; set; } = string.Empty;
|
|
public string JobDescription { get; set; } = string.Empty;
|
|
public string GradeId { get; set; } = string.Empty;
|
|
public decimal BasicSalary { get; set; }
|
|
public string SalaryBankName { get; set; } = string.Empty;
|
|
public string SalaryBankAccountName { get; set; } = string.Empty;
|
|
public string SalaryBankAccountNumber { get; set; } = string.Empty;
|
|
public string PlaceOfBirth { get; set; } = string.Empty;
|
|
public DateTime? DateOfBirth { get; set; }
|
|
public string Gender { get; set; } = string.Empty;
|
|
public string MaritalStatus { get; set; } = string.Empty;
|
|
public string Religion { get; set; } = string.Empty;
|
|
public string BloodType { get; set; } = string.Empty;
|
|
public string IdentityNumber { get; set; } = string.Empty;
|
|
public string TaxNumber { get; set; } = string.Empty;
|
|
public string LastEducation { get; set; } = string.Empty;
|
|
public DateTime? JoinedDate { get; set; }
|
|
public DateTime? ResignedDate { get; set; }
|
|
public string EmployeeStatus { get; set; } = string.Empty;
|
|
public string EmploymentType { get; set; } = string.Empty;
|
|
public string StreetAddress { get; set; } = string.Empty;
|
|
public string City { get; set; } = string.Empty;
|
|
public string StateProvince { get; set; } = string.Empty;
|
|
public string ZipCode { get; set; } = string.Empty;
|
|
public string Phone { get; set; } = string.Empty;
|
|
public string Email { get; set; } = string.Empty;
|
|
public string BranchId { get; set; } = string.Empty;
|
|
public string DepartmentId { get; set; } = string.Empty;
|
|
public string DesignationId { get; set; } = string.Empty;
|
|
public string SocialMediaLinkedIn { get; set; } = string.Empty;
|
|
public string SocialMediaX { get; set; } = string.Empty;
|
|
public string SocialMediaFacebook { get; set; } = string.Empty;
|
|
public string SocialMediaInstagram { get; set; } = string.Empty;
|
|
public string SocialMediaTikTok { get; set; } = string.Empty;
|
|
public string OtherInformation1 { get; set; } = string.Empty;
|
|
public string OtherInformation2 { get; set; } = string.Empty;
|
|
public string OtherInformation3 { get; set; } = string.Empty;
|
|
}
|
|
|
|
public class CreateEmployeeResponse
|
|
{
|
|
public string? Id { get; set; }
|
|
public string? Code { get; set; }
|
|
}
|
|
|
|
public record CreateEmployeeCommand(CreateEmployeeRequest Data) : IRequest<CreateEmployeeResponse>;
|
|
|
|
public class CreateEmployeeHandler : IRequestHandler<CreateEmployeeCommand, CreateEmployeeResponse>
|
|
{
|
|
private readonly AppDbContext _context;
|
|
public CreateEmployeeHandler(AppDbContext context) => _context = context;
|
|
|
|
public async Task<CreateEmployeeResponse> Handle(CreateEmployeeCommand request, CancellationToken cancellationToken)
|
|
{
|
|
var isExists = await _context.Employee.AnyAsync(x => x.Code == request.Data.Code, cancellationToken);
|
|
if (isExists) throw new AlreadyExistsException("Employee", request.Data.Code);
|
|
|
|
var entityName = nameof(Data.Entities.Employee);
|
|
var autoNo = await _context.GenerateAutoNumberAsync(
|
|
entityName: entityName,
|
|
prefixTemplate: $"EMP/{{Year}}/",
|
|
ct: cancellationToken
|
|
);
|
|
|
|
var entity = new Data.Entities.Employee
|
|
{
|
|
AutoNumber = autoNo,
|
|
Code = request.Data.Code,
|
|
FirstName = request.Data.FirstName,
|
|
MiddleName = request.Data.MiddleName,
|
|
LastName = request.Data.LastName,
|
|
JobDescription = request.Data.JobDescription,
|
|
GradeId = request.Data.GradeId,
|
|
BasicSalary = request.Data.BasicSalary,
|
|
SalaryBankName = request.Data.SalaryBankName,
|
|
SalaryBankAccountName = request.Data.SalaryBankAccountName,
|
|
SalaryBankAccountNumber = request.Data.SalaryBankAccountNumber,
|
|
PlaceOfBirth = request.Data.PlaceOfBirth,
|
|
DateOfBirth = request.Data.DateOfBirth,
|
|
Gender = request.Data.Gender,
|
|
MaritalStatus = request.Data.MaritalStatus,
|
|
Religion = request.Data.Religion,
|
|
BloodType = request.Data.BloodType,
|
|
IdentityNumber = request.Data.IdentityNumber,
|
|
TaxNumber = request.Data.TaxNumber,
|
|
LastEducation = request.Data.LastEducation,
|
|
JoinedDate = request.Data.JoinedDate,
|
|
ResignedDate = request.Data.ResignedDate,
|
|
EmployeeStatus = request.Data.EmployeeStatus,
|
|
EmploymentType = request.Data.EmploymentType,
|
|
StreetAddress = request.Data.StreetAddress,
|
|
City = request.Data.City,
|
|
StateProvince = request.Data.StateProvince,
|
|
ZipCode = request.Data.ZipCode,
|
|
Phone = request.Data.Phone,
|
|
Email = request.Data.Email,
|
|
BranchId = request.Data.BranchId,
|
|
DepartmentId = request.Data.DepartmentId,
|
|
DesignationId = request.Data.DesignationId,
|
|
SocialMediaLinkedIn = request.Data.SocialMediaLinkedIn,
|
|
SocialMediaX = request.Data.SocialMediaX,
|
|
SocialMediaFacebook = request.Data.SocialMediaFacebook,
|
|
SocialMediaInstagram = request.Data.SocialMediaInstagram,
|
|
SocialMediaTikTok = request.Data.SocialMediaTikTok,
|
|
OtherInformation1 = request.Data.OtherInformation1,
|
|
OtherInformation2 = request.Data.OtherInformation2,
|
|
OtherInformation3 = request.Data.OtherInformation3
|
|
};
|
|
|
|
_context.Employee.Add(entity);
|
|
await _context.SaveChangesAsync(cancellationToken);
|
|
|
|
return new CreateEmployeeResponse { Id = entity.Id, Code = entity.Code };
|
|
}
|
|
} |