Skip to content

Commit

Permalink
Classse helper de paginação
Browse files Browse the repository at this point in the history
  • Loading branch information
suarezrafael committed Aug 13, 2021
1 parent a086656 commit 566d4f2
Showing 1 changed file with 32 additions and 0 deletions.
32 changes: 32 additions & 0 deletions CourseLibrary.API/Helpers/PagedList.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace CourseLibrary.API.Helpers
{
public class PagedList<T> : List<T>
{
public int CurrentPage { get; private set; }
public int TotalPages { get; private set; }
public int PageSize { get; private set; }
public int TotalCount { get; private set; }
public bool HasPrevious => (CurrentPage > 1);
public bool HasNext => (CurrentPage < TotalPages);

public PagedList(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 static PagedList<T> Create(IQueryable<T> source, int pageNumber, int pageSize)
{
var count = source.Count();
var items = source.Skip((pageNumber - 1) * pageSize).Take(pageSize).ToList();
return new PagedList<T>(items, count, pageNumber, pageSize);
}
}
}

0 comments on commit 566d4f2

Please sign in to comment.