89 lines
2.7 KiB
C#
89 lines
2.7 KiB
C#
using System;
|
|
using Microsoft.AspNetCore.Routing;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
using Newtonsoft.Json;
|
|
|
|
namespace AyaNova.Api.ControllerHelpers
|
|
{
|
|
|
|
public class PaginationLinkBuilder
|
|
{ //adapted from //https://www.jerriepelser.com/blog/paging-in-aspnet-webapi-pagination-links/
|
|
public Uri FirstPage { get; private set; }
|
|
public Uri LastPage { get; private set; }
|
|
public Uri NextPage { get; private set; }
|
|
public Uri PreviousPage { get; private set; }
|
|
public ListOptions PagingOptions { get; }
|
|
public long TotalRecordCount { get; }
|
|
|
|
public PaginationLinkBuilder(IUrlHelper urlHelper, string routeName, object routeValues, ListOptions pagingOptions, long totalRecordCount)
|
|
{
|
|
PagingOptions = pagingOptions;
|
|
TotalRecordCount = totalRecordCount;
|
|
|
|
// Determine total number of pages
|
|
var pageCount = totalRecordCount > 0
|
|
? (int)Math.Ceiling(totalRecordCount / (double)pagingOptions.Limit)
|
|
: 0;
|
|
|
|
// Create page links
|
|
|
|
FirstPage = new Uri(urlHelper.Link(routeName, new RouteValueDictionary(routeValues)
|
|
{
|
|
{"pageNo", 1},
|
|
{"pageSize", pagingOptions.Limit}
|
|
}));
|
|
|
|
|
|
LastPage = new Uri(urlHelper.Link(routeName, new RouteValueDictionary(routeValues)
|
|
{
|
|
{"pageNo", pageCount},
|
|
{"pageSize", pagingOptions.Limit}
|
|
}));
|
|
|
|
if (pagingOptions.Offset > 1)
|
|
{
|
|
PreviousPage = new Uri(urlHelper.Link(routeName, new RouteValueDictionary(routeValues)
|
|
{
|
|
{"pageNo", pagingOptions.Offset - 1},
|
|
{"pageSize", pagingOptions.Limit}
|
|
}));
|
|
}
|
|
|
|
|
|
|
|
if (pagingOptions.Offset < pageCount)
|
|
{
|
|
NextPage = new Uri(urlHelper.Link(routeName, new RouteValueDictionary(routeValues)
|
|
{
|
|
{"pageNo", pagingOptions.Offset + 1},
|
|
{"pageSize", pagingOptions.Limit}
|
|
}));
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
/// Return paging data suitable for API return
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
public Object PagingLinksObject()
|
|
{
|
|
return new
|
|
{
|
|
Count = TotalRecordCount,
|
|
Offset = PagingOptions.Offset,
|
|
Limit = PagingOptions.Limit,
|
|
First = FirstPage,
|
|
Previous = PreviousPage,
|
|
Next = NextPage,
|
|
Last = LastPage
|
|
};
|
|
}
|
|
|
|
|
|
}
|
|
|
|
} |