public async Task <IActionResult> Get( [FromQuery(Name = "id")] int userId, [FromQuery(Name = "offset")] int offset, [FromQuery(Name = "limit")] int limit, [FromQuery(Name = "sort")] int sort, [FromQuery(Name = "tag")] int tagId, [FromQuery(Name = "keyword")] string keyword) { // 排序相关: // 0 -> 不排序,直接返回查询结果(默认) // 1 -> 按照文章的作者ID升序 // 2 -> 按照文章的作者ID降序 // 3 -> 按照文章发表日期升序 // 4 -> 按照的文章发表日期降序 // 5 -> 按照标题升序 // 6 -> 按照标题降序 // 7 -> 按照浏览量升序 // 8 -> 按照浏览量降序 // 9 -> 按照评论量升序 // 10 -> 按照评论量降序 // other -> 不排序,直接返回查询结果(同0) ModelResultList <ArticleInfo> result; if (limit == 0) { limit = 10; } else if (limit < 0) { limit = 0; } IQueryable <Article> articleQuery; if (userId != 0) { articleQuery = _context.Articles .Where(a => a.UserId == userId); } else if (keyword != null) { keyword = keyword.ToLower(); articleQuery = _context.Articles .Where(a => a.Content.Contains(keyword) || a.Title.Contains(keyword)); } else { articleQuery = _context.Articles; } if (tagId != 0) { articleQuery = articleQuery .Where(a => a.ArticleTags.Exists(at => at.TagId == tagId)); } List <ArticleInfo> articleInfos = articleQuery .Select(a => new ArticleInfo(a, _context)).ToList(); int totalCount = articleInfos.Count; if (totalCount == 0) { result = new ModelResultList <ArticleInfo>(404, null, "No Article Exists", false, totalCount, null); return(Ok(result)); } bool hasNext = offset + limit < totalCount; var nextUrl = hasNext ? $@"/api/article?id={userId}&keyword={keyword}&tag={tagId}&offset={limit + offset}&limit={limit}" : null; articleInfos = articleInfos.Select(ai => { ai.CommentCount = _context.Comments.Count(c => c.ArticleId == ai.ArticleId); return(ai); }).ToList(); switch (sort) { case 1: articleInfos.Sort((a1, a2) => a1.UserId - a2.UserId); break; case 2: articleInfos.Sort((a1, a2) => a2.UserId - a1.UserId); break; case 3: articleInfos.Sort((a1, a2) => DateTime.Compare(a1.PublishDate, a2.PublishDate)); break; case 4: articleInfos.Sort((a1, a2) => DateTime.Compare(a2.PublishDate, a1.PublishDate)); break; case 5: articleInfos.Sort((a1, a2) => string.CompareOrdinal(a1.Title, a2.Title)); break; case 6: articleInfos.Sort((a1, a2) => string.CompareOrdinal(a2.Title, a1.Title)); break; case 7: articleInfos.Sort((a1, a2) => a1.ViewNumber - a2.ViewNumber); break; case 8: articleInfos.Sort((a1, a2) => a2.ViewNumber - a1.ViewNumber); break; case 9: articleInfos.Sort((a1, a2) => a1.CommentCount - a2.CommentCount); break; case 10: articleInfos.Sort((a1, a2) => a2.CommentCount - a1.CommentCount); break; } if (offset <= totalCount) { if (offset + limit > totalCount) { limit = totalCount - offset; } articleInfos = articleInfos.GetRange(offset, limit); } else { result = new ModelResultList <ArticleInfo>(400, null, "Index Out of Limit", false, totalCount, nextUrl); return(BadRequest(result)); } if (articleInfos.Count == 0) { result = new ModelResultList <ArticleInfo>(404, null, "No Article Exists", hasNext, totalCount, nextUrl); } else { result = new ModelResultList <ArticleInfo>(200, articleInfos, null, hasNext, totalCount, nextUrl); } return(Ok(result)); }