public ActionResult <List <Tournament> > GetActiveTournaments(string userId = null) { List <Tournament> TournamentList = new List <Tournament>(); try { if (userId != null) { Int32.TryParse(userId, out int UserId); User user = unitOfWork.UserRepository.GetByID(UserId); if (user.GetType() == typeof(Player)) { foreach (var league in unitOfWork.TournamentRepository.Get(null, null, "League")) { foreach (var leagueMembership in (user as Player).LeagueMemberShips) { if (league.LeagueId == leagueMembership.LeagueId) { TournamentList.Add(league); } } } } else { foreach (var league in unitOfWork.TournamentRepository.Get(null, null, "League")) { foreach (var League in (user as LeagueOwner).Leagues) { if (league.LeagueId == League.LeagueId) { TournamentList.Add(league); } } } } } else { foreach (var league in unitOfWork.TournamentRepository.Get(null, null, "League")) { TournamentList.Add(league); } } return(TournamentList); } catch (Exception) { return(StatusCode(500, "Something went wrong!")); } }