-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPaginator.cs
190 lines (162 loc) · 7.42 KB
/
Paginator.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore;
namespace Paginator.EntityFrameworkCore
{
/// <summary>
/// Static class containing pagination methods.
/// </summary>
public static class Paginator
{
internal const int DEF_PAGE = 1;
internal const int DEF_PERPAGE = 10;
internal const bool DEF_SKIPCOUNT = false;
/// <summary>
/// Asynchronously lists items in the pagination request.
/// </summary>
/// <typeparam name="TEntity"></typeparam>
/// <param name="query"></param>
/// <param name="page">Page</param>
/// <param name="perpage">Items per page.</param>
/// <param name="cancellationToken">Cancellation token.</param>
/// <returns>A task that represents the asynchronous operation which you can await.</returns>
/// <exception cref="ArgumentException"/>
/// <exception cref="ArgumentNullException"/>
/// <exception cref="OperationCanceledException"/>
/// <exception cref="ObjectDisposedException"/>
public static Task<PagedResult<TEntity>> PaginateAsync<TEntity>(this IQueryable<TEntity> query, int page = DEF_PAGE, int perpage = DEF_PERPAGE, CancellationToken cancellationToken = default)
where TEntity : class
=> query.ProcessPaginationAsync(page, perpage, DEF_SKIPCOUNT, cancellationToken);
/// <summary>
/// Asynchronously lists items in the pagination request.
/// </summary>
/// <typeparam name="TEntity"></typeparam>
/// <param name="query"></param>
/// <param name="page">Page</param>
/// <param name="perpage">Items per page.</param>
/// <param name="skipCount">Specify whether to omit running a count operation on your query againt the data store.</param>
/// <param name="cancellationToken">Cancellation token.</param>
/// <returns>A task that represents the asynchronous operation which you can await.</returns>
/// <exception cref="ArgumentException"/>
/// <exception cref="ArgumentNullException"/>
/// <exception cref="OperationCanceledException"/>
/// <exception cref="ObjectDisposedException"/>
public static Task<PagedResult<TEntity>> PaginateAsync<TEntity>(this IQueryable<TEntity> query,
int page, int perpage, bool skipCount, CancellationToken cancellationToken = default)
where TEntity : class
=> query.ProcessPaginationAsync(page, perpage, skipCount, cancellationToken);
/// <summary>
/// Lists items in the sequence and only returns the specified number.
/// </summary>
/// <typeparam name="TEntity"></typeparam>
/// <param name="query"></param>
/// <param name="page">Page</param>
/// <param name="perpage">Items per page.</param>
/// <param name="skipCount">Specify whether to omit running a count operation on your query againt the data store.</param>
/// <returns>A <see cref="PagedResult{TEntity}"/> response object.</returns>
/// <exception cref="ArgumentException"/>
/// <exception cref="ArgumentNullException"/>
public static PagedResult<TEntity> Paginate<TEntity>(this IQueryable<TEntity> query,
int page = DEF_PAGE, int perpage = DEF_PERPAGE, bool skipCount = DEF_SKIPCOUNT)
where TEntity : class
=> query.ProcessPagination(page, perpage, skipCount);
internal static async Task<PagedResult<TEntity>> ProcessPaginationAsync<TEntity>(this IQueryable<TEntity> query,
int page = DEF_PAGE, int perpage = DEF_PERPAGE, bool skipCount = DEF_SKIPCOUNT, CancellationToken cancellationToken = default)
{
ValidateParams_IfInvalid_Throw(page, perpage);
int total = 0;
var list = new List<TEntity>();
cancellationToken.ThrowIfCancellationRequested();
if (!skipCount)
total = await query.CountEntitiesAsync(cancellationToken);
if (skipCount || (!skipCount && total > 0))
list = await query.Skip((page - 1) * perpage).Take(perpage).ToListAsync(cancellationToken);
if (skipCount)
total = list.Count;
var paged = new PagedResult<TEntity>()
{
Page = page,
ItemsPerPage = perpage,
TotalItems = total,
TotalPages = CalculateTotalPages(total, perpage),
Items = list
};
if (skipCount)
paged = NonDeterministicEstimation(paged);
return paged;
}
internal static PagedResult<TEntity> ProcessPagination<TEntity>(this IQueryable<TEntity> query,
int page = DEF_PAGE, int perpage = DEF_PERPAGE, bool skipCount = DEF_SKIPCOUNT)
{
ValidateParams_IfInvalid_Throw(page, perpage);
int total = 0;
var list = new List<TEntity>();
if (!skipCount)
total = query.Count();
if (skipCount || (!skipCount && total > 0))
list = query.Skip((page - 1) * perpage).Take(perpage).ToList();
if (skipCount)
total = list.Count;
var paged = new PagedResult<TEntity>()
{
Page = page,
ItemsPerPage = perpage,
TotalItems = total,
TotalPages = CalculateTotalPages(total, perpage),
Items = list
};
if (skipCount)
paged = NonDeterministicEstimation(paged);
return paged;
}
internal static Task<int> CountEntitiesAsync<TEntity>(this IQueryable<TEntity> query, CancellationToken cancellationToken = default)
{
return query.CountAsync(cancellationToken);
}
internal static int CalculateTotalPages(int totalItems, int perpage)
{
int ans = 0;
if (perpage >= 1)
{
ans = totalItems / perpage;
ans += (totalItems % perpage) > 0 ? 1 : 0;
}
return ans;
}
internal static PagedResult<TEntity> NonDeterministicEstimation<TEntity>(PagedResult<TEntity> paged)
{
if (paged.Page == 1)
{
if (paged.Items.Count == paged.ItemsPerPage)
{
paged.TotalPages = paged.Page + 1;
}
}
else
{
if (paged.Items.Count == paged.ItemsPerPage)
{
paged.TotalPages = paged.Page + 1;
paged.TotalItems = paged.Page * paged.ItemsPerPage;
}
else
{
paged.TotalPages = paged.Page;
paged.TotalItems = ((paged.Page - 1) * paged.ItemsPerPage) + paged.Items.Count;
}
}
return paged;
}
internal static void ValidateParams_IfInvalid_Throw(int page, int perpage)
{
if (page <= 0)
throw new ArgumentException("Page parameter must be greater than zero.", nameof(page));
if (perpage < 0)
throw new ArgumentException("Per-page parameter must be 0 or greater than 0.", nameof(perpage));
return;
}
}
}