-
-
Notifications
You must be signed in to change notification settings - Fork 525
Expand file tree
/
Copy pathPaginationResult.cs
More file actions
184 lines (162 loc) · 6.3 KB
/
Copy pathPaginationResult.cs
File metadata and controls
184 lines (162 loc) · 6.3 KB
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
using System;
using System.Collections.Generic;
using System.Data;
using System.Threading;
using System.Threading.Tasks;
namespace SqlKata.Execution
{
/// <summary>
/// Represents one page of query results together with pagination metadata
/// and helpers for loading adjacent pages.
/// </summary>
/// <typeparam name="T">The type of each item in the result set.</typeparam>
public class PaginationResult<T>
{
/// <summary>
/// Gets or sets the query used to produce the paginated results.
/// </summary>
public Query Query { get; set; }
/// <summary>
/// Gets or sets the total number of matching records across all pages.
/// </summary>
public long Count { get; set; }
/// <summary>
/// Gets or sets the records in the current page.
/// </summary>
public IEnumerable<T> List { get; set; }
/// <summary>
/// Gets or sets the one-based current page number.
/// </summary>
public int Page { get; set; }
/// <summary>
/// Gets or sets the maximum number of records per page.
/// </summary>
public int PerPage { get; set; }
/// <summary>
/// Gets the total number of pages, rounded up from <see cref="Count"/>.
/// </summary>
public int TotalPages
{
get
{
if (PerPage < 1)
{
return 0;
}
var div = (float)Count / PerPage;
return (int)Math.Ceiling(div);
}
}
/// <summary>
/// Gets a value indicating whether this is the first page.
/// </summary>
public bool IsFirst
{
get
{
return Page == 1;
}
}
/// <summary>
/// Gets a value indicating whether this is the last page.
/// </summary>
public bool IsLast
{
get
{
return Page == TotalPages;
}
}
/// <summary>
/// Gets a value indicating whether a page follows the current page.
/// </summary>
public bool HasNext
{
get
{
return Page < TotalPages;
}
}
/// <summary>
/// Gets a value indicating whether a page precedes the current page.
/// </summary>
public bool HasPrevious
{
get
{
return Page > 1;
}
}
/// <summary>
/// Applies the next page's limit and offset to <see cref="Query"/>.
/// </summary>
/// <returns>The query configured for the next page.</returns>
public Query NextQuery()
{
return this.Query.ForPage(Page + 1, PerPage);
}
/// <summary>
/// Executes the query for the next page.
/// </summary>
/// <param name="transaction">The transaction to use, or <see langword="null"/> for none.</param>
/// <param name="timeout">The command timeout in seconds, or <see langword="null"/> to use the configured default.</param>
/// <returns>The next page of results.</returns>
public PaginationResult<T> Next(IDbTransaction transaction = null, int? timeout = null)
{
return this.Query.Paginate<T>(Page + 1, PerPage, transaction, timeout);
}
/// <summary>
/// Asynchronously executes the query for the next page.
/// </summary>
/// <param name="transaction">The transaction to use, or <see langword="null"/> for none.</param>
/// <param name="timeout">The command timeout in seconds, or <see langword="null"/> to use the configured default.</param>
/// <param name="cancellationToken">The token used to cancel the operation.</param>
/// <returns>A task containing the next page of results.</returns>
public async Task<PaginationResult<T>> NextAsync(IDbTransaction transaction = null, int? timeout = null, CancellationToken cancellationToken = default)
{
return await this.Query.PaginateAsync<T>(Page + 1, PerPage, transaction, timeout, cancellationToken);
}
/// <summary>
/// Applies the previous page's limit and offset to <see cref="Query"/>.
/// </summary>
/// <returns>The query configured for the previous page.</returns>
public Query PreviousQuery()
{
return this.Query.ForPage(Page - 1, PerPage);
}
/// <summary>
/// Executes the query for the previous page.
/// </summary>
/// <param name="transaction">The transaction to use, or <see langword="null"/> for none.</param>
/// <param name="timeout">The command timeout in seconds, or <see langword="null"/> to use the configured default.</param>
/// <returns>The previous page of results.</returns>
public PaginationResult<T> Previous(IDbTransaction transaction = null, int? timeout = null)
{
return this.Query.Paginate<T>(Page - 1, PerPage, transaction, timeout);
}
/// <summary>
/// Asynchronously executes the query for the previous page.
/// </summary>
/// <param name="transaction">The transaction to use, or <see langword="null"/> for none.</param>
/// <param name="timeout">The command timeout in seconds, or <see langword="null"/> to use the configured default.</param>
/// <param name="cancellationToken">The token used to cancel the operation.</param>
/// <returns>A task containing the previous page of results.</returns>
public async Task<PaginationResult<T>> PreviousAsync(IDbTransaction transaction = null, int? timeout = null, CancellationToken cancellationToken = default)
{
return await this.Query.PaginateAsync<T>(Page - 1, PerPage, transaction, timeout, cancellationToken);
}
/// <summary>
/// Gets an iterator that starts with this page and loads each remaining page.
/// </summary>
public PaginationIterator<T> Each
{
get
{
return new PaginationIterator<T>
{
FirstPage = this
};
}
}
}
}