async Task <Guid?> IEmployeeServiceAdmin.CreateAsync(Common.Models.Employee.Employee employee) { Guard.ArgumentIsNotNull(employee, nameof(employee)); EmployeeValidator.Validate(employee); await ValdiateIfUserWithSameCompanyEmailExists(employee); employee.EmployeeId = Guid.NewGuid(); EmployeeDto dto = _mapper.Map <Common.Models.Employee.Employee, EmployeeDto>(employee); Guid?employeeId = await _employeeRepositoryAdmin.CreateAsync(dto); if (employeeId == null) { return(null); } await _eventPublisher.PublishAsync <IEmployeeCreatedEvent>(new EmployeeCreatedEvent { EmployeeId = employee.EmployeeId, FullName = employee.FullName, JobTitle = employee.JobTitle, Technology = employee.Technology, StartDate = employee.StartDate }); return(employeeId); }
async Task <int> IEmployeeServiceAdmin.UpdateAsync(Common.Models.Employee.Employee employee) { Guard.ArgumentIsNotNull(employee, nameof(employee)); EmployeeValidator.Validate(employee); await ValdiateIfUserWithSameCompanyEmailExists(employee); string bioUrl = MoveBioFromTempFolder(employee.EmployeeIdForFiles, employee.EmployeeId); string photoUrl = MovePhotoFromTempFolder(employee.EmployeeIdForFiles, employee.EmployeeId); DeleteTempFolder(employee.EmployeeIdForFiles); employee.BioUrl = bioUrl; employee.PhotoUrl = photoUrl; EmployeeDto dto = _mapper.Map <Common.Models.Employee.Employee, EmployeeDto>(employee); int updatedRowsCount = await _employeeRepositoryAdmin.UpdateAsync(dto); if (updatedRowsCount == 0) { return(0); } await _eventPublisher.PublishAsync <IEmployeeUpdatedEvent>( new EmployeeUpdatedEvent { EmployeeId = employee.EmployeeId, FullName = employee.FullName, JobTitle = employee.JobTitle, Technology = employee.Technology, StartDate = employee.StartDate }); return(updatedRowsCount); }
private async Task ValdiateIfUserWithSameCompanyEmailExists(Common.Models.Employee.Employee employee) { EmployeeDto employeeWithSameCompanyEmail = await _employeeRepositoryAdmin.GetByCompanyEmailAsync(employee.CompanyEmail); if (employeeWithSameCompanyEmail != null && employeeWithSameCompanyEmail.EmployeeId != employee.EmployeeId) { throw new ValidationException("Employee with same Company Email already exists."); } }
async Task <Common.Models.Employee.Employee> IEmployeeServiceAdmin.GetByIdAsync(Guid id) { Guard.ArgumentIsNotNullOrEmpty(id, nameof(id)); EmployeeDto dto = await _employeeRepositoryAdmin.GetByIdAsync(id); Common.Models.Employee.Employee result = _mapper.Map <EmployeeDto, Common.Models.Employee.Employee>(dto); return(result); }
public static void Validate(Common.Models.Employee.Employee employee) { Guard.ArgumentIsNotNull(employee, nameof(employee)); if (string.IsNullOrWhiteSpace(employee.FullName)) { throw new ValidationException("FullName is required"); } if (employee.FullName.Length > FieldLength) { throw new ValidationException($"FullName length can not be more than {FieldLength} symbols"); } if (employee.FullNameCyrillic != null && employee.FullNameCyrillic.Length > FieldLength) { throw new ValidationException($"FullNameCyrillic length can not be more than {FieldLength} symbols"); } if (employee.PatronymicCyrillic != null && employee.PatronymicCyrillic.Length > FieldLength) { throw new ValidationException($"PatronymicCyrillic length can not be more than {FieldLength} symbols"); } if (string.IsNullOrWhiteSpace(employee.JobTitle)) { throw new ValidationException("JobTitle is required"); } if (employee.JobTitle.Length > FieldLength) { throw new ValidationException($"JobTitle length can not be more than {FieldLength} symbols"); } if (employee.DepartmentName != null && employee.DepartmentName.Length > FieldLength) { throw new ValidationException($"DepartmentName length can not be more than {FieldLength} symbols"); } if (employee.Technology != null && employee.Technology.Length > FieldLength) { throw new ValidationException($"Technology length can not be more than {FieldLength} symbols"); } if (employee.ProjectName != null && employee.ProjectName.Length > FieldLength) { throw new ValidationException($"ProjectName length can not be more than {FieldLength} symbols"); } if (string.IsNullOrWhiteSpace(employee.CompanyEmail)) { throw new ValidationException("CompanyEmail is required"); } if (employee.CompanyEmail.Length > EmailLength) { throw new ValidationException($"CompanyEmail length can not be more than {EmailLength} symbols"); } if (!Regex.IsMatch(employee.CompanyEmail, EmailPattern)) { throw new ValidationException("CompanyEmail does not have an email format"); } if (employee.PersonalEmail != null && employee.PersonalEmail.Length > EmailLength) { throw new ValidationException($"PersonalEmail length can not be more than {EmailLength} symbols"); } if (employee.PersonalEmail != null && !Regex.IsMatch(employee.PersonalEmail, EmailPattern)) { throw new ValidationException("PersonalEmail does not have an email format"); } if (employee.Messenger?.Name != null && employee.Messenger.Name.Length > EmailLength) { throw new ValidationException($"Messanger name length can not be more than {EmailLength} symbols"); } if (employee.Messenger?.Login != null && employee.Messenger.Login.Length > EmailLength) { throw new ValidationException($"Messanger login length can not be more than {EmailLength} symbols"); } if (employee.MobileNumber != null && employee.MobileNumber.Length > MobileNumberLength) { throw new ValidationException($"MobileNumber length can not be more than {MobileNumberLength} symbols"); } if (employee.AdditionalMobileNumber != null && employee.AdditionalMobileNumber.Length > MobileNumberLength) { throw new ValidationException($"AdditionalMobileNumber length can not be more than {MobileNumberLength} symbols"); } if (employee.DaysSkipped < 0) { throw new ValidationException("DaysSkipped can not be less than zero."); } if (employee.BioUrl != null && employee.BioUrl.Length > BioUrlLength) { throw new ValidationException($"AdditionalMobileNumber length can not be more than {BioUrlLength} symbols"); } if (employee.Notes != null && employee.Notes.Length > NotesLength) { throw new ValidationException($"Notes length can not be more than {NotesLength} symbols"); } if (employee.PhotoUrl != null && employee.PhotoUrl.Length > BioUrlLength) { throw new ValidationException($"PhotoUrl length can not be more than {BioUrlLength} symbols"); } }