public async Task <ActionResult <UserResource> > UpdateUser(int id, [FromBody] UserSaveResource user) { var validator = new UserSaveResourceValidator(); var validationResult = await validator.ValidateAsync(user); var requestIsInvalid = id == 0 || !validationResult.IsValid; if (requestIsInvalid) { return(BadRequest(validationResult.Errors)); // this needs refining, but for demo it is ok } var userToBeUpdate = await _userService.GetById(id); if (userToBeUpdate == null) { return(NotFound()); } var mappedUser = _mapper.Map <UserSaveResource, User>(user); await _userService.UpdateUser(userToBeUpdate, mappedUser); var updatedUser = await _userService.GetById(id); var updatedUserResource = _mapper.Map <User, UserResource>(updatedUser); return(Ok(updatedUserResource)); }
public async Task <ActionResult <UserResource> > CreateUser([FromBody] UserSaveResource user) { var validator = new UserSaveResourceValidator(); var validationResult = await validator.ValidateAsync(user); if (!validationResult.IsValid) { return(BadRequest(validationResult.Errors)); // this needs refining, but for demo it is ok } var userToCreate = _mapper.Map <UserSaveResource, User>(user); userToCreate.Statistics = new Statistics() { NumberOfGamesPlayed = 0, SuccesfullGames = 0, TimeInGames = 0 }; var newUser = await _userService.CreateUser(userToCreate); var createdUser = await _userService.GetById(newUser.Id); var userResource = _mapper.Map <User, UserResource>(createdUser); return(Ok(userResource)); }