Beispiel #1
0
        public static Uri Link(
            this IUrlHelper urlHelper,
            string routeName,
            int pageNumber,
            PagedQueryParams pagedQueryParams)
        {
            if (urlHelper is null)
            {
                throw new ArgumentNullException(nameof(urlHelper));
            }

            if (string.IsNullOrEmpty(routeName))
            {
                throw new ArgumentException("Expected non-null/empty route name", nameof(routeName));
            }

            if (pagedQueryParams is null)
            {
                throw new ArgumentNullException(nameof(pagedQueryParams));
            }

            if (!(pagedQueryParams.GetType()
                  .GetProperty(nameof(PagedQueryParams.PageNumber), BindingFlags.Public | BindingFlags.Instance)
                  ?.GetCustomAttribute(typeof(FromQueryAttribute)) is FromQueryAttribute pageNumberParam))
            {
                throw new InvalidOperationException($"Expected the property '{nameof(PagedQueryParams.PageNumber)}' to have an attribute of type '{nameof(FromQueryAttribute)}'.");
            }

            var routeValues = pagedQueryParams.ToRouteValuesDictionary();

            routeValues[pageNumberParam.Name] = pageNumber;

            return(new Uri(urlHelper.Link(routeName, routeValues)));
        }
        public static IActionResult OkWithPageHeader <T>(
            this ControllerBase controller,
            IPagedCollection <T> value,
            string routeName,
            PagedQueryParams queryParams,
            IUrlHelper urlHelper)
        {
            if (controller is null)
            {
                throw new ArgumentNullException(nameof(controller));
            }

            if (value is null)
            {
                throw new ArgumentNullException(nameof(value));
            }

            if (string.IsNullOrEmpty(routeName))
            {
                throw new ArgumentException("Expected non-null/empty route name", nameof(routeName));
            }

            if (queryParams is null)
            {
                throw new ArgumentNullException(nameof(queryParams));
            }

            if (urlHelper is null)
            {
                throw new ArgumentNullException(nameof(urlHelper));
            }

            controller.Response.Headers.AddPaginationHeader(value, routeName, queryParams, urlHelper);
            return(controller.Ok(value.ToList()));
        }
Beispiel #3
0
        public static Uri LinkFirstPage(
            this IUrlHelper urlHelper,
            string routeName,
            int pageSize,
            PagedQueryParams pagedQueryParams)
        {
            if (pageSize < 1)
            {
                return(null);
            }

            return(urlHelper.Link(routeName, 1, pagedQueryParams));
        }
Beispiel #4
0
        public static Uri LinkCurrentPage(
            this IUrlHelper urlHelper,
            string routeName,
            int pageSize,
            int currentPageNumber,
            PagedQueryParams pagedQueryParams)
        {
            if (currentPageNumber < 1 || pageSize < 1)
            {
                return(null);
            }

            return(urlHelper.Link(routeName, currentPageNumber, pagedQueryParams));
        }
Beispiel #5
0
        public static Uri LinkPreviousPage(
            this IUrlHelper urlHelper,
            string routeName,
            int pageSize,
            int?previousPageNumber,
            PagedQueryParams pagedQueryParams)
        {
            if (!previousPageNumber.HasValue || previousPageNumber < 1 || pageSize < 1)
            {
                return(null);
            }

            return(urlHelper.Link(routeName, previousPageNumber.Value, pagedQueryParams));
        }
Beispiel #6
0
        public static PaginationHeader ToPaginationHeader <T>(
            this IPagedCollection <T> pagedCollection,
            string routeName,
            PagedQueryParams queryParams,
            IUrlHelper urlHelper
            )
        {
            if (pagedCollection is null)
            {
                throw new ArgumentNullException(nameof(pagedCollection));
            }

            if (string.IsNullOrEmpty(routeName))
            {
                throw new ArgumentException("message", nameof(routeName));
            }

            if (queryParams is null)
            {
                throw new ArgumentNullException(nameof(queryParams));
            }

            if (urlHelper is null)
            {
                throw new ArgumentNullException(nameof(urlHelper));
            }

            return(new PaginationHeader(
                       pagedCollection.CurrentPageNumber,
                       pagedCollection.PageSize,
                       pagedCollection.PageCount,
                       pagedCollection.ItemCount,
                       urlHelper.LinkNextPage(routeName, pagedCollection.PageSize, pagedCollection.NextPageNumber, queryParams),
                       urlHelper.LinkPreviousPage(routeName, pagedCollection.PageSize, pagedCollection.PreviousPageNumber, queryParams),
                       urlHelper.LinkFirstPage(routeName, pagedCollection.PageSize, queryParams),
                       urlHelper.LinkLastPage(routeName, pagedCollection.PageSize, pagedCollection.LastPageNumber, queryParams),
                       urlHelper.LinkCurrentPage(routeName, pagedCollection.PageSize, pagedCollection.CurrentPageNumber, queryParams)));
        }
Beispiel #7
0
        public ResourceBase AddCurrentPage <T>(string routeName, IPagedCollection <T> items, PagedQueryParams query)
        {
            if (items is null)
            {
                throw new ArgumentNullException(nameof(items));
            }

            var link = _urlHelper.LinkCurrentPage(routeName, items.PageSize, items.CurrentPageNumber, query);

            _links.Add(new ResourceLink(link?.ToString() ?? string.Empty, "current-page", HttpMethod.Get.ToString()));
            return(this);
        }