public ActionResult AddUser(CreateUserRequest createRequest) { if (_validator.Validate(createRequest)) { return(BadRequest(new { error = "users must have a username and password" })); } var newUser = _repository.AddUser(createRequest.Username, createRequest.Password); return(Created($"api/users/{newUser.Id}", newUser)); }
public ActionResult <int> AddUser(CreateUserRequest createRequest) { if (!_validator.Validate(createRequest)) { return(BadRequest(new { error = "users must have a username and password" })); } var userRepository = new UserRepository(); var newUser = _userRepository.AddUser(createRequest.Username, createRequest.Password); //http response return(Created($"api/users/{newUser.Id}", newUser)); }
// asp.net is creating user for us. // Bad Request and Crated methods are getting inherited from the base class; ActionResult doesnt necessarily need a type //public ActionResult<int> AddUser(User newUser) public ActionResult AddUser(CreateUserRequest createRequest) { if (!_validator.Validate(createRequest)) { // below is how you create a new anonymous type; have to return bad request in order for it to actually be seen return(BadRequest(new { error = "users must have a username and password" })); } var newUser = _userRepository.AddUser(createRequest.Username, createRequest.Password); // ****HTTP Response**** // Created method is a method that gives us 201 return(Created($"api/users/{newUser.Id}", newUser)); // Other way to write it...kinda makes more sense // ************ If it is valid, do this. Else return error ************ //if (_validator.Validate(createRequest)) //{ // var newUser = _userRepository.AddUser(createRequest.Username, createRequest.Password); // return Created($"api/users/{newUser.Id}", newUser); //} //return BadRequest(new { error = "users must have a username and password" }); }