Exemple #1
0
        public async Task <IEnumerable <Post> > GetPostListAsync(PostListInputModel input, CancellationToken cancellationToken = default)
        {
            var query = Repository.GetQueryable();

            query = BuildPostListInputQuery(query, input);

            return(await query.ToListAsync(cancellationToken));
        }
Exemple #2
0
        public Task <IPagedList <Post> > GetPostsPagedListAsync(PostListInputModel input, CancellationToken cancellationToken = default)
        {
            var query = Repository.GetQueryable();

            query = BuildPostListInputQuery(query, input);

            // return
            return(query.ToPagedListAsync(input));
        }
Exemple #3
0
        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));
        }
Exemple #4
0
        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);
        }