private void FillRelations(IEnumerable <Tourney> tourneys) { if (Guard.IsEmptyIEnumerable(tourneys)) { return; } RoundDal dalRounds = null; if (FillProtocols) { FillGames = true; } if (FillGames) { FillRounds = true; } if (FillRounds) { dalRounds = new RoundDal(); dalRounds.FillGames = FillGames; dalRounds.FillProtocols = FillProtocols; dalRounds.SetContext(Context); } if (dalRounds != null) { foreach (Tourney tourney in tourneys) { if (dalRounds != null) { tourney.Round = dalRounds.GetRoundsByTourney(tourney.Id).ToArray(); } } } }
private void FillRelations(IEnumerable <Game> games) { if (Guard.IsEmptyIEnumerable(games)) { return; } IEnumerable <Team> teams = new Team[0]; IEnumerable <Round> rounds = new Round[0]; IEnumerable <Stadium> stadiums = new Stadium[0]; IEnumerable <ProtocolRecord> protocols = new ProtocolRecord[0]; if (FillTourneys) { FillRounds = true; } if (FillTeams) { var teamDal = new TeamDal(); teamDal.SetContext(Context); var teamIds = new List <int>(); teamIds.AddRange(games.Select(g => g.homeId)); teamIds.AddRange(games.Select(g => g.awayId)); teams = teamDal.GetTeams(teamIds.Distinct()).ToList(); if (!teams.Any()) { throw new DalMappingException(nameof(teams), typeof(Game)); } } if (FillRounds) { var roundDal = new RoundDal(); roundDal.SetContext(Context); roundDal.FillTourneys = FillTourneys; var roundIds = new List <int>(); roundIds.AddRange(games.Select(g => (int)g.roundId)); rounds = roundDal.GetRounds(roundIds.Distinct()).ToList(); if (!rounds.Any()) { throw new DalMappingException(nameof(rounds), typeof(Game)); } } if (FillStadiums) { var stadiumDal = new StadiumDal(); stadiumDal.SetContext(Context); stadiumDal.FillCities = true; var stadiumIds = new List <int>(); stadiumIds.AddRange(games.Where(s => s.stadiumId.HasValue).Select(g => (int)g.stadiumId)); stadiums = stadiumDal.GetStadiums(stadiumIds.Distinct()).ToList(); } if (FillProtocols) { var protocolRecordDal = new ProtocolRecordDal(); var gameIds = new List <int>(); gameIds.AddRange(games.Select(g => g.Id).Distinct()); protocols = protocolRecordDal.GetProtocol(gameIds).ToList(); } foreach (Game game in games) { if (FillTeams && teams.Any()) { game.home = teams.FirstOrDefault(t => t.Id == game.homeId); if (game.home == null) { throw new DalMappingException(nameof(game.home), typeof(Game)); } game.away = teams.FirstOrDefault(t => t.Id == game.awayId); if (game.away == null) { throw new DalMappingException(nameof(game.away), typeof(Game)); } } else { game.home = null; game.away = null; } if (FillRounds && rounds.Any()) { game.round = rounds.FirstOrDefault(r => r.Id == game.roundId); if (game.round == null) { throw new DalMappingException(nameof(game.round), typeof(Game)); } } else { game.round = null; } if (FillStadiums && stadiums.Any()) { game.stadium = stadiums.FirstOrDefault(r => r.Id == game.stadiumId); } else { game.stadium = null; } if (FillProtocols && protocols.Any()) { game.ProtocolRecord = protocols.Where(p => p.gameId == game.Id).ToList(); } else { game.ProtocolRecord = null; } } }