-
Notifications
You must be signed in to change notification settings - Fork 0
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
1 parent
a086656
commit 566d4f2
Showing
1 changed file
with
32 additions
and
0 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,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); | ||
} | ||
} | ||
} |