示例#1
0
        public async Task <RequestResponseModel <UserViewModel> > SignUpAsync(RegistrationWriteModel signUpData)
        {
            var errors = await _userValidator.CheckSignUpDataBeforeRegisteringAsync(signUpData);

            if (errors.Any())
            {
                return(new RequestResponseModel <UserViewModel>(StatusCodes.Status400BadRequest,
                                                                errors,
                                                                null));
            }

            var passwordHash = _passwordHasher.EncodePassword(signUpData.Password);
            var role         = await _userValidator.GetUserRoleBeforeRegisterAsync();

            var registeredUser = await _userRepository.SignUpAsync(new UserEntity
            {
                MailAddress  = signUpData.MailAddress,
                Username     = signUpData.Username,
                PasswordHash = passwordHash,
                Role         = role
            });

            if (registeredUser == null)
            {
                return(new RequestResponseModel <UserViewModel>(StatusCodes.Status400BadRequest,
                                                                new List <ErrorViewModel>
                {
                    ErrorViewModel.Factory.NewErrorFromMessage(_localizer["UserCreationErrorMessage"]
                                                               .Value)
                },
                                                                null));
            }

            await _userRepository.SaveChangesAsync();

            var result = (UserViewModel)registeredUser;

            return(new RequestResponseModel <UserViewModel>(StatusCodes.Status200OK,
                                                            Enumerable.Empty <ErrorViewModel>(),
                                                            result));
        }