/// <summary> /// Sets the pagination links. /// </summary> /// <param name="options">Application Global settings.</param> /// <param name="httpContext">The Http Context of the request</param> internal void SetLinks(NexusatAspNetCoreOptions options, HttpContext context) { string requestUrl = context.Request.GetEncodedPathAndQuery(); // Only one of the following condition can hold /// <see cref="Builders.ApiEnumResponseBuilder{T}"/> Debug.Assert(ItemsCount.HasValue ^ HasNextPage); PageSize = PaginationCursor.PageSize; if (PaginationCursor.IsPageSizeBounded) // If unbounded links are not generated { Links = new PaginationLinks(); var linkBuilder = new PaginationInfoLinkBuilder( context.Request.GetEncodedPathAndQuery(), options.PaginationPageIndexName, options.PaginationPageSizeName, PaginationCursor.PageSize ); // First Links.First = linkBuilder.GetLink(1); // Current Links.Current = linkBuilder.GetLink(PaginationCursor.PageIndex); // Previous if (PaginationCursor.PageIndex > 1) { Links.Previous = linkBuilder.GetLink(PaginationCursor.PageIndex - 1); } if (ItemsCount.HasValue) { // calculate links and pageCount int pageCount = (ItemsCount.Value + PaginationCursor.PageSize - 1) / PaginationCursor.PageSize; PagesCount = pageCount; if (pageCount > PaginationCursor.PageIndex) { // Next Links.Next = linkBuilder.GetLink(PaginationCursor.PageIndex + 1); } // Last Links.Last = linkBuilder.GetLink(pageCount); } else if (HasNextPage) // implies hasn't itemsCount { // calculate links except the "last" Links.Next = linkBuilder.GetLink(PaginationCursor.PageIndex + 1); } } else // Unbounded requests have only 1 page of data { PagesCount = 1; } }
public void TestUrlLinks(string originalLink, int pageIndex, int pageSize) { // Setup var linkBuilder = new PaginationInfoLinkBuilder(originalLink, pageIndexKeyName, pageSizeKeyName, pageSize); Output.WriteLine("Original URL: {0}", originalLink); // Act var actual = linkBuilder.GetLink(pageIndex); Output.WriteLine("Altered URL: {0}", actual); // Check Assert.Contains("?", actual); Assert.Contains("&", actual); Assert.DoesNotContain("&?", actual); Assert.DoesNotContain("?&", actual); Assert.DoesNotContain("&&", actual); Assert.DoesNotContain("??", actual); Assert.True(CheckPageIndex(actual, pageIndex)); Assert.True(CheckPageSize(actual, pageSize)); }