| | | 1 | | using Microsoft.EntityFrameworkCore; |
| | | 2 | | using System; |
| | | 3 | | using System.Collections.Generic; |
| | | 4 | | using System.Linq; |
| | | 5 | | using System.Text; |
| | | 6 | | using System.Threading.Tasks; |
| | | 7 | | |
| | | 8 | | namespace WinSolutions.Sveta.Server.Domain |
| | | 9 | | { |
| | | 10 | | public class PaginatedList<T>: List<T> |
| | | 11 | | { |
| | 0 | 12 | | public int CurrentPage { get; private set; } |
| | 0 | 13 | | public int TotalPage { get; private set; } |
| | 0 | 14 | | public PaginatedList(List<T> items, int count, int currentPage, int limit) |
| | 0 | 15 | | { |
| | 0 | 16 | | CurrentPage = currentPage; |
| | 0 | 17 | | TotalPage = (int)Math.Ceiling(count / (double)limit); |
| | 0 | 18 | | this.AddRange(items); |
| | 0 | 19 | | } |
| | | 20 | | public bool HasPreviousPage |
| | | 21 | | { |
| | | 22 | | get |
| | 0 | 23 | | { |
| | 0 | 24 | | return (CurrentPage > 1); |
| | 0 | 25 | | } |
| | | 26 | | } |
| | | 27 | | public bool HasNextPage |
| | | 28 | | { |
| | | 29 | | get |
| | 0 | 30 | | { |
| | 0 | 31 | | return (CurrentPage < TotalPage); |
| | 0 | 32 | | } |
| | | 33 | | } |
| | | 34 | | public static async Task<PaginatedList<T>> CreateAsync(IQueryable<T> source, int currentPage, int limit) |
| | 0 | 35 | | { |
| | 0 | 36 | | var count = await source.CountAsync(); |
| | 0 | 37 | | var items = await source.Skip(currentPage * limit).Take(limit).ToListAsync(); |
| | 0 | 38 | | return new PaginatedList<T>(items, count, currentPage, limit); |
| | 0 | 39 | | } |
| | | 40 | | } |
| | | 41 | | } |