示例#1
0
        /// <inheritdoc />
        public virtual void Read(string parameterName, StringValues parameterValue)
        {
            try
            {
                PaginationQueryStringValueExpression constraint = GetPageConstraint(parameterValue);

                if (constraint.Elements.Any(element => element.Scope == null))
                {
                    AssertIsCollectionRequest();
                }

                if (parameterName == PageSizeParameterName)
                {
                    ValidatePageSize(constraint);
                    _pageSizeConstraint = constraint;
                }
                else
                {
                    ValidatePageNumber(constraint);
                    _pageNumberConstraint = constraint;
                }
            }
            catch (QueryParseException exception)
            {
                throw new InvalidQueryStringParameterException(parameterName, "The specified paging is invalid.", exception.Message, exception);
            }
        }
示例#2
0
        protected virtual void ValidatePageNumber(PaginationQueryStringValueExpression constraint)
        {
            if (_options.MaximumPageNumber != null && constraint.Elements.Any(element => element.Value > _options.MaximumPageNumber.OneBasedValue))
            {
                throw new QueryParseException($"Page number cannot be higher than {_options.MaximumPageNumber}.");
            }

            if (constraint.Elements.Any(element => element.Value < 1))
            {
                throw new QueryParseException("Page number cannot be negative or zero.");
            }
        }
        public PaginationQueryStringValueExpression Parse(string source, ResourceContext resourceContextInScope)
        {
            ArgumentGuard.NotNull(resourceContextInScope, nameof(resourceContextInScope));

            _resourceContextInScope = resourceContextInScope;

            Tokenize(source);

            PaginationQueryStringValueExpression expression = ParsePagination();

            AssertTokenStackIsEmpty();

            return(expression);
        }
示例#4
0
        private IList <PaginationElementQueryStringValueExpression> ParsePageSizeExpression(string pageSizeParameterValue)
        {
            if (pageSizeParameterValue == null)
            {
                return(new List <PaginationElementQueryStringValueExpression>());
            }

            ResourceContext requestResource = _request.SecondaryResource ?? _request.PrimaryResource;

            var parser = new PaginationParser(_provider);
            PaginationQueryStringValueExpression paginationExpression = parser.Parse(pageSizeParameterValue, requestResource);

            return(paginationExpression.Elements.ToList());
        }
示例#5
0
        protected virtual void ValidatePageSize(PaginationQueryStringValueExpression constraint)
        {
            if (_options.MaximumPageSize != null)
            {
                if (constraint.Elements.Any(element => element.Value > _options.MaximumPageSize.Value))
                {
                    throw new QueryParseException($"Page size cannot be higher than {_options.MaximumPageSize}.");
                }

                if (constraint.Elements.Any(element => element.Value == 0))
                {
                    throw new QueryParseException("Page size cannot be unconstrained.");
                }
            }

            if (constraint.Elements.Any(element => element.Value < 0))
            {
                throw new QueryParseException("Page size cannot be negative.");
            }
        }