/// <summary> /// 创建 <see cref="UserPage"/> /// </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> /// <returns></returns> public static async Task <UserPage> CreateAsync(string currentUserId, string keyword, [Injected] KeylolDbContext dbContext, [Injected] CachedDataProvider cachedData, bool searchAll = true) { return(new UserPage { Results = await UserResultList.CreateAsync(currentUserId, keyword, dbContext, cachedData, 1, searchAll) }); }
/// <summary> /// 创建 <see cref="UserResultList"/> /// </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 <UserResultList> 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 queryResult = await dbContext.Database.SqlQuery <UserResult>(@"SELECT *, (SELECT COUNT(1) FROM Articles WHERE AuthorId = [t3].[Id]) AS ArticleCount, (SELECT COUNT(1) FROM Activities WHERE AuthorId = [t3].[Id]) AS ActivityCount FROM (SELECT * FROM [dbo].[KeylolUsers] AS [t1] INNER JOIN (SELECT * FROM CONTAINSTABLE([dbo].[KeylolUsers], ([UserName]), {0})) AS [t2] ON [t1].[Sid] = [t2].[KEY]) AS [t3] ORDER BY [t3].[RANK] DESC, [ArticleCount] DESC OFFSET {1} ROWS FETCH NEXT {2} ROWS ONLY", $"\"{keyword}\" OR \"{keyword}*\"", skip, take).ToListAsync(); var result = new UserResultList(queryResult.Count); foreach (var u in queryResult) { result.Add(new UserResult { Id = searchAll ? u.Id : null, UserName = u.UserName, GamerTag = u.GamerTag, IdCode = u.IdCode, AvatarImage = u.AvatarImage, ArticleCount = u.ArticleCount, ActivityCount = u.ActivityCount, LikeCount = await cachedData.Likes.GetUserLikeCountAsync(u.Id), Subscribed = string.IsNullOrWhiteSpace(currentUserId) ? (bool?)null : await cachedData.Subscriptions.IsSubscribedAsync(currentUserId, u.Id, SubscriptionTargetType.User), IsFriend = string.IsNullOrWhiteSpace(currentUserId) ? (bool?)null : await cachedData.Users.IsFriendAsync(currentUserId, u.Id) }); } return(result); }