/// <summary> /// 按照发布顺序获取帖子列表 /// </summary> /// <param name="pager"></param> /// <returns></returns> public async Task <Posts_Result> GetPostsAsync(Paginator pager) { using ATXDbContext db = new ATXDbContext(); pager.TotalRows = await db.Posts.CountAsync(p => p.State == (int)ShareEnum.State.Enabled); List <Posts_Result.PostItem> list = await db.Posts.AsNoTracking() .Where(p => p.State == (int)ShareEnum.State.Enabled) .Include(p => p.File) .Skip(pager.GetSkip()) .Take(pager.Rows) .OrderByDescending(p => p.CreateDate) .Select(o => new Posts_Result.PostItem { Id = o.Id, Author = o.Author, Description = o.Description, SourceFilePath = o.File.Path, ThumbnailPath = o.File.Thumbnail, PublishDateTime = o.CreateDate.ToString("yyyy-MM-dd"), Likes = o.Likes }) .ToListAsync(); Posts_Result result = new Posts_Result { List = list }; return(result); }
public async Task <Posts_Result> GetPostsAsync(Paginator pager) { /* * 随机获取帖子 * 分页获取 * index: 当前页码 * rows: 每页行数 */ using ATXDbContext db = new ATXDbContext(); pager.TotalRows = await db.Posts.CountAsync(p => p.State == (int)ShareEnum.State.Enabled); List <Posts_Result.PostItem> list = await db.Posts .FromSqlRaw($"SELECT * FROM POSTS WHERE STATE = {(int)ShareEnum.State.Enabled} ORDER BY RAND() LIMIT {pager.Rows}") .Select(o => new Posts_Result.PostItem { Id = o.Id, Author = o.Author, Description = o.Description, Type = o.File.Type, SourceFilePath = o.File.Path, ThumbnailPath = o.File.Thumbnail, PublishDateTime = o.CreateDate.ToString("yyyy-MM-dd"), Likes = o.Likes }) .ToListAsync(); //List<Posts_Result.PostItem> list = await db.Posts.AsNoTracking() // .Where(p => p.State == (int)ShareEnum.State.Enabled) // .OrderBy(p => Guid.NewGuid()) // .Take(pager.Rows) // .Select(o => new Posts_Result.PostItem // { // Id = o.Id, // Author = o.Author, // Description = o.Description, // Type = o.File.Type, // SourceFilePath = o.File.Path, // ThumbnailPath = o.File.Thumbnail, // PublishDateTime = o.CreateDate.ToString("yyyy-MM-dd"), // Likes = o.Likes // }) // .ToListAsync(); Posts_Result result = new Posts_Result { List = list }; return(result); }
public async Task <ActionResult> GetPostListAsync(int index, int rows, int order) { Paginator pager = new Paginator { Index = index, Rows = rows }; if (index <= 0 || rows <= 0) { pager.List = new List <Posts_Result.PostItem>(); return(BadRequest(Result("参数有误, 请重试", pager))); } Domain.Posts.PostsService postsService = new Domain.Posts.PostsService(); Posts_Result result = await postsService.GetPostsAsync(pager, order); pager.List = result.List; return(Ok(Result("", pager))); }