public async Task <SingleGameSessionViewModel> LeaveCurrentGameSession(int gameSessionID, ClaimsPrincipal user) { string userID = user.FindAll(ClaimTypes.NameIdentifier).FirstOrDefault().Value; var currentUser = _db.Users.GetUserAndProfileById(userID); var currentGameSession = await _db.GameSessions.GetActiveGameSessionByIdAsync(gameSessionID); List <ApplicationUser> gameSessionPlayers = _db.GameSessionApplicationUsers.GetGameSessionPlayers(currentGameSession); var adminEvent = _db.Users.GetEventAdmin(currentGameSession); if (adminEvent == currentUser) { var errorMessage = "You are event administrator. U cant leave current game"; throw new RequestException(HttpStatusCode.BadRequest, errorMessage, LoggingEvents.LeaveSingleGameSession); } else if (!gameSessionPlayers.Contains(currentUser)) { var errorMessage = "You are not in game. U cant leave current game"; throw new RequestException(HttpStatusCode.BadRequest, errorMessage, LoggingEvents.LeaveSingleGameSession); } else { var gameSesAppUser = _db.GameSessionApplicationUsers.GetGameSessionAppUser(currentGameSession.ID, userID); await _db.LeaveGameSession(gameSesAppUser, currentGameSession); _logger.LogInformation(LoggingEvents.LeaveSingleGameSession, "Leave GameSession successfully"); await _db.UpdateNumberGamesLeft(currentUser.Profile); var singleGameSessionAfterJoin = await GetSingleGameSessionAsync(currentGameSession.ID, user); return(singleGameSessionAfterJoin); } }