예제 #1
0
        public async Task <ActionResult <ApiResultViewModel <AccountStatsSummary> > > GetStatsByAccountId(
            [FromRoute] long accountId,
            CancellationToken cancellationToken)
        {
            var accountStats = await _statsManager.GetByAccountIdAsync(accountId, cancellationToken);

            if (accountStats == null)
            {
                return(OkData(new AccountStatsSummaryViewModel
                {
                    AccountId = accountId
                }));
            }

            accountStats.Rank = await _statsManager.GetRank(accountStats.Score);

            var accountCategoryStats = await _dataContext.AccountCategoryStats.AsNoTracking()
                                       .Include(q => q.Category).Where(q => q.AccountId == accountId)
                                       .GroupBy(x => new { x.CategoryId, x.Category }).Take(5)
                                       .ToListAsync(cancellationToken);

            var accountCategoryStatViewModels = accountCategoryStats.Select(x => new AccountCategoryStatViewModel
            {
                CategoryId          = x.Key.CategoryId,
                CategoryTitle       = x.Key.Category.Title,
                CorrectAnswersCount = x.Sum(a => a.CorrectAnswersCount),
                TotalQuestionsCount = x.Sum(a => a.TotalQuestionsCount)
            });

            return(OkData(AccountStatsSummaryViewModel.Map(accountStats, accountCategoryStatViewModels.ToList())));
        }
예제 #2
0
        public async Task <IActionResult> GetTopRankPlayer(CancellationToken cancellationToken)
        {
            var topPlayer = await _dataContext.AccountStatsSummaries.OrderByDescending(x => x.Score).Take(20)
                            .ToListAsync(cancellationToken);

            var player = topPlayer.Select(p =>
                                          AccountStatsSummaryViewModel.Map(p, _dataContext.Accounts.FirstOrDefault(x => x.Id == p.AccountId))).ToList();

            return(OkData(player));
        }