예제 #1
0
        /// <summary>
        /// 创建 <see cref="DossierPage"/>
        /// </summary>
        /// <param name="user">用户对象</param>
        /// <param name="currentUserId">当前登录用户 ID</param>
        /// <param name="dbContext"><see cref="KeylolDbContext"/></param>
        /// <param name="cachedData"><see cref="CachedDataProvider"/></param>
        /// <param name="userManager"><see cref="KeylolUserManager"/></param>
        /// <returns><see cref="DossierPage"/></returns>
        public static async Task <DossierPage> CreateAsync(KeylolUser user, string currentUserId,
                                                           KeylolDbContext dbContext, CachedDataProvider cachedData, KeylolUserManager userManager)
        {
            var subscribedPoints =
                await SubscribedPointList.CreateAsync(currentUserId, user.Id, 1, 3, true, dbContext, cachedData);

            var selectedArticles =
                await SelectedArticleList.CreateAsync(user.Id, 1, 8, true, currentUserId, dbContext, cachedData);

            var dossierPage = new DossierPage
            {
                Coupon          = user.Coupon,
                LikeCount       = await cachedData.Likes.GetUserLikeCountAsync(user.Id),
                GameCount       = await dbContext.UserSteamGameRecords.CountAsync(r => r.UserId == user.Id),
                PlayedGameCount =
                    await dbContext.UserSteamGameRecords.CountAsync(r => r.UserId == user.Id && r.TotalPlayedTime > 0),
                SpotlightCount       = await dbContext.Articles.CountAsync(a => a.AuthorId == user.Id && a.Spotlighted),
                IsOperator           = await userManager.IsInRoleAsync(user.Id, KeylolRoles.Operator),
                SubscribedPointCount = subscribedPoints.Item2,
                SubscribedPoints     = subscribedPoints.Item1,
                ArticleCount         = selectedArticles.Item2,
                SelectedArticles     = selectedArticles.Item1
            };

            return(dossierPage);
        }
예제 #2
0
        /// <summary>
        /// 创建 <see cref="SelectedArticleList"/>
        /// </summary>
        /// <param name="userId">用户 ID</param>
        /// <param name="page">分页页码</param>
        /// <param name="recordsPerPage">每页数量</param>
        /// <param name="returnCount">是否返回总数</param>
        /// <param name="currentUserId">当前登录用户 ID</param>
        /// <param name="dbContext"><see cref="KeylolDbContext"/></param>
        /// <param name="cachedData"><see cref="CachedDataProvider"/></param>
        /// <returns>Item1 表示 <see cref="SelectedArticleList"/>,Item2 表示总数</returns>
        public static async Task <Tuple <SelectedArticleList, int> > CreateAsync(string userId, int page,
                                                                                 int recordsPerPage, bool returnCount, string currentUserId, KeylolDbContext dbContext,
                                                                                 CachedDataProvider cachedData)
        {
            var queryResult = await(from article in dbContext.Articles
                                    where article.AuthorId == userId && article.Archived == ArchivedState.None
                                    orderby dbContext.Likes
                                    .Count(l => l.TargetId == article.Id && l.TargetType == LikeTargetType.Article) descending
                                    select new
            {
                Count = returnCount ? dbContext.Articles.Count(a => a.AuthorId == userId) : 1,
                article.SidForAuthor,
                article.Title,
                article.Subtitle,
                article.CoverImage,
                PointIdCode      = article.TargetPoint.IdCode,
                PointAvatarImage = article.TargetPoint.AvatarImage,
                PointType        = article.TargetPoint.Type,
                PointChineseName = article.TargetPoint.ChineseName,
                PointEnglishName = article.TargetPoint.EnglishName,
                PointSteamAppId  = article.TargetPoint.SteamAppId
            }).TakePage(page, recordsPerPage).ToListAsync();

            var result = new SelectedArticleList(queryResult.Count);

            foreach (var a in queryResult)
            {
                result.Add(new SelectedArticle
                {
                    SidForAuthor     = a.SidForAuthor,
                    Title            = a.Title,
                    Subtitle         = a.Subtitle,
                    CoverImage       = a.CoverImage,
                    PointIdCode      = a.PointIdCode,
                    PointAvatarImage = a.PointAvatarImage,
                    PointType        = a.PointType,
                    PointChineseName = a.PointChineseName,
                    PointEnglishName = a.PointEnglishName,
                    PointInLibrary   = string.IsNullOrWhiteSpace(currentUserId) || a.PointSteamAppId == null
                        ? (bool?)null
                        : await cachedData.Users.IsSteamAppInLibraryAsync(currentUserId, a.PointSteamAppId.Value)
                });
            }

            var firstRecord = queryResult.FirstOrDefault();

            return(new Tuple <SelectedArticleList, int>(result, firstRecord?.Count ?? 0));
        }