48 lines
1.5 KiB
C#
48 lines
1.5 KiB
C#
using AutoMapper;
|
|
using AutoMapper.QueryableExtensions;
|
|
|
|
namespace ExpenseTracker.Application.Common.Models;
|
|
|
|
public class PaginatedList<T>
|
|
{
|
|
public IReadOnlyCollection<T> Items { get; }
|
|
public int PageNumber { get; }
|
|
public int TotalPages { get; }
|
|
public int TotalCount { get; }
|
|
|
|
public PaginatedList(IReadOnlyCollection<T> items, int count, int pageNumber, int pageSize)
|
|
{
|
|
PageNumber = pageNumber;
|
|
TotalPages = (int) Math.Ceiling(count / (double) pageSize);
|
|
TotalCount = count;
|
|
Items = items;
|
|
}
|
|
|
|
public bool HasPreviousPage => PageNumber > 1;
|
|
|
|
public bool HasNextPage => PageNumber < TotalPages;
|
|
|
|
public static PaginatedList<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 PaginatedList<T>(items, count, pageNumber, pageSize);
|
|
}
|
|
|
|
public static PaginatedList<TDestination> Create<TSource, TDestination>(
|
|
IQueryable<TSource> source,
|
|
int pageNumber,
|
|
int pageSize,
|
|
IConfigurationProvider mappingConfigurationProvider)
|
|
{
|
|
var count = source.Count();
|
|
var items = source.Skip((pageNumber - 1) * pageSize).Take(pageSize).ToList();
|
|
|
|
var mappedItems = items.AsQueryable()
|
|
.ProjectTo<TDestination>(mappingConfigurationProvider).ToList();
|
|
|
|
return new PaginatedList<TDestination>(mappedItems, count, pageNumber, pageSize);
|
|
}
|
|
}
|