public async Task <ActionResult <UserDTO> > CreateUser([FromBody] CreateUserDTO createUserResource) { var validator = new CreateUserResourceValidator(); var validationResult = await validator.ValidateAsync(createUserResource); if (!validationResult.IsValid) { return(BadRequest(validationResult.Errors)); // this needs refining, but for demo it is ok } var userToCreate = _mapper.Map <CreateUserDTO, User>(createUserResource); userToCreate.PasswordHash = HashHelper.CreatePasswordHash(createUserResource.Password, userToCreate.SecretKey); var newUser = await _userService.CreateUser(userToCreate); var user = await _userService.GetUserById(newUser.Id); var userResource = _mapper.Map <User, UserDTO>(user); return(Ok(userResource)); }
public async Task <UserResource> CreateUser(CreateUserResource newUser) { var validator = new CreateUserResourceValidator(); var validationResult = await validator.ValidateAsync(newUser); if (!validationResult.IsValid) { throw new ValidationException(validationResult.Errors); } var user = _mapper.Map <CreateUserResource, User>(newUser); IdentityResult result = await _userManager.CreateAsync(user, newUser.Password); if (!result.Succeeded) { throw ExceptionBuilder.Create("Eror when creating user - " + result.ToString(), HttpStatusCode.BadRequest); } var userModel = _mapper.Map <UserResource>(user); return(userModel); }