public override void OnFormatting(ActionContext context) { base.OnFormatting(context); NexusatAspNetCoreOptions options = GetAspNetCoreOptions(context.HttpContext); Navigation?.SetLinks(options, context.HttpContext); }
/// <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; } }
private static void ParsePageCursor(NexusatAspNetCoreOptions options, IQueryCollection query, out int?pageSize, out int?pageIndex) { StringValues values; pageSize = pageIndex = null; int _size, _index; if (query.TryGetValue(options.PaginationPageSizeName, out values)) { if (!int.TryParse(values[0], out _size)) { var ex = new BadRequestResponseException("KO_BAD_PAGE_SIZE"); ex.Description = "Page Size is not a valid integer"; throw ex; } if (_size < 0) { var ex = new BadRequestResponseException("KO_BAD_PAGE_SIZE"); ex.Description = "Page Size must be a non negative integer"; throw ex; } pageSize = _size; } if (query.TryGetValue(options.PaginationPageIndexName, out values)) { if (!int.TryParse(values[0], out _index)) { var ex = new BadRequestResponseException("KO_BAD_PAGE_INDEX"); ex.Description = "Page Index is not a valid integer"; throw ex; } if (_index < 1) { var ex = new BadRequestResponseException("KO_BAD_PAGE_INDEX"); ex.Description = "Page Index must be a positive integer"; throw ex; } pageIndex = _index; } }