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

AddedPagination #29

Merged
merged 1 commit into from
Feb 22, 2024
Merged
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
37 changes: 37 additions & 0 deletions Inflow.Domain/Pagination/PaginatedList.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
using Inflow.Domain.Common;
using Inflow.Domain.Responses;
using static System.Net.WebRequestMethods;

namespace Inflow.Domain.Pagniation;

public class PaginatedList<T> : List<T> where T : class
{
public int CurrentPage { get; set; }
public int TotalPages { get; set; }
public int PageSize { get; set; }
public int TotalCount { get; set; }
public bool HasPrevious => CurrentPage > 1;
public bool HasNext => CurrentPage < TotalPages;

public PaginatedList(List<T> items, int count, int pageNumber, int pageSize)
{
TotalCount = count;
PageSize = pageSize;
CurrentPage = pageNumber;
TotalPages = (int)Math.Ceiling(count / (double)pageSize);

AddRange(items);
}

public GetBaseResponse<T> ToResponse()
=> new()
{
Data = this.ToList(),
HasNextPage = HasNext,
HasPreviousPage = HasPrevious,
PageNumber = CurrentPage,
PageSize = PageSize,
TotalPages = TotalPages,
TotalCount = TotalCount
};
}
34 changes: 34 additions & 0 deletions Inflow.Domain/Pagination/PaginationExtension.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
using Inflow.Domain.Common;
using Microsoft.EntityFrameworkCore;

namespace Inflow.Domain.Pagniation
{
public static class PaginationExtension
{
public static async Task<PaginatedList<T>> ToPaginatedListAsync<T>(
this IQueryable<T> source,
int pageSize,
int pageNumber) where T : EntityBase
{
var count = await source.CountAsync();
var items = await source.Skip((pageNumber - 1) * pageSize)
.Take(pageSize)
.ToListAsync();

return new PaginatedList<T>(items, count, pageNumber, pageSize);
}

public static PaginatedList<T> ToPaginatedList<T>(
this IQueryable<T> source,
int pageSize,
int pageNumber) where T : EntityBase
{
var count = source.Count();
var items = source.Skip((pageNumber - 1) * pageSize)
.Take(pageSize)
.ToList();

return new PaginatedList<T>(items, count, pageNumber, pageSize);
}
}
}