コード例 #1
0
        public async Task OnGet(int?id = 1)
        {
            Player player = new Player();

            // Todo: Leverage the navigation properties better?  Instead of pulling from table like lines 46 & 48?
            var allPlayersInfo = await _context.Players
                                 .Include(p => p.GolfClubs)
                                 .Include(p => p.PlayerPracticeSessionAssignments)
                                 .ThenInclude(p => p.PracticeSession)
                                 .ThenInclude(p => p.GolfShots).ToListAsync();

            if (id != null)
            {
                PlayerID   = id.Value;
                player     = allPlayersInfo.Single(i => i.ID == PlayerID);
                PlayerName = player.FullName;
                // Todo: Include all below in 'if' or early exit back to page?

                var golfClubs = _context.GolfClubs.Where(p => p.PlayerID == PlayerID);
                var playerPracticeSessions     = player.PlayerPracticeSessionAssignments.Select(s => s.PracticeSession).ToList();
                var practiceSessionAssignments = _context.PlayerPracticeSessionAssignments.Where(p => p.PlayerID == PlayerID);

                var practiceSessions = playerPracticeSessions
                                       .Join(practiceSessionAssignments, ps => ps.ID, psa => psa.PlayerPracticeSessionAssignmentID, (ps, psa) => new { ps, psa })
                                       .Join(golfClubs, pspsa => pspsa.psa.GolfClubID, gc => gc.ID, (pspsa, gc) => new { pspsa, gc })
                                       .Select(x => new PracticeSummaryVM()
                {
                    ClubName     = x.gc.Name,
                    PracticeDate = x.pspsa.ps.PracticeDate,
                    //Altitude = x.pspsa.ps.Altitude,
                    GolfShots = x.pspsa.ps.GolfShots
                }).ToList();

                PracticeSummaryList = new List <PracticeSummaryVM>();
                foreach (var practiceSession in practiceSessions)
                {
                    var practiceSummary = new PracticeSummaryVM();
                    practiceSummary.ClubName = practiceSession.ClubName;
                    //practiceSummary.Altitude = practiceSession.Altitude;
                    practiceSummary.PracticeDate   = practiceSession.PracticeDate;
                    practiceSummary.CarryAveYds    = CalcAverageYards(practiceSession.GolfShots.Select(gs => gs.CarryYards));
                    practiceSummary.TotalAveYds    = CalcAverageYards(practiceSession.GolfShots.Select(gs => gs.TotalYards));
                    practiceSummary.CarryMedianYds = CalcMedianYards(practiceSession.GolfShots.Select(gs => gs.CarryYards));
                    practiceSummary.TotalMedianYds = CalcMedianYards(practiceSession.GolfShots.Select(gs => gs.TotalYards));
                    practiceSummary.CarryModeYds   = CalcModeYards(practiceSession.GolfShots.Select(gs => gs.CarryYards));
                    practiceSummary.TotalModeYds   = CalcModeYards(practiceSession.GolfShots.Select(gs => gs.TotalYards));
                    practiceSummary.OfflineLeft    = CalcAverageYards(practiceSession.GolfShots
                                                                      .Where(gs => gs.OfflineYards <= 0).Select(gs => gs.OfflineYards));
                    practiceSummary.OfflineRight = CalcAverageYards(practiceSession.GolfShots
                                                                    .Where(gs => gs.OfflineYards >= 0).Select(gs => gs.OfflineYards));
                    practiceSummary.NumberOfShots = practiceSession.GolfShots.Count;

                    PracticeSummaryList.Add(practiceSummary);
                }
            }
        }
 public void Initialize()
 {
     _repositoryMock  = new Mock <IRepository>();
     _objectUnderTest = new PracticeSummaryVM(_repositoryMock.Object);
 }
