using AutobusApi.Application.Common.Interfaces; using AutobusApi.Domain.Entities; using AutobusApi.Domain.Enums; using MediatR; namespace AutobusApi.Application.Companies.Commands.CreateCompany; public class CreateCompanyCommandHandler : IRequestHandler { private readonly IApplicationDbContext _dbContext; private readonly IIdentityService _identityService; public CreateCompanyCommandHandler( IApplicationDbContext dbContext, IIdentityService identityService) { _dbContext = dbContext; _identityService = identityService; } public async Task Handle( CreateCompanyCommand request, CancellationToken cancellationToken) { var company = new Company(); company.Name = request.Name; company.LegalAddress = request.LegalAddress; company.ContactPhoneNumber = request.ContactPhoneNumber; company.ContactEmail = request.ContactEmail; var userId = await _identityService.RegisterAsync(request.OwnerEmail, request.OwnerPassword, cancellationToken); company.Employees = new List() { new Employee() { IdentityId = userId, FirstName = request.OwnerFirstName, LastName = request.OwnerLastName, Patronymic = request.OwnerPatronymic, Sex = Enum.Parse(request.OwnerSex), BirthDate = request.OwnerBirthDate, Documents = new List() { new EmployeeDocument() { Type = Enum.Parse(request.OwnerDocumentType), Information = request.OwnerDocumentInformation } } } }; _dbContext.Companies.Add(company); await _dbContext.SaveChangesAsync(cancellationToken); return company.Id; } }