public IActionResult CreateUser([FromBody] User user) { UserValidator validator = new UserValidator(); var results = validator.Validate(user); var errors = results.ToString("\n"); if (errors != string.Empty) { var errorList = ErrorFormatter.FormatValidationErrors(errors); return(BadRequest(new { Errors = errorList })); } if (_repository.GetUser(user.EmailAddress) != null) { return(BadRequest("Email address already exists. Please sign in")); } try { User newUser = _repository.CreateUser(user); var mappedNewUser = _userMappings.StripSensitiveDataSingleUser(newUser); return(CreatedAtRoute("User At Id", new { userId = mappedNewUser.Id }, mappedNewUser)); } catch (Exception e) { _logger.LogError($"Something went wrong while trying to create user. ${e}"); } return(StatusCode(500, "Unable to create new user. Try again later")); }
public IActionResult GetUser(int userId) { var user = _repository.GetUser(userId); if (user == null) { return(NotFound()); } var mappedUser = _userMappings.StripSensitiveDataSingleUser(user); return(Ok(mappedUser)); }