コード例 #3
0
        public async Task OnGetAsync(int?id = 1)
        {
            var allPlayersInfo = await _context.Players
                                 .Include(p => p.GolfClubs)
                                 .Include(p => p.PlayerPracticeSessionAssignments)
                                 .ThenInclude(p => p.PracticeSession)
                                 .ThenInclude(p => p.GolfShots).ToListAsync();

            if (id != null)
            {
                PlayerID = id.Value;
                Player player = _context.Players.Single(p => p.ID == PlayerID);
                PlayerName = player.FullName;
                // Todo: Include all below in 'if' or early exit back to page?

                var golfClubs = _context.GolfClubs.Where(gc => gc.PlayerID == PlayerID && gc.InBag)
                                .OrderBy(so => so.SortOrder);
                var playerPracticeSessions =
                    player.PlayerPracticeSessionAssignments.Select(s => s.PracticeSession).ToList();
                var practiceSessionAssignments =
                    _context.PlayerPracticeSessionAssignments.Where(p => p.PlayerID == PlayerID);

                // Get a list of all practice sessions and statistics for player
                var practicesWithShotAggregates = playerPracticeSessions
                                                  .Join(practiceSessionAssignments, ps => ps.ID, psa => psa.PlayerPracticeSessionAssignmentID,
                                                        (ps, psa) => new { ps, psa })
                                                  .Join(golfClubs, pspsa => pspsa.psa.GolfClubID, gc => gc.ID, (pspsa, gc) => new { pspsa, gc })
                                                  .Select(x => new PracticeSummaryVM()
                {
                    ClubName           = x.gc.Name,
                    PracticeDate       = x.pspsa.ps.PracticeDate,
                    GolfShots          = x.pspsa.ps.GolfShots,
                    CarryAveYds        = x.pspsa.ps.GolfShots.Sum(gs => gs.CarryYards),
                    TotalAveYds        = x.pspsa.ps.GolfShots.Sum(gs => gs.TotalYards),
                    NumberOfShotsLeft  = x.pspsa.ps.GolfShots.Where(gs => gs.OfflineYards <= 0).Select(gs => gs.OfflineYards).Count(),
                    NumberOfShotsRight = x.pspsa.ps.GolfShots.Where(gs => gs.OfflineYards >= 0).Select(gs => gs.OfflineYards).Count(),
                    OfflineLeft        = x.pspsa.ps.GolfShots.Where(gs => gs.OfflineYards <= 0).Select(gs => gs.OfflineYards).Sum(),
                    OfflineRight       = x.pspsa.ps.GolfShots.Where(gs => gs.OfflineYards >= 0).Select(gs => gs.OfflineYards).Sum(),
                    NumberOfShots      = x.pspsa.ps.GolfShots.Count
                }).ToList();

                // Todo: write this in method syntax and write all other LINQ queries in sql syntax for practice
                // Flatten the golf shots for each practice session into a single value and then get average
                var clubsWithAverages = from practiceAggregates in practicesWithShotAggregates
                                        group practiceAggregates by practiceAggregates.ClubName
                                        into groupedClubs
                                        select new PracticeSummaryVM()
                {
                    ClubName           = groupedClubs.Key,
                    NumberOfShots      = groupedClubs.Sum(shot => shot.NumberOfShots),
                    CarryAveYds        = groupedClubs.Sum(shot => shot.CarryAveYds) / groupedClubs.Sum(shot => shot.NumberOfShots),
                    TotalAveYds        = groupedClubs.Sum(shot => shot.TotalAveYds) / groupedClubs.Sum(shot => shot.NumberOfShots),
                    OfflineLeft        = groupedClubs.Sum(shot => shot.OfflineLeft) / groupedClubs.Sum(shot => shot.NumberOfShots),
                    OfflineRight       = groupedClubs.Sum(shot => shot.OfflineRight) / groupedClubs.Sum(shot => shot.NumberOfShots),
                    NumberOfShotsLeft  = groupedClubs.Sum(shot => shot.NumberOfShotsLeft),
                    NumberOfShotsRight = groupedClubs.Sum(shot => shot.NumberOfShotsRight),
                };

                // Build the view model to display the averages for each (active) club in the bag
                ClubAverages = new List <PracticeSummaryVM>();
                foreach (var golfClub in golfClubs)
                {
                    var club = new PracticeSummaryVM();
                    if (clubsWithAverages.Any(c => c.ClubName == golfClub.Name))
                    {
                        ClubAverages.Add(clubsWithAverages.FirstOrDefault(c => c.ClubName == golfClub.Name));
                    }
                    else
                    {
                        club.ClubName = golfClub.Name;
                        ClubAverages.Add(club);
                    }
                }
            }
        }