using AutobusApi.Application.Common.Interfaces; using AutobusApi.Domain.Entities; using AutobusApi.Domain.Enums; using MediatR; namespace AutobusApi.Application.Employees.Commands.CreateEmployee; public class CreateEmployeeCommandHandler : IRequestHandler { private readonly IApplicationDbContext _dbContext; private readonly IIdentityService _identityService; public CreateEmployeeCommandHandler( IApplicationDbContext dbContext, IIdentityService identityService) { _dbContext = dbContext; _identityService = identityService; } public async Task Handle( CreateEmployeeCommand request, CancellationToken cancellationToken) { var employee = new Employee(); employee.EmployerCompanyId = request.CompanyId; employee.FirstName = request.FirstName; employee.LastName = request.LastName; employee.Patronymic = request.Patronymic; employee.Sex = Enum.Parse(request.Sex); employee.BirthDate = request.BirthDate; var userId = await _identityService.RegisterAsync(request.Email, request.Password, cancellationToken); employee.IdentityId = userId; employee.Documents = new List(); foreach (var document in request.Documents) { employee.Documents.Add(new EmployeeDocument() { Type = Enum.Parse(document.Type), Information = document.Information }); } _dbContext.Employees.Add(employee); await _dbContext.SaveChangesAsync(cancellationToken); return employee.Id; } }