public async Task <IActionResult> UpdateInGameUser([FromBody] UpdateInGameUserRequest updateInGameUserRequest) { try { var userId = User.FindFirstValue(ClaimTypes.NameIdentifier); await gameService.UpdateInGameUserAsync(updateInGameUserRequest, userId); return(Ok("User updated")); } catch (ItemNotFoundException e) { return(BadRequest(e.Message)); } catch (HostMisMatchException e) { return(BadRequest(e.Message)); } }
public async Task UpdateInGameUserAsync(UpdateInGameUserRequest updateInGameUserRequest, string userId) { var game = context.Games .Where(g => g.Id == updateInGameUserRequest.GameId) .SingleOrDefault() ?? throw new ItemNotFoundException($"Game with id {updateInGameUserRequest.GameId} not found."); var inGameUser = context.InGameUsers .Where(i => i.InGameUserId == updateInGameUserRequest.InGameUserId) .SingleOrDefault() ?? throw new ItemNotFoundException($"User with id {updateInGameUserRequest.InGameUserId} not found."); if (game.HostingUserId == userId) { inGameUser.Level = updateInGameUserRequest.Level <= 1 ? 1 : updateInGameUserRequest.Level; inGameUser.Bonus = updateInGameUserRequest.Bonus <= 0 ? 0 : updateInGameUserRequest.Bonus; context.InGameUsers.Update(inGameUser); await context.SaveChangesAsync(); } else { throw new HostMisMatchException("You cannot delete this game, not belong to you."); } }