public static async Task <CollectionApiResponse <T> > GetPageAsync <T>(this IQueryable <T> source, int targetPage, int size,
                                                                               CancellationToken cancellationToken)
        {
            if (source == null)
            {
                throw new ArgumentNullException(nameof(source));
            }
            if (targetPage < 1)
            {
                throw new ArgumentOutOfRangeException(nameof(targetPage), "Target page can't be less than 1");
            }
            if (size <= 0)
            {
                throw new ArgumentOutOfRangeException(nameof(size), "Size of page can't be less than 0");
            }

            var page = new CollectionApiResponse <T>();

            page.CurrentPage  = targetPage;
            page.OverallCount = await source.CountAsync(cancellationToken : cancellationToken).ConfigureAwait(false);

            page.PageSize = size;
            page.Content  = await source.Skip((targetPage - 1) *size).Take(size)
                            .ToListAsync(cancellationToken: cancellationToken).ConfigureAwait(false);

            page.PageCount = (int)Math.Ceiling((decimal)page.OverallCount / size);

            return(page);
        }
Beispiel #2
0
        private static CollectionApiResponse <RecipeShortModel> GetCollectionApiResponse(params RecipeShortModel[] result)
        {
            CollectionApiResponse <RecipeShortModel> collectionApiResponse =
                new CollectionApiResponse <RecipeShortModel>
            {
                Content      = result,
                CurrentPage  = 1,
                OverallCount = result.Length,
                PageCount    = 1,
                PageSize     = result.Length
            };

            return(collectionApiResponse);
        }