Exemple #1
0
        public async Task <ReadOnlyCollection <TeamGameResult> > GetGamesAsync(int teamId,
                                                                               int seasonId)
        {
            List <TeamGameResult> games = new List <TeamGameResult>();

            using (var context = new DatabaseContext())
            {
                List <InternalModel.StoredProcedureResults.TeamGameResult> databaseGames = await context.Database.SqlQuery <InternalModel.StoredProcedureResults.TeamGameResult>(
                    "dbo.api_GetTeamGames_asc @teamId, @seasonId",
                    new SqlParameter("teamId", teamId),
                    new SqlParameter("seasonId", seasonId)).ToListAsync();

                List <int> teamIds = GetGameIds(databaseGames);
                List <InternalModel.Team> teams = await context.Teams.Where(t => teamIds.Contains(t.TeamId)).ToListAsync();

                InternalModel.Season season = await context.Seasons.FirstOrDefaultAsync(s => s.SeasonId == seasonId);

                List <int>      divisionIds = databaseGames.Select(d => d.DivisionId).Distinct().ToList();
                List <Division> divisions   =
                    await context.Divisions.Where(d => divisionIds.Contains(d.DivisionId)).ToListAsync();

                List <int>   venueIds = databaseGames.Select(v => v.VenueId).Distinct().ToList();
                List <Venue> venues   = await context.Venues.Where(v => venueIds.Contains(v.VenueId)).ToListAsync();

                foreach (InternalModel.StoredProcedureResults.TeamGameResult result in databaseGames)
                {
                    Division division = divisions.FirstOrDefault(d => d.DivisionId == result.DivisionId);
                    Venue    venue    = venues.FirstOrDefault(v => v.VenueId == result.VenueId);

                    TeamGameResult game = new TeamGameResult(
                        result.ShortName,
                        result.GameId,
                        result.HomeTeamId,
                        Team.Convert(teams.SingleOrDefault(t => t.TeamId == result.HomeTeamId)),
                        result.AwayTeamId,
                        Team.Convert(teams.SingleOrDefault(t => t.TeamId == result.AwayTeamId)),
                        result.HomeTeamHtScore,
                        result.AwayTeamHtScore,
                        result.HomeTeamScore,
                        result.AwayTeamScore,
                        result.GameDate,
                        ExternalModel.Venue.Convert(venue),
                        ExternalModel.Division.Convert(division),
                        result.DDate,
                        result.TTime,
                        result.GameStatus,
                        result.Comp,
                        result.CompCupPoId,
                        result.GameTypeId,
                        result.SeasonId,
                        result.SeasonName,
                        Season.Convert(season),
                        result.HomeTeamName,
                        result.AwayTeamName);

                    games.Add(game);
                }
            }

            return(new ReadOnlyCollection <TeamGameResult>(games));
        }