public static async Task<CourseStatsQueryResponse> CourseStatsQuery(ToracGolfContext dbContext, CourseStatsQueryRequest queryModel, int userId) { var query = dbContext.Rounds.AsNoTracking().Where(x => x.UserId == userId && x.CourseId == queryModel.CourseId); if (queryModel.SeasonId.HasValue) { query = query.Where(x => x.SeasonId == queryModel.SeasonId.Value); } if (queryModel.TeeBoxLocationId.HasValue) { query = query.Where(x => x.CourseTeeLocationId == queryModel.TeeBoxLocationId.Value); } if (query.Count() == 0) { return new CourseStatsQueryResponse { QuickStats = new CondensedStats { TeeBoxCount = dbContext.CourseTeeLocations.Count(y => y.CourseId == queryModel.CourseId) } }; } //group by should chunk it up to 1 record var model = await query.GroupBy(x => x.UserId).Select(x => new CourseStatsQueryResponse { QuickStats = new CondensedStats { RoundCount = x.Count(), AverageScore = x.Average(y => y.Score), BestScore = x.Min(y => y.Score), TeeBoxCount = dbContext.CourseTeeLocations.Count(y => y.CourseId == queryModel.CourseId) } }).FirstAsync().ConfigureAwait(false); model.RecentRounds = await RecentCourseStatRounds(dbContext, userId, queryModel.CourseId, queryModel.SeasonId, queryModel.TeeBoxLocationId).ConfigureAwait(false); model.ScoreGraphData = await ScoreGraph(dbContext, userId, queryModel.CourseId, queryModel.SeasonId, queryModel.TeeBoxLocationId).ConfigureAwait(false); model.PuttsGraphData = await PuttGraph(dbContext, userId, queryModel.CourseId, queryModel.SeasonId, queryModel.TeeBoxLocationId).ConfigureAwait(false); model.FairwaysGraphData = await FairwaysHitGraph(dbContext, userId, queryModel.CourseId, queryModel.SeasonId, queryModel.TeeBoxLocationId).ConfigureAwait(false); model.GIRGraphData = await GIRGraph(dbContext, userId, queryModel.CourseId, queryModel.SeasonId, queryModel.TeeBoxLocationId).ConfigureAwait(false); return model; }
public async Task<IActionResult> CourseStatQueryRecords(CourseStatsQueryRequest queryModel) { return Json(await CourseDataProvider.CourseStatsQuery(DbContext, queryModel, GetUserId())); }