Exemplo n.º 1
0
 /// <summary>
 /// 创建 <see cref="RankingPage"/>
 /// </summary>
 /// <param name="currentUserId">当前登录用户 ID</param>
 /// <param name="dbContext"><see cref="KeylolDbContext"/></param>
 /// <param name="cachedData"><see cref="CachedDataProvider"/></param>
 /// <returns><see cref="RankingPage"/></returns>
 public static async Task <RankingPage> CreateAsync(string currentUserId, KeylolDbContext dbContext,
                                                    CachedDataProvider cachedData)
 {
     return(new RankingPage
     {
         RankingUsers = await RankingUserList.CreateAsync(currentUserId, 1, dbContext, cachedData)
     });
 }
Exemplo n.º 2
0
        /// <summary>
        /// 创建 <see cref="RankingUserList"/>
        /// </summary>
        /// <param name="currentUserId">当前登录用户 ID</param>
        /// <param name="page">分页页码</param>
        /// <param name="dbContext"><see cref="KeylolDbContext"/></param>
        /// <param name="cachedData"><see cref="CachedDataProvider"/></param>
        /// <returns><see cref="RankingUserList"/></returns>
        public static async Task <RankingUserList> CreateAsync(string currentUserId, int page,
                                                               KeylolDbContext dbContext, CachedDataProvider cachedData)
        {
            if (page > 7)
            {
                page = 7;
            }
            var queryResult = await dbContext.Users.OrderByDescending(u => u.SeasonLikeCount)
                              .ThenByDescending(u => u.Coupon)
                              .Select(u => new
            {
                u.Id,
                u.IdCode,
                u.UserName,
                u.AvatarImage,
                u.GamerTag,
                u.SeasonLikeCount,
                u.Coupon
            }).Take(100).ToListAsync();

            var actualResult = queryResult.Select((u, i) => new
            {
                Ranking = i,
                User    = u
            }).Skip(RecordsPerPage * (page - 1)).Take(RecordsPerPage).ToList();
            var result = new RankingUserList(actualResult.Count);

            foreach (var g in actualResult)
            {
                result.Add(new RankingUser
                {
                    Ranking       = g.Ranking,
                    UserBasicInfo = new UserBasicInfo
                    {
                        IdCode      = g.User.IdCode,
                        AvatarImage = g.User.AvatarImage,
                        UserName    = g.User.UserName,
                        GamerTag    = g.User.GamerTag,
                        IsFriend    = string.IsNullOrWhiteSpace(currentUserId)
                            ? (bool?)null
                            : await cachedData.Users.IsFriendAsync(currentUserId, g.User.Id)
                    },
                    Coupon          = g.User.Coupon,
                    SeasonLikeCount = g.User.SeasonLikeCount
                });
            }
            return(result);
        }