Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add pagination classes and extensions #6

Merged
merged 2 commits into from
May 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions src/TemplateFastEndpoints.API/Extensions/PaginationExtensions.cs
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) where T : class
{
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);
}
}
7 changes: 7 additions & 0 deletions src/TemplateFastEndpoints.API/Utils/PaginatedRequest.cs
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; }
}
19 changes: 19 additions & 0 deletions src/TemplateFastEndpoints.API/Utils/PaginatedResponse.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
namespace TemplateFastEndpoints.API.Utils;

public class PaginatedResponse<T> where T : class
{
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; }
}
Loading