-
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
223 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
namespace Raiqub.Expressions.Queries; | ||
|
||
internal static class PagedQuery | ||
{ | ||
public static IQueryable<T> PrepareQueryForPaging<T>(this IQueryable<T> queryable, int pageNumber, int pageSize) | ||
{ | ||
if (pageNumber < 1) | ||
{ | ||
throw new ArgumentOutOfRangeException( | ||
nameof(pageNumber), | ||
$"Page number must be greater than or equals to 1, but it is {pageNumber}"); | ||
} | ||
|
||
if (pageSize < 1) | ||
{ | ||
throw new ArgumentOutOfRangeException( | ||
nameof(pageSize), | ||
$"Page size must be greater than or equals to 1, but it is {pageSize}"); | ||
} | ||
|
||
return pageNumber != 1 | ||
? queryable.Skip((pageNumber - 1) * pageSize).Take(pageSize) | ||
: queryable.Take(pageSize); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
namespace Raiqub.Expressions.Queries.Internal; | ||
|
||
public static class Paging | ||
{ | ||
public static bool PageNumberExists(int pageNumber, int pageSize, long totalCount) | ||
{ | ||
long pageCount = GetPageCount(pageSize, totalCount); | ||
return pageNumber <= pageCount; | ||
} | ||
|
||
public static long GetPageCount(int pageSize, long totalCount) | ||
{ | ||
return totalCount > 0L ? (long)Math.Ceiling(totalCount / (double)pageSize) : 0L; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
using Raiqub.Expressions.Queries.Internal; | ||
|
||
namespace Raiqub.Expressions.Queries; | ||
|
||
/// <summary>Represents the result of paged query.</summary> | ||
/// <typeparam name="TResult">The type of items returned by the query.</typeparam> | ||
public sealed class PagedResult<TResult> | ||
{ | ||
public PagedResult(long pageNumber, int pageSize, IReadOnlyList<TResult> items, long totalCount) | ||
{ | ||
PageNumber = pageNumber; | ||
PageSize = pageSize; | ||
Items = items; | ||
TotalCount = totalCount; | ||
|
||
PageCount = Paging.GetPageCount(pageSize, totalCount); | ||
} | ||
|
||
/// <summary>Gets current page number (one-based).</summary> | ||
public long PageNumber { get; } | ||
|
||
/// <summary>Gets page size.</summary> | ||
public int PageSize { get; } | ||
|
||
/// <summary>Gets the records found for the paged query.</summary> | ||
public IReadOnlyList<TResult> Items { get; } | ||
|
||
/// <summary>Gets the total number of records.</summary> | ||
public long TotalCount { get; } | ||
|
||
/// <summary>Gets number of available pages.</summary> | ||
public long PageCount { get; } | ||
|
||
/// <summary>Gets a value indicating whether there is a previous page.</summary> | ||
public bool HasPreviousPage => PageCount > 0 && PageNumber > 1; | ||
|
||
/// <summary>Gets a value indicating whether there is a next page.</summary> | ||
public bool HasNextPage => PageNumber < PageCount; | ||
|
||
/// <summary>Gets a value indicating whether the current page is the first page.</summary> | ||
public bool IsFirstPage => PageCount > 0 && PageNumber == 1; | ||
|
||
/// <summary>Gets a value indicating whether the current page is the last page.</summary> | ||
public bool IsLastPage => PageCount > 0 && PageNumber == PageCount; | ||
|
||
/// <summary>Gets a value indicating whether the current page exists.</summary> | ||
public bool IsValidPage => PageNumber <= PageCount; | ||
|
||
/// <summary>Gets one-based index of first item in current page.</summary> | ||
public long FirstItemOnPage => IsValidPage ? (PageNumber - 1L) * PageSize + 1L : 0L; | ||
|
||
/// <summary>Gets one-based index of last item in current page.</summary> | ||
public long LastItemOnPage | ||
{ | ||
get | ||
{ | ||
if (!IsValidPage) return 0L; | ||
long num = FirstItemOnPage + PageSize - 1L; | ||
return num > TotalCount ? TotalCount : num; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters