public async Task <IEnumerable <Post> > GetPostListAsync(PostListInputModel input, CancellationToken cancellationToken = default) { var query = Repository.GetQueryable(); query = BuildPostListInputQuery(query, input); return(await query.ToListAsync(cancellationToken)); }
public Task <IPagedList <Post> > GetPostsPagedListAsync(PostListInputModel input, CancellationToken cancellationToken = default) { var query = Repository.GetQueryable(); query = BuildPostListInputQuery(query, input); // return return(query.ToPagedListAsync(input)); }
protected async Task <IActionResult> GetPostsAsync(GetPostListRequestModel model) { var vm = new PostsViewModel(); var limit = _basicSettings.PageShowCount; if (limit <= 0) { limit = 1; } if (limit > 100) { limit = 100; } var listInputModel = new PostListInputModel() { Skip = 0, Limit = limit, IsDraft = false, IncludeOptions = new PostIncludeOptions() { IncludeCategory = true, IncludeTags = true, IncludeUser = true, } }; listInputModel.CategoryId = model.Category?.Id; listInputModel.TagsId = model.Tags?.Id; listInputModel.UserId = model.Author?.Id; listInputModel.PublishedYearMonth = model.PublishYearMonth; if (model.Page <= 1) { model.Page = 1; } listInputModel.Skip = (model.Page - 1) * listInputModel.Limit; if (!string.IsNullOrWhiteSpace(model.Q)) { listInputModel.SearchTerm = model.Q; } await SetPageTitleAndMetadataAsync(model); var list = await _postService.GetPostsPagedListAsync(listInputModel); vm.Posts = list.ReplaceTo((item) => _postFactory.ToModel(item, new Models.PostModel())); return(View("Posts", vm)); }
private IQueryable <Post> BuildPostListInputQuery(IQueryable <Post> query, PostListInputModel input) { // include options if (input.IncludeOptions?.IncludeUser == true) { query = query.Include(t => t.User); } if (input.IncludeOptions?.IncludeTags == true) { query = query.Include(t => t.Tags).ThenInclude(t => t.Tags); } if (input.IncludeOptions?.IncludeCategory == true) { query = query.Include(t => t.Categories).ThenInclude(c => c.Category); } // where filter options query = query .WhereIf(t => t.Title.Contains(input.SearchTerm), () => !string.IsNullOrWhiteSpace(input.SearchTerm)) .WhereIf(t => t.UserId == input.UserId, () => !string.IsNullOrWhiteSpace(input.UserId)) .WhereIf(t => t.IsDraft == input.IsDraft, () => input.IsDraft.HasValue) .WhereIf(t => t.Categories.Any(c => c.CategoryId == input.CategoryId), () => input.CategoryId.HasValue) .WhereIf(t => t.Tags.Any(c => c.TagsId == input.TagsId), () => input.TagsId.HasValue) .WhereIf(t => t.PublishedTime.Year == input.PublishedYearMonth.Value.Year && t.PublishedTime.Month == input.PublishedYearMonth.Value.Month, () => input.PublishedYearMonth.HasValue) .WhereIf(t => t.PublishedTime.Date == input.PublishedDate, () => input.PublishedDate.HasValue) ; // orders if (input.Orders?.Count > 0) { foreach (var order in input.Orders) { query = query.OrderBy($"{order.Key} {order.Value}"); } } else { query = query.OrderByDescending(t => t.PublishedTime) .ThenByDescending(t => t.CreationTime); } return(query); }