/// <summary> /// 创建 <see cref="DiscoveryPage"/> /// </summary> /// <param name="currentUserId">当前登录用户 ID</param> /// <param name="dbContext"><see cref="KeylolDbContext"/></param> /// <param name="cachedData"><see cref="CachedDataProvider"/></param> /// <returns><see cref="DiscoveryPage"/></returns> public static async Task <DiscoveryPage> CreateAsync(string currentUserId, KeylolDbContext dbContext, CachedDataProvider cachedData) { var onSalePoints = await OnSalePointList.CreateAsync(currentUserId, 1, true, true, dbContext, cachedData); var latestArticles = await LatestArticleList.CreateAsync(1, true, true, dbContext, cachedData); return(new DiscoveryPage { SlideshowEntries = await SlideshowEntryList.CreateAsync(1, 4, dbContext), SpotlightPoints = await SpotlightPointList.CreateAsync(currentUserId, 1, 30, dbContext, cachedData), SpotlightReviews = await SpotlightArticleList.CreateAsync(currentUserId, 1, 4, SpotlightArticleStream.ArticleCategory.Review, dbContext, cachedData), SpotlightStudies = await SpotlightArticleList.CreateAsync(currentUserId, 1, 4, SpotlightArticleStream.ArticleCategory.Study, dbContext, cachedData), OnSalePointHeaderImage = onSalePoints.Item3, OnSalePointPageCount = onSalePoints.Item2, OnSalePoints = onSalePoints.Item1, SpotlightStories = await SpotlightArticleList.CreateAsync(currentUserId, 1, 4, SpotlightArticleStream.ArticleCategory.Story, dbContext, cachedData), LatestArticleHeaderImage = latestArticles.Item3, LatestArticlePageCount = latestArticles.Item2, LatestArticles = latestArticles.Item1, LatestActivityHeaderImage = onSalePoints.Item4, LatestActivities = await TimelineCardList.CreateAsync(LatestActivityStream.Name, currentUserId, 12, false, dbContext, cachedData) }); }
/// <summary> /// 创建 <see cref="LatestArticleList"/> /// </summary> /// <param name="page">分页页码</param> /// <param name="returnPageCount">是否返回总页数</param> /// <param name="returnFirstCoverImage">是否返回第一篇文章封面图</param> /// <param name="dbContext"><see cref="KeylolDbContext"/></param> /// <param name="cachedData"><see cref="CachedDataProvider"/></param> /// <returns>Item1 表示 <see cref="LatestArticleList"/>,Item2 表示总页数,Item3 表示第一篇文章封面图</returns> public static async Task <Tuple <LatestArticleList, int, string> > CreateAsync(int page, bool returnPageCount, bool returnFirstCoverImage, KeylolDbContext dbContext, CachedDataProvider cachedData) { var conditionQuery = from article in dbContext.Articles where article.Rejected == false && article.Archived == ArchivedState.None orderby article.Sid descending select article; var queryResult = await conditionQuery.Select(a => new { Count = returnPageCount ? conditionQuery.Count() : 1, CoverImage = returnFirstCoverImage ? a.CoverImage : null, a.Id, a.SidForAuthor, a.Title, a.PublishTime, AuthorIdCode = a.Author.IdCode, AuthorAvatarImage = a.Author.AvatarImage, AuthorUserName = a.Author.UserName, PointIdCode = a.TargetPoint.IdCode, PointType = a.TargetPoint.Type, PointAvatarImage = a.TargetPoint.AvatarImage, PointChineseName = a.TargetPoint.ChineseName, PointEnglishName = a.TargetPoint.EnglishName }).TakePage(page, RecordsPerPage).ToListAsync(); var result = new LatestArticleList(queryResult.Count); foreach (var a in queryResult) { result.Add(new LatestArticle { LikeCount = await cachedData.Likes.GetTargetLikeCountAsync(a.Id, LikeTargetType.Article), CommentCount = await cachedData.ArticleComments.GetArticleCommentCountAsync(a.Id), SidForAuthor = a.SidForAuthor, Title = a.Title, PublishTime = a.PublishTime, AuthorIdCode = a.AuthorIdCode, AuthorAvatarImage = a.AuthorAvatarImage, AuthorUserName = a.AuthorUserName, PointIdCode = a.PointIdCode, PointAvatarImage = a.PointAvatarImage, PointType = a.PointType, PointChineseName = a.PointChineseName, PointEnglishName = a.PointEnglishName }); } var firstRecord = queryResult.FirstOrDefault(r => !string.IsNullOrWhiteSpace(r.CoverImage)); return(new Tuple <LatestArticleList, int, string>( result, (int)Math.Ceiling(firstRecord?.Count / (double)RecordsPerPage ?? 1), firstRecord?.CoverImage)); }