-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add pagination classes and extensions
- Loading branch information
Showing
3 changed files
with
50 additions
and
0 deletions.
There are no files selected for viewing
24 changes: 24 additions & 0 deletions
24
src/TemplateFastEndpoints.API/Extensions/PaginationExtensions.cs
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,24 @@ | ||
using TemplateFastEndpoints.API.Utils; | ||
|
||
namespace TemplateFastEndpoints.API.Extensions; | ||
|
||
public static class PaginationExtensions | ||
{ | ||
/// <summary> | ||
/// Extension method to paginate a source collection. | ||
/// </summary> | ||
/// <typeparam name="T">The type of the elements in the source.</typeparam> | ||
/// <param name="source">The source collection to paginate. Can be IQueryable or IEnumerable.</param> | ||
/// <param name="pageNumber">The page number to retrieve.</param> | ||
/// <param name="pageSize">The page size (number of items per page).</param> | ||
/// <returns>A paginated response containing the items on the requested page and pagination information.</returns> | ||
public static PaginatedResponse<T> ToPaginatedResult<T>(this IEnumerable<T> source, int pageNumber, int pageSize) | ||
{ | ||
var enumerable = source as IQueryable<T> ?? source.AsQueryable(); | ||
var items = enumerable.Skip((pageNumber - 1) * pageSize).Take(pageSize).ToList(); | ||
|
||
var totalRecords = enumerable.Count(); | ||
var totalPages = (int)Math.Ceiling((double)totalRecords / pageSize); | ||
return new PaginatedResponse<T>(items, pageNumber, pageSize, totalPages, totalRecords); | ||
} | ||
} |
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,7 @@ | ||
namespace TemplateFastEndpoints.API.Utils; | ||
|
||
public class PaginatedRequest | ||
{ | ||
public int PageNumber { get; set; } | ||
public int PageSize { get; set; } | ||
} |
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,19 @@ | ||
namespace TemplateFastEndpoints.API.Utils; | ||
|
||
public class PaginatedResponse<T> | ||
{ | ||
public PaginatedResponse(IEnumerable<T> data, int pageNumber, int pageSize,int totalPages, int totalRecords) | ||
{ | ||
Data = data; | ||
PageNumber = pageNumber; | ||
PageSize = pageSize; | ||
TotalPages = totalPages; | ||
TotalRecords = totalRecords; | ||
} | ||
|
||
public IEnumerable<T> Data { get; set; } | ||
public int PageNumber { get; set; } | ||
public int PageSize { get; set; } | ||
public int TotalPages { get; set; } | ||
public int TotalRecords { get; set; } | ||
} |