Exemplo n.º 1
0
 /// <summary>
 /// 创建 <see cref="PointResultList"/>
 /// </summary>
 /// <param name="currentUserId">当前登录用户 ID</param>
 /// <param name="keyword">搜索关键字</param>
 /// <param name="dbContext"><see cref="KeylolDbContext"/></param>
 /// <param name="cachedData"><see cref="CachedDataProvider"/></param>
 /// <param name="searchAll">是否全部查询</param>
 public static async Task <PointPage> CreateAsync(string currentUserId, string keyword,
                                                  [Injected] KeylolDbContext dbContext, [Injected] CachedDataProvider cachedData, bool searchAll = true)
 {
     return(new PointPage
     {
         Results = await PointResultList.CreateAsync(currentUserId, keyword, dbContext, cachedData, 1, searchAll)
     });
 }
Exemplo n.º 2
0
        /// <summary>
        /// 创建 <see cref="PointResultList"/>
        /// </summary>
        /// <param name="currentUserId">当前用户 ID</param>
        /// <param name="keyword">搜索关键字</param>
        /// <param name="dbContext"><see cref="KeylolDbContext"/></param>
        /// <param name="cachedData"><see cref="CachedDataProvider"/></param>
        /// <param name="page">分页页码</param>
        /// <param name="searchAll">是否全部查询</param>
        public static async Task <PointResultList> CreateAsync(string currentUserId, string keyword,
                                                               [Injected] KeylolDbContext dbContext, [Injected] CachedDataProvider cachedData, int page,
                                                               bool searchAll = true)
        {
            var take = searchAll ? 10 : 5;
            var skip = (page - 1) * take;

            keyword = keyword.Replace('"', ' ').Replace('*', ' ').Replace('\'', ' ');
            var searchResult = await dbContext.Database.SqlQuery <PointResult>(@"SELECT
                        *,
                        (SELECT
                            COUNT(1)
                        FROM Articles
                        WHERE TargetPointId = t4.Id)
                        AS ArticleCount,
                        (SELECT
                            COUNT(1)
                        FROM Activities
                        WHERE TargetPointId = t4.Id)
                        AS ActivityCount
                    FROM (SELECT
                        *
                    FROM [dbo].[Points] AS [t1]
                    INNER JOIN (SELECT
                        [t2].[KEY],
                        MAX([t2].[RANK]) AS RANK
                    FROM (SELECT
                        *
                    FROM CONTAINSTABLE([dbo].[Points], ([EnglishName], [EnglishAliases]), {0})
                    UNION ALL
                    SELECT
                        *
                    FROM CONTAINSTABLE([dbo].[Points], ([ChineseName], [ChineseAliases]), {0})) AS [t2]
                    GROUP BY [t2].[KEY]) AS [t3]
                        ON [t1].[Sid] = [t3].[KEY]) AS [t4]
                    ORDER BY [t4].[RANK] DESC, [ArticleCount] DESC OFFSET {1} ROWS FETCH NEXT {2} ROWS ONLY",
                                                                               $"\"{keyword}\" OR \"{keyword}*\"", skip, take).ToListAsync();

            var result = new PointResultList(searchResult.Count);

            foreach (var p in searchResult)
            {
                result.Add(new PointResult
                {
                    Id              = searchAll ? p.Id : null,
                    IdCode          = p.IdCode,
                    ChineseName     = p.ChineseName,
                    EnglishName     = p.EnglishName,
                    AvatarImage     = p.AvatarImage,
                    SubscriberCount = searchAll
                        ? await cachedData.Subscriptions.GetSubscriberCountAsync(p.Id, SubscriptionTargetType.Point)
                        : (long?)null,
                    ArticleCount  = searchAll ? p.ArticleCount : null,
                    ActivityCount = searchAll ? p.ActivityCount : null,
                    Subscribed    = searchAll && !string.IsNullOrWhiteSpace(currentUserId)
                        ? await cachedData.Subscriptions.IsSubscribedAsync(currentUserId, p.Id,
                                                                           SubscriptionTargetType.Point)
                        : (bool?)null,
                    InLibrary = p.SteamAppId == null || string.IsNullOrWhiteSpace(currentUserId)
                        ? (bool?)null
                        : await cachedData.Users.IsSteamAppInLibraryAsync(currentUserId, p.SteamAppId.Value)
                });
            }
            return(result);
        }