public async Task RegisterStaffAsync(RegisterStaffDTO registrationDTO, int userId) { var hotelAdmin = context.HotelAdmins.Get(userId); if (hotelAdmin == null) { throw new NotFoundException("No such hotel admin"); } int hotelId = hotelAdmin.Hotel.Id; ValidationResults result = ModelValidator.IsValid(registrationDTO); if (!result.Successed) { throw ValidationExceptionBuilder.BuildValidationException(result); } User newUser = new HotelStaff() { FirstName = registrationDTO.FirstName, LastName = registrationDTO.LastName, Email = registrationDTO.Email, UserName = registrationDTO.Email, Category = registrationDTO.Category }; string password = registrationDTO.Password; await CheckIfThePasswordIsValid(password); await CheckIfTheUserDoesNotExist(newUser); var isCreated = await _userManager.CreateAsync(newUser, password); if (!isCreated.Succeeded) { throw new DBException("Cann't create new user"); } var isAddedToRole = await _userManager.AddToRoleAsync(newUser, "hotel-staff"); if (!isAddedToRole.Succeeded) { throw new DBException("Cann't add new user to hotel-staff's role"); } var code = await _userManager.GenerateEmailConfirmationTokenAsync(newUser); string message = "Ваш код пiдтвердження: " + code; await EmailService.SendEmailAsync(newUser.Email, "HotelLocker пiдтвердження паролю", message); }
public async Task RegisterStaff([FromBody] RegisterStaffDTO registerDTO) { await accountService.RegisterStaffAsync(registerDTO, GetUserId()); }