diff --git a/Pagination.EntityFrameworkCore.Extensions/Pagination.cs b/Pagination.EntityFrameworkCore.Extensions/Pagination.cs index 15a219d..1c857e5 100644 --- a/Pagination.EntityFrameworkCore.Extensions/Pagination.cs +++ b/Pagination.EntityFrameworkCore.Extensions/Pagination.cs @@ -5,68 +5,78 @@ namespace Pagination.EntityFrameworkCore.Extensions { - public class Pagination - { - public long TotalItems { get; private set; } - public int CurrentPage { get; private set; } - public int? NextPage { get; private set; } - public int? PreviousPage { get; private set; } - public int TotalPages { get; private set; } - public IEnumerable Results { get; private set; } + public class Pagination + { + public long TotalItems { get; private set; } + public int CurrentPage { get; private set; } + public int? NextPage { get; private set; } + public int? PreviousPage { get; private set; } + public int TotalPages { get; private set; } + public IEnumerable Results { get; private set; } - public Pagination(Pagination pagination) - { - TotalItems = pagination.TotalPages; - CurrentPage = pagination.CurrentPage; - NextPage = pagination.NextPage; - PreviousPage = pagination.PreviousPage; - TotalPages = pagination.TotalPages; - Results = pagination.Results; - } + public Pagination() + { + TotalItems = 0; + CurrentPage = 1; + NextPage = null; + PreviousPage = null; + TotalPages = 1; + Results = Enumerable.Empty(); + } - public Pagination(IEnumerable results, long totalItems, int page = 1, int limit = 10, bool applyPageAndLimitToResults = false) - { - if (page <= 0) - { - throw new PaginationException("Page must be greater than 0"); - } + public Pagination(Pagination pagination) + { + TotalItems = pagination.TotalPages; + CurrentPage = pagination.CurrentPage; + NextPage = pagination.NextPage; + PreviousPage = pagination.PreviousPage; + TotalPages = pagination.TotalPages; + Results = pagination.Results; + } - var startIndex = (page - 1) * limit; - var endIndex = page * limit; + public Pagination(IEnumerable results, long totalItems, int page = 1, int limit = 10, bool applyPageAndLimitToResults = false) + { + if (page <= 0) + { + throw new PaginationException("Page must be greater than 0"); + } - TotalItems = totalItems; - CurrentPage = page; - Results = results ?? Enumerable.Empty(); - if (applyPageAndLimitToResults) - { - Results = Results.Skip(startIndex).Take(limit); - } - if (startIndex > 0) - { - PreviousPage = page - 1; - } - if (endIndex < totalItems) - { - NextPage = page + 1; - } + var startIndex = (page - 1) * limit; + var endIndex = page * limit; - TotalPages = limit > 0 ? (int)Math.Ceiling((decimal)totalItems / (decimal)limit) : 0; - } + TotalItems = totalItems; + CurrentPage = page; + Results = results ?? Enumerable.Empty(); + if (applyPageAndLimitToResults) + { + Results = Results.Skip(startIndex).Take(limit); + } + if (startIndex > 0) + { + PreviousPage = page - 1; + } + if (endIndex < totalItems) + { + NextPage = page + 1; + } - public static Pagination GetPagination(IEnumerable results, long totalItems, Func convertTSourceToTDestinationMethod, int page = 1, int limit = 10, bool applyPageAndLimitToResults = false) - { - var destinationResults = results?.Select(x => convertTSourceToTDestinationMethod(x)) ?? new List(); - return new Pagination(destinationResults, totalItems, page, limit, applyPageAndLimitToResults); - } + TotalPages = limit > 0 ? (int)Math.Ceiling((decimal)totalItems / (decimal)limit) : 0; + } - public static async Task> GetPaginationAsync(IEnumerable results, long totalItems, Func> convertTSourceToTDestinationMethod, int page = 1, int limit = 10, bool applyPageAndLimitToResults = false) - { - if (results == null) - { - return new Pagination(new List(), totalItems, page, limit, applyPageAndLimitToResults); - } - var destinationResults = await results.Select(async ev => await convertTSourceToTDestinationMethod(ev)).WhenAll().ConfigureAwait(false); - return new Pagination(destinationResults, totalItems, page, limit, applyPageAndLimitToResults); - } - } + public static Pagination GetPagination(IEnumerable results, long totalItems, Func convertTSourceToTDestinationMethod, int page = 1, int limit = 10, bool applyPageAndLimitToResults = false) + { + var destinationResults = results?.Select(x => convertTSourceToTDestinationMethod(x)) ?? new List(); + return new Pagination(destinationResults, totalItems, page, limit, applyPageAndLimitToResults); + } + + public static async Task> GetPaginationAsync(IEnumerable results, long totalItems, Func> convertTSourceToTDestinationMethod, int page = 1, int limit = 10, bool applyPageAndLimitToResults = false) + { + if (results == null) + { + return new Pagination(new List(), totalItems, page, limit, applyPageAndLimitToResults); + } + var destinationResults = await results.Select(async ev => await convertTSourceToTDestinationMethod(ev)).WhenAll().ConfigureAwait(false); + return new Pagination(destinationResults, totalItems, page, limit, applyPageAndLimitToResults); + } + } } \ No newline at end of file