This commit is contained in:
89
server/AyaNova/ControllerHelpers/PaginationLinkBuilder.cs
Normal file
89
server/AyaNova/ControllerHelpers/PaginationLinkBuilder.cs
Normal file
@@ -0,0 +1,89 @@
|
||||
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 PagingOptions PagingOptions { get; }
|
||||
public long TotalRecordCount { get; }
|
||||
|
||||
public PaginationLinkBuilder(IUrlHelper urlHelper, string routeName, object routeValues, PagingOptions 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
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user