Exemplo n.º 1
0
        /// <summary>
        /// 创建 <see cref="BriefReviewList"/>
        /// </summary>
        /// <param name="point">据点对象</param>
        /// <param name="currentUserId">当前登录用户 ID</param>
        /// <param name="page">分页页码</param>
        /// <param name="returnCount">是否返回总数</param>
        /// <param name="dbContext"><see cref="KeylolDbContext"/></param>
        /// <param name="cachedData"><see cref="CachedDataProvider"/></param>
        /// <returns>Item1 表示 <see cref="BriefReviewList"/>,Item2 表示总数,Item3 表示总页数</returns>
        public static async Task <Tuple <BriefReviewList, int, int> > CreateAsync(Models.Point point, string currentUserId,
                                                                                  int page, bool returnCount, KeylolDbContext dbContext, CachedDataProvider cachedData)
        {
            var queryResult = await(from activity in dbContext.Activities
                                    where activity.TargetPointId == point.Id && activity.Rating != null &&
                                    activity.Archived == ArchivedState.None && activity.Rejected == false
                                    orderby dbContext.Likes
                                    .Count(l => l.TargetId == activity.Id && l.TargetType == LikeTargetType.Activity) descending
                                    select new
            {
                Count = returnCount
                        ? dbContext.Activities.Count(
                    a => a.TargetPointId == point.Id && a.Rating != null && a.Archived == ArchivedState.None)
                        : 1,
                activity.Id,
                activity.AuthorId,
                AuthorIdCode      = activity.Author.IdCode,
                AuthorAvatarImage = activity.Author.AvatarImage,
                AuthorUserName    = activity.Author.UserName,
                activity.SidForAuthor,
                activity.Rating,
                activity.Content
            }).TakePage(page, RecordsPerPage).ToListAsync();

            var result = new BriefReviewList(queryResult.Count);

            foreach (var a in queryResult)
            {
                result.Add(new BriefReview
                {
                    Id                = a.Id,
                    AuthorIdCode      = a.AuthorIdCode,
                    AuthorAvatarImage = a.AuthorAvatarImage,
                    AuthorUserName    = a.AuthorUserName,
                    AuthorPlayedTime  = point.SteamAppId == null
                        ? null
                        : (await dbContext.UserSteamGameRecords
                           .Where(r => r.UserId == a.AuthorId && r.SteamAppId == point.SteamAppId)
                           .SingleOrDefaultAsync())?.TotalPlayedTime,
                    SidForAuthor = a.SidForAuthor,
                    Rating       = a.Rating,
                    LikeCount    = await cachedData.Likes.GetTargetLikeCountAsync(a.Id, LikeTargetType.Activity),
                    Liked        = string.IsNullOrWhiteSpace(currentUserId)
                        ? (bool?)null
                        : await cachedData.Likes.IsLikedAsync(currentUserId, a.Id, LikeTargetType.Activity),
                    Content = a.Content
                });
            }
            var firstRecord = queryResult.FirstOrDefault();

            return(new Tuple <BriefReviewList, int, int>(result,
                                                         firstRecord?.Count ?? 0,
                                                         (int)Math.Ceiling(firstRecord?.Count / (double)RecordsPerPage ?? 1)));
        }
