Exemple #1
0
        public async Task <Pageable <Story> > GetAllByTypeAsync(StoryClientRequest request)
        {
            var ids = await GetIdsByTypeAsync(request.type);

            var total         = ids.Count();
            var requestedPage = request.page;

            /*Do not load all if there is no search*/
            if (string.IsNullOrEmpty(request.search))
            {
                ids = ids.Page(request.page, request.pageSize);
            }
            /*load the id details in separate threads*/
            IEnumerable <Story> stories = (await Task.WhenAll(ids.Select(GetByIdAsync)));

            stories = stories.Where(x => x?.link != null && x?.title != null);
            /*no filter ? easy return pageable*/
            if (string.IsNullOrEmpty(request.search))
            {
                Pageable <Story> pageable = new Pageable <Story>()
                {
                    filtered   = total,
                    totalCount = total,
                    pageSize   = request.pageSize,
                    page       = requestedPage,
                    content    = stories
                };
                pageable.Calculate();
                return(pageable);
            }
            /*if we filter then call the pager*/
            return(stories.Page(request));
        }
        public static Pageable <Story> Page(this IEnumerable <Story> source, IPageRequest request)
        {
            Pageable <Story> pageable = new Pageable <Story>
            {
                totalCount = source.Count(),
                pageSize   = request.pageSize,
                page       = request.page
            };

            if (!string.IsNullOrWhiteSpace(request.search))
            {
                pageable.content = source.Where(x => x?.title?.IndexOf(request.search.Trim(), StringComparison.OrdinalIgnoreCase) > -1);
            }
            else
            {
                pageable.content = source;
            }
            pageable.filtered = (pageable.content?.Count()).GetValueOrDefault();
            pageable.content  = pageable.content?.Skip(request.page * request.pageSize).Take(request.pageSize);
            pageable.Calculate();
            return(pageable);
        }