Beispiel #1
0
        private static JsonapiPagination ExtractPagination(IEnumerable <KeyValuePair <string, string> > parameters)
        {
            var hasPageNumberParam = parameters.Any(e => e.Key == PageNumberQueryParam);
            var hasPageSizeParam   = parameters.Any(e => e.Key == PageSizeQueryParam);
            var pageNumber         = 0;
            var pageSize           = DefaultPageSize;

            if (!hasPageNumberParam && !hasPageSizeParam)
            {
                return(null);
            }

            if (hasPageNumberParam && !int.TryParse(parameters.FirstOrDefault(e => e.Key == PageNumberQueryParam).Value, out pageNumber))
            {
                throw new InvalidCastException("Page number must be a positive integer.");
            }

            if (hasPageSizeParam && !int.TryParse(parameters.FirstOrDefault(e => e.Key == PageSizeQueryParam).Value, out pageSize))
            {
                throw new InvalidCastException("Page size must be a positive integer.");
            }

            if (pageNumber < 0)
            {
                throw new NotSupportedException("Page number must not be negative.");
            }

            if (pageSize <= 0)
            {
                throw new NotSupportedException("Page size must be greater than or equal to 1.");
            }


            JsonapiPagination pagination = new JsonapiPagination();

            pagination.PageNumber = pageNumber;
            pagination.PageSize   = pageSize;

            return(pagination);
        }
Beispiel #2
0
        public static IQueryable <T> GeneratePagination <T>(this IQueryable <T> query, JsonapiPagination pagination)
        {
            if (pagination != null)
            {
                if (pagination.PageSize > 100)
                {
                    throw new InvalidCastException("Page size must be less than 100");
                }

                query = query.Skip((pagination.PageNumber - 1) * pagination.PageSize).Take(pagination.PageSize);
            }
            return(query);
        }