Exemplo n.º 2
0
        /// <summary>
        /// 创建 <see cref="FrontpagePage"/>
        /// </summary>
        /// <param name="point">已经查询好的据点对象</param>
        /// <param name="currentUserId">当前登录用户 ID</param>
        /// <param name="dbContext"><see cref="KeylolDbContext"/></param>
        /// <param name="cachedData"><see cref="CachedDataProvider"/></param>
        /// <returns><see cref="FrontpagePage"/></returns>
        public static async Task <FrontpagePage> CreateAsync(Models.Point point, string currentUserId,
                                                             KeylolDbContext dbContext, CachedDataProvider cachedData)
        {
            var frontPage = new FrontpagePage();

            if (point.Type == PointType.Game || point.Type == PointType.Hardware)
            {
                var briefReviews =
                    await BriefReviewList.CreateAsync(point, currentUserId, 1, true, dbContext, cachedData);

                frontPage.BriefReviewCount     = briefReviews.Item2;
                frontPage.BriefReviewPageCount = briefReviews.Item3;
                frontPage.BriefReviews         = briefReviews.Item1;
                frontPage.MediaHeaderImage     = point.MediaHeaderImage;
                frontPage.Media         = Helpers.SafeDeserialize <List <PointMedia> >(point.Media);
                frontPage.SimilarPoints =
                    await SimilarPointList.CreateAsync(point.Id, currentUserId, 1, dbContext, cachedData);

                frontPage.SelectedArticles =
                    await SelectedArticleList.CreateAsync(point.Id, 1, 4, currentUserId, dbContext, cachedData);
            }

            if (point.Type == PointType.Game)
            {
                frontPage.Platforms = await(from relationship in dbContext.PointRelationships
                                            where relationship.SourcePointId == point.Id &&
                                            relationship.Relationship == PointRelationshipType.Platform
                                            orderby relationship.Sid descending
                                            select relationship.TargetPoint.IdCode)
                                      .ToListAsync();

                #region 特性属性

                frontPage.MultiPlayer         = point.MultiPlayer ? true : (bool?)null;
                frontPage.SinglePlayer        = point.SinglePlayer ? true : (bool?)null;
                frontPage.Coop                = point.Coop ? true : (bool?)null;
                frontPage.CaptionsAvailable   = point.CaptionsAvailable ? true : (bool?)null;
                frontPage.CommentaryAvailable = point.CommentaryAvailable ? true : (bool?)null;
                frontPage.IncludeLevelEditor  = point.IncludeLevelEditor ? true : (bool?)null;
                frontPage.Achievements        = point.Achievements ? true : (bool?)null;
                frontPage.Cloud               = point.Cloud ? true : (bool?)null;
                frontPage.LocalCoop           = point.LocalCoop ? true : (bool?)null;
                frontPage.SteamTradingCards   = point.SteamTradingCards ? true : (bool?)null;
                frontPage.SteamWorkshop       = point.SteamWorkshop ? true : (bool?)null;
                frontPage.InAppPurchases      = point.InAppPurchases ? true : (bool?)null;

                #endregion

                frontPage.ChineseAvailability = Helpers.SafeDeserialize <ChineseAvailability>(point.ChineseAvailability);

                if (point.SteamAppId != null)
                {
                    frontPage.AddictedUsers =
                        await AddictedUserList.CreateAsync(currentUserId, point.SteamAppId, 1, dbContext, cachedData);
                }
            }
            else if (point.Type == PointType.Vendor)
            {
                var developerProducts = await ProductPointList.CreateAsync(currentUserId, point.Id,
                                                                           PointRelationshipType.Developer, 3, dbContext, cachedData);

                frontPage.DeveloperProducts     = developerProducts.Item1;
                frontPage.DeveloperProductCount = developerProducts.Item2;

                var publisherProducts = await ProductPointList.CreateAsync(currentUserId, point.Id,
                                                                           PointRelationshipType.Publisher, 3, dbContext, cachedData);

                frontPage.PublisherProducts     = publisherProducts.Item1;
                frontPage.PublisherProductCount = publisherProducts.Item2;

                var resellerProducts = await ProductPointList.CreateAsync(currentUserId, point.Id,
                                                                          PointRelationshipType.Reseller, 3, dbContext, cachedData);

                frontPage.ResellerProducts     = resellerProducts.Item1;
                frontPage.ResellerProductCount = resellerProducts.Item2;

                var manufacturerProducts = await ProductPointList.CreateAsync(currentUserId, point.Id,
                                                                              PointRelationshipType.Manufacturer, 3, dbContext, cachedData);

                frontPage.ManufacturerProducts     = manufacturerProducts.Item1;
                frontPage.ManufacturerProductCount = manufacturerProducts.Item2;
            }
            else if (point.Type == PointType.Category)
            {
                var tagProducts = await ProductPointList.CreateAsync(currentUserId, point.Id,
                                                                     PointRelationshipType.Tag, 3, dbContext, cachedData);

                frontPage.TagProducts     = tagProducts.Item1;
                frontPage.TagProductCount = tagProducts.Item2;

                var seriesProducts = await ProductPointList.CreateAsync(currentUserId, point.Id,
                                                                        PointRelationshipType.Series, 3, dbContext, cachedData);

                frontPage.SeriesProducts     = seriesProducts.Item1;
                frontPage.SeriesProductCount = seriesProducts.Item2;

                var genreProducts = await ProductPointList.CreateAsync(currentUserId, point.Id,
                                                                       PointRelationshipType.Genre, 3, dbContext, cachedData);

                frontPage.GenreProducts     = genreProducts.Item1;
                frontPage.GenreProductCount = genreProducts.Item2;
            }
            else if (point.Type == PointType.Platform)
            {
                var platformProducts = await ProductPointList.CreateAsync(currentUserId, point.Id,
                                                                          PointRelationshipType.Platform, 3, dbContext, cachedData);

                frontPage.PlatformProducts = platformProducts.Item1;
            }

            var latestArticles = await LatestArticleList.CreateAsync(point.Id, 1, true, true, dbContext, cachedData);

            frontPage.LatestArticleHeaderImage = latestArticles.Item3;
            frontPage.LatestArticlePageCount   = latestArticles.Item2;
            frontPage.LatestArticles           = latestArticles.Item1;

            return(frontPage);
        }