103 lines
3.6 KiB
C#
103 lines
3.6 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? Name { get; set; }
|
|
public string? Description { get; set; }
|
|
public string? Street { get; set; }
|
|
public string? City { get; set; }
|
|
public string? State { get; set; }
|
|
public string? ZipCode { get; set; }
|
|
public string? Country { get; set; }
|
|
public string? PhoneNumber { get; set; }
|
|
public string? FaxNumber { get; set; }
|
|
public string? EmailAddress { get; set; }
|
|
public string? Website { get; set; }
|
|
public string? WhatsApp { get; set; }
|
|
public string? LinkedIn { get; set; }
|
|
public string? Facebook { get; set; }
|
|
public string? Instagram { get; set; }
|
|
public string? TwitterX { get; set; }
|
|
public string? TikTok { get; set; }
|
|
public string? EmployeeGroupId { get; set; }
|
|
public string? EmployeeCategoryId { get; set; }
|
|
public string? JobTitle { get; set; }
|
|
public decimal CommissionRate { get; set; }
|
|
public string? EmployeeNumber { get; set; }
|
|
}
|
|
|
|
public class CreateEmployeeResponse
|
|
{
|
|
public string? Id { get; set; }
|
|
public string? Name { 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.Name == request.Data.Name, cancellationToken);
|
|
|
|
if (isExists)
|
|
{
|
|
throw new AlreadyExistsException("Employee", request.Data.Name ?? string.Empty);
|
|
}
|
|
|
|
var entityName = nameof(Data.Entities.Employee);
|
|
|
|
var autoNo = await _context.GenerateAutoNumberAsync(
|
|
entityName: entityName,
|
|
prefixTemplate: $"{entityName.ToShortNameConsonant(3)}/{{Year}}/",
|
|
ct: cancellationToken
|
|
);
|
|
|
|
var entity = new Data.Entities.Employee
|
|
{
|
|
AutoNumber = autoNo,
|
|
Name = request.Data.Name,
|
|
Description = request.Data.Description,
|
|
Street = request.Data.Street,
|
|
City = request.Data.City,
|
|
State = request.Data.State,
|
|
ZipCode = request.Data.ZipCode,
|
|
Country = request.Data.Country,
|
|
PhoneNumber = request.Data.PhoneNumber,
|
|
FaxNumber = request.Data.FaxNumber,
|
|
EmailAddress = request.Data.EmailAddress,
|
|
Website = request.Data.Website,
|
|
WhatsApp = request.Data.WhatsApp,
|
|
LinkedIn = request.Data.LinkedIn,
|
|
Facebook = request.Data.Facebook,
|
|
Instagram = request.Data.Instagram,
|
|
TwitterX = request.Data.TwitterX,
|
|
TikTok = request.Data.TikTok,
|
|
EmployeeGroupId = request.Data.EmployeeGroupId,
|
|
EmployeeCategoryId = request.Data.EmployeeCategoryId,
|
|
JobTitle = request.Data.JobTitle,
|
|
CommissionRate = request.Data.CommissionRate,
|
|
EmployeeNumber = request.Data.EmployeeNumber
|
|
};
|
|
|
|
_context.Employee.Add(entity);
|
|
await _context.SaveChangesAsync(cancellationToken);
|
|
|
|
return new CreateEmployeeResponse
|
|
{
|
|
Id = entity.Id,
|
|
Name = entity.Name
|
|
};
|
|
}
|
|
} |