@@ -26,6 +26,8 @@ public static class Paginator
26
26
/// <param name="skipCount">Specify whether to omit running a count operation on your query againt the data store.</param>
27
27
/// <param name="token">Cancellation token.</param>
28
28
/// <returns>A task that represents the asynchronous operation which you can await.</returns>
29
+ /// <exception cref="ArgumentException"/>
30
+ /// <exception cref="ArgumentNullException"/>
29
31
/// <exception cref="OperationCanceledException"/>
30
32
public static Task < PagedResult < TEntity > > PaginateAsync < TEntity > ( this IQueryable < TEntity > query ,
31
33
int page = DEF_PAGE , int perpage = DEF_PERPAGE , bool skipCount = DEF_SKIPCOUNT , CancellationToken token = default )
@@ -40,7 +42,8 @@ public static Task<PagedResult<TEntity>> PaginateAsync<TEntity>(this IQueryable<
40
42
/// <param name="perpage">Items per page.</param>
41
43
/// <param name="skipCount">Specify whether to omit running a count operation on your query againt the data store.</param>
42
44
/// <returns>A <see cref="PagedResult{TEntity}"/> response object.</returns>
43
- /// <exception cref="OperationCanceledException"/>
45
+ /// <exception cref="ArgumentException"/>
46
+ /// <exception cref="ArgumentNullException"/>
44
47
public static PagedResult < TEntity > Paginate < TEntity > ( this IQueryable < TEntity > query ,
45
48
int page = DEF_PAGE , int perpage = DEF_PERPAGE , bool skipCount = DEF_SKIPCOUNT )
46
49
where TEntity : class
@@ -49,6 +52,8 @@ public static PagedResult<TEntity> Paginate<TEntity>(this IQueryable<TEntity> qu
49
52
internal static async Task < PagedResult < TEntity > > ProcessPaginationAsync < TEntity > ( this IQueryable < TEntity > query ,
50
53
int page = DEF_PAGE , int perpage = DEF_PERPAGE , bool skipCount = DEF_SKIPCOUNT , CancellationToken token = default )
51
54
{
55
+ ValidateParams_IfInvalid_Throw ( page , perpage ) ;
56
+
52
57
int total = 0 ;
53
58
var list = new List < TEntity > ( ) ;
54
59
@@ -75,6 +80,8 @@ internal static async Task<PagedResult<TEntity>> ProcessPaginationAsync<TEntity>
75
80
internal static PagedResult < TEntity > ProcessPagination < TEntity > ( this IQueryable < TEntity > query ,
76
81
int page = DEF_PAGE , int perpage = DEF_PERPAGE , bool skipCount = DEF_SKIPCOUNT )
77
82
{
83
+ ValidateParams_IfInvalid_Throw ( page , perpage ) ;
84
+
78
85
int total = 0 ;
79
86
var list = new List < TEntity > ( ) ;
80
87
@@ -96,6 +103,17 @@ internal static PagedResult<TEntity> ProcessPagination<TEntity>(this IQueryable<
96
103
Items = list
97
104
} ;
98
105
}
106
+
107
+ internal static void ValidateParams_IfInvalid_Throw ( int page , int perpage )
108
+ {
109
+ if ( page <= 0 )
110
+ throw new ArgumentException ( "Page parameter must be greater than zero." , nameof ( page ) ) ;
111
+
112
+ if ( perpage < 0 )
113
+ throw new ArgumentException ( "Per-page parameter must be 0 or greater than 0." , nameof ( perpage ) ) ;
114
+
115
+ return ;
116
+ }
99
117
internal static Task < int > CountEntitiesAsync < TEntity > ( this IQueryable < TEntity > query , CancellationToken token = default )
100
118
{
101
119
return query . CountAsync ( token ) ;
@@ -107,7 +125,6 @@ internal static int CalculateTotalPages(int totalItems, int perpage)
107
125
{
108
126
ans = totalItems / perpage ;
109
127
ans += ( totalItems % perpage ) > 0 ? 1 : 0 ;
110
- return ans ;
111
128
}
112
129
return ans ;
113
130
}
0 commit comments