public async Task <IActionResult> CreateUserAsync([FromBody] Client.UserCreationInfo creationInfo, CancellationToken cancellationToken) { cancellationToken.ThrowIfCancellationRequested(); if (creationInfo == null) { var error = ServiceErrorResponses.BodyIsMissing("UserCreationInfo"); return(BadRequest(error)); } var hashPassword = PasswordEncoder.Encode(creationInfo.Password); var modelCreationInfo = new UserCreationInfo(creationInfo.Login, hashPassword); User modelUser = null; try { modelUser = await repository.CreateAsync(modelCreationInfo, cancellationToken) .ConfigureAwait(false); } catch (UserDuplicationException) { var error = ServiceErrorResponses.UserDuplication(modelCreationInfo.Login); return(BadRequest(error)); } var clientUser = UserConverter.Convert(modelUser); return(Ok(clientUser)); }
public async Task <IActionResult> CreateAsync([FromBody] Client.UserCreationInfo clientCreationInfo, CancellationToken cancellationToken) { cancellationToken.ThrowIfCancellationRequested(); if (clientCreationInfo == null) { var error = Responses.BodyIsMissing(nameof(clientCreationInfo)); return(BadRequest(error)); } var modelCreationInfo = ModelConverters.Users.UserCreationInfoConverter.Convert(clientCreationInfo); var user = await userManager.FindByNameAsync(modelCreationInfo.UserName); if (user != null) { var error = Responses.DuplicationError($"User with username {modelCreationInfo.UserName} already use", "User"); return(BadRequest(error)); } var dateTime = DateTime.UtcNow; var modelUser = new User { UserName = modelCreationInfo.UserName, Email = modelCreationInfo.Email, PhoneNumber = modelCreationInfo.PhoneNumber, CreatedAt = dateTime, LastUpdatedAt = dateTime }; var result = await userManager.CreateAsync(modelUser, modelCreationInfo.Password); if (!result.Succeeded) { var error = Responses.InvalidData(result.Errors.First().ToString(), "NotValid"); return(BadRequest(error)); } await userManager.AddToRoleAsync(modelUser, "user"); var clientUser = ModelConverters.Users.UserConverter.Convert(modelUser); return(CreatedAtRoute("GetUserRoute", new { userName = clientUser.UserName }, clientUser)); }
public static Model.UserCreationInfo Convert(Client.UserCreationInfo clientCreationInfo) { if (clientCreationInfo == null) { throw new ArgumentNullException(nameof(clientCreationInfo)); } if (string.IsNullOrEmpty(clientCreationInfo.UserName)) { throw new ArgumentNullException(nameof(clientCreationInfo.UserName)); } if (string.IsNullOrEmpty(clientCreationInfo.Name)) { throw new ArgumentNullException(nameof(clientCreationInfo.Name)); } if (string.IsNullOrEmpty(clientCreationInfo.Email)) { throw new ArgumentNullException(nameof(clientCreationInfo.Email)); } if (string.IsNullOrEmpty(clientCreationInfo.PhoneNumber)) { throw new ArgumentNullException(nameof(clientCreationInfo.PhoneNumber)); } if (string.IsNullOrEmpty(clientCreationInfo.Password)) { throw new ArgumentNullException(nameof(clientCreationInfo.Password)); } var modelCreationInfo = new Model.UserCreationInfo( clientCreationInfo.UserName, clientCreationInfo.Name, clientCreationInfo.Email, clientCreationInfo.PhoneNumber, clientCreationInfo.Password ); return(modelCreationInfo); }
public async Task <IActionResult> CreateUserAsync([FromBody] Client.UserCreationInfo clientUserCreationInfo, CancellationToken cancellationToken) { cancellationToken.ThrowIfCancellationRequested(); if (clientUserCreationInfo == null) { var error = ErrorResponsesService.BodyIsMissing(nameof(clientUserCreationInfo)); return(BadRequest(error)); } if (!clientUserCreationInfo.Password.Equals(clientUserCreationInfo.ConfirmPassword)) { var error = ErrorResponsesService.ValidationError(nameof(clientUserCreationInfo.ConfirmPassword), PasswordValidationMessage); return(BadRequest(error)); } UserCreationInfo modelCreationInfo; try { modelCreationInfo = Converter.UserCreationInfoConverter.Convert(clientUserCreationInfo); } catch (ArgumentNullException ex) { var error = ErrorResponsesService.InvalidCredentialsError(ex.Message); return(BadRequest(error)); } var user = await userManager.FindByNameAsync(modelCreationInfo.UserName); if (user != null) { var error = ErrorResponsesService.DuplicationError(Target, $"User with name '{clientUserCreationInfo.UserName}' already exists."); return(BadRequest(error)); } var dateTime = DateTime.UtcNow; var modelUser = new User { UserName = modelCreationInfo.UserName, Name = modelCreationInfo.Name, Email = modelCreationInfo.Email, PhoneNumber = modelCreationInfo.PhoneNumber, RegisteredAt = dateTime, LastUpdateAt = dateTime }; var result = await userManager.CreateAsync(modelUser, modelCreationInfo.Password); if (!result.Succeeded) { var error = ErrorResponsesService.ValidationError(nameof(clientUserCreationInfo), result.Errors.First().Description); return(BadRequest(error)); } await userManager.AddToRoleAsync(modelUser, "user"); var clientUser = ModelConverters.Users.UserConverter.Convert(modelUser); return(CreatedAtRoute("GetUserRoute", new { username = clientUser.UserName }, clientUser)); }