Exemple #1
0
 /// <summary>
 /// 创建 <see cref="ProductPage"/>
 /// </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="ProductPage"/></returns>
 public static async Task <ProductPage> CreateAsync(Models.Point point, string currentUserId,
                                                    KeylolDbContext dbContext, CachedDataProvider cachedData)
 {
     return(new ProductPage
     {
         Products = await ProductPointList.CreateAsync(currentUserId, point.Id, dbContext, cachedData)
     });
 }
        /// <summary>
        /// 创建满足指定据点关系的 <see cref="ProductPointList"/>
        /// </summary>
        /// <param name="currentUserId">当前登录用户 ID</param>
        /// <param name="pointId">据点 ID</param>
        /// <param name="relationshipType">据点身份</param>
        /// <param name="take">获取数量,如果为 null 表示获取全部</param>
        /// <param name="dbContext"><see cref="KeylolDbContext"/></param>
        /// <param name="cachedData"><see cref="CachedDataProvider"/></param>
        /// <returns>Item1 表示 <see cref="ProductPointList"/>,Item2 表示总据点个数</returns>
        public static async Task <Tuple <ProductPointList, int> > CreateAsync(string currentUserId, string pointId,
                                                                              PointRelationshipType relationshipType, int?take, KeylolDbContext dbContext, CachedDataProvider cachedData)
        {
            var conditionQuery = from relationship in dbContext.PointRelationships
                                 where relationship.TargetPointId == pointId && relationship.Relationship == relationshipType
                                 select relationship;
            var query = conditionQuery.OrderByDescending(r => r.Sid).Select(r => new
            {
                Count = conditionQuery.Count(),
                r.SourcePoint.Id,
                r.SourcePoint.IdCode,
                r.SourcePoint.AvatarImage,
                r.SourcePoint.ChineseName,
                r.SourcePoint.EnglishName,
                r.SourcePoint.TitleCoverImage,
                r.SourcePoint.SteamAppId
            });

            if (take != null)
            {
                query = query.Take(() => take.Value);
            }
            var queryResult = await query.ToListAsync();

            var result = new ProductPointList(queryResult.Count);

            foreach (var p in queryResult)
            {
                result.Add(new ProductPoint
                {
                    Id              = p.Id,
                    IdCode          = p.IdCode,
                    AvatarImage     = p.AvatarImage,
                    ChineseName     = p.ChineseName,
                    EnglishName     = p.EnglishName,
                    TitleCoverImage = p.TitleCoverImage,
                    AverageRating   = (await cachedData.Points.GetRatingsAsync(p.Id)).AverageRating,
                    Subscribed      = string.IsNullOrWhiteSpace(currentUserId)
                        ? (bool?)null
                        : await cachedData.Subscriptions.IsSubscribedAsync(currentUserId, p.Id,
                                                                           SubscriptionTargetType.Point),
                    InLibrary = string.IsNullOrWhiteSpace(currentUserId) || p.SteamAppId == null
                        ? (bool?)null
                        : await cachedData.Users.IsSteamAppInLibraryAsync(currentUserId, p.SteamAppId.Value)
                });
            }
            var firstRecord = queryResult.FirstOrDefault();

            return(new Tuple <ProductPointList, int>(result, firstRecord?.Count ?? 0));
        }
        /// <summary>
        /// 创建 <see cref="ProductPointList"/>
        /// </summary>
        /// <param name="currentUserId">当前登录用户 ID</param>
        /// <param name="pointId">据点 ID</param>
        /// <param name="dbContext"><see cref="KeylolDbContext"/></param>
        /// <param name="cachedData"><see cref="CachedDataProvider"/></param>
        /// <returns><see cref="ProductPointList"/></returns>
        public static async Task <ProductPointList> CreateAsync(string currentUserId, string pointId,
                                                                KeylolDbContext dbContext, CachedDataProvider cachedData)
        {
            var queryResult = await(from relationship in dbContext.PointRelationships
                                    where relationship.TargetPointId == pointId
                                    group relationship.Relationship by new
            {
                relationship.SourcePoint.Sid,
                relationship.SourcePoint.Id,
                relationship.SourcePoint.IdCode,
                relationship.SourcePoint.AvatarImage,
                relationship.SourcePoint.ChineseName,
                relationship.SourcePoint.EnglishName,
                relationship.SourcePoint.TitleCoverImage,
                relationship.SourcePoint.SteamAppId
            }
                                    into g
                                    orderby g.Key.Sid descending
                                    select g)
                              .ToListAsync();

            var result = new ProductPointList(queryResult.Count);

            foreach (var g in queryResult)
            {
                result.Add(new ProductPoint
                {
                    Id              = g.Key.Id,
                    IdCode          = g.Key.IdCode,
                    AvatarImage     = g.Key.AvatarImage,
                    ChineseName     = g.Key.ChineseName,
                    EnglishName     = g.Key.EnglishName,
                    TitleCoverImage = g.Key.TitleCoverImage,
                    AverageRating   = (await cachedData.Points.GetRatingsAsync(g.Key.Id)).AverageRating,
                    Subscribed      = string.IsNullOrWhiteSpace(currentUserId)
                        ? (bool?)null
                        : await cachedData.Subscriptions.IsSubscribedAsync(currentUserId, g.Key.Id,
                                                                           SubscriptionTargetType.Point),
                    InLibrary = string.IsNullOrWhiteSpace(currentUserId) || g.Key.SteamAppId == null
                        ? (bool?)null
                        : await cachedData.Users.IsSteamAppInLibraryAsync(currentUserId, g.Key.SteamAppId.Value),
                    Roles = g.ToList()
                });
            }
            return(result);
        }