| | 1 | | using System.Runtime.CompilerServices; |
| | 2 | | using System.Text.Json.Serialization; |
| | 3 | |
|
| | 4 | | namespace Pozitron.QuerySpecification; |
| | 5 | |
|
| | 6 | | public record Pagination |
| | 7 | | { |
| | 8 | | private readonly PaginationSettings _paginationSettings; |
| | 9 | |
|
| 181 | 10 | | public int TotalItems { get; } |
| 124 | 11 | | public int TotalPages { get; } |
| 298 | 12 | | public int PageSize { get; } |
| 298 | 13 | | public int Page { get; } |
| 21 | 14 | | public int StartItem { get; } |
| 21 | 15 | | public int EndItem { get; } |
| 21 | 16 | | public bool HasPrevious { get; } |
| 21 | 17 | | public bool HasNext { get; } |
| | 18 | |
|
| | 19 | | [JsonIgnore] |
| 29 | 20 | | public int Take { get; } |
| | 21 | | [JsonIgnore] |
| 29 | 22 | | public int Skip { get; } |
| | 23 | |
|
| | 24 | | [JsonConstructor] |
| 4 | 25 | | public Pagination(int totalItems, int totalPages, int pageSize, int page, int startItem, int endItem, bool hasPrevio |
| | 26 | | { |
| 4 | 27 | | _paginationSettings = default!; |
| 4 | 28 | | TotalItems = totalItems; |
| 4 | 29 | | TotalPages = totalPages; |
| 4 | 30 | | PageSize = pageSize; |
| 4 | 31 | | Page = page; |
| 4 | 32 | | StartItem = startItem; |
| 4 | 33 | | EndItem = endItem; |
| 4 | 34 | | HasPrevious = hasPrevious; |
| 4 | 35 | | HasNext = hasNext; |
| 4 | 36 | | } |
| | 37 | |
|
| 2 | 38 | | public static Pagination Empty { get; } = new Pagination(PaginationSettings.Default, 0, null, null); |
| | 39 | |
|
| | 40 | | public Pagination(int itemsCount, PagingFilter filter) |
| 4 | 41 | | : this(PaginationSettings.Default, itemsCount, filter.PageSize, filter.Page) |
| | 42 | | { |
| 4 | 43 | | } |
| | 44 | |
|
| | 45 | | public Pagination(int itemsCount, int? pageSize, int? page) |
| 6 | 46 | | : this(PaginationSettings.Default, itemsCount, pageSize, page) |
| | 47 | | { |
| 6 | 48 | | } |
| | 49 | |
|
| | 50 | | public Pagination(PaginationSettings paginationSettings, int itemsCount, PagingFilter filter) |
| 9 | 51 | | : this(paginationSettings, itemsCount, filter.PageSize, filter.Page) |
| | 52 | | { |
| 9 | 53 | | } |
| | 54 | |
|
| 48 | 55 | | public Pagination(PaginationSettings paginationSettings, int itemsCount, int? pageSize, int? page) |
| | 56 | | { |
| 48 | 57 | | _paginationSettings = paginationSettings; |
| | 58 | |
|
| | 59 | | // The order of actions is important |
| 48 | 60 | | TotalItems = GetHandledTotalItems(itemsCount); |
| 48 | 61 | | PageSize = GetHandledPageSize(_paginationSettings, pageSize); |
| 48 | 62 | | TotalPages = GetHandledTotalPages(TotalItems, PageSize); |
| 48 | 63 | | Page = GetHandledPage(_paginationSettings, TotalPages, page); |
| | 64 | |
|
| 48 | 65 | | HasNext = Page != TotalPages; |
| 48 | 66 | | HasPrevious = Page != 1; |
| | 67 | |
|
| 48 | 68 | | StartItem = TotalItems == 0 ? 0 : (PageSize * (Page - 1)) + 1; |
| 48 | 69 | | EndItem = (PageSize * Page) > TotalItems ? TotalItems : PageSize * Page; |
| | 70 | |
|
| 48 | 71 | | Take = PageSize; |
| 48 | 72 | | Skip = PageSize * (Page - 1); |
| 48 | 73 | | } |
| | 74 | |
|
| | 75 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | 76 | | private static int GetHandledTotalItems(int itemsCount) |
| | 77 | | { |
| 48 | 78 | | return itemsCount < 0 ? 0 : itemsCount; |
| | 79 | | } |
| | 80 | |
|
| | 81 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | 82 | | private static int GetHandledPageSize(PaginationSettings settings, int? pageSize) |
| | 83 | | { |
| 57 | 84 | | if (!pageSize.HasValue || pageSize <= 0) return settings.DefaultPageSize; |
| | 85 | |
|
| 46 | 86 | | if (pageSize > settings.DefaultPageSizeLimit) return settings.DefaultPageSizeLimit; |
| | 87 | |
|
| 32 | 88 | | return pageSize.Value; |
| | 89 | | } |
| | 90 | |
|
| | 91 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | 92 | | private static int GetHandledTotalPages(int handledTotalItems, int handledPageSize) |
| | 93 | | { |
| 48 | 94 | | return handledTotalItems == 0 ? 1 : (int)Math.Ceiling((decimal)handledTotalItems / handledPageSize); |
| | 95 | | } |
| | 96 | |
|
| | 97 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | 98 | | private static int GetHandledPage(PaginationSettings settings, int handledTotalPages, int? page) |
| | 99 | | { |
| 56 | 100 | | if (!page.HasValue || page <= 0) return settings.DefaultPage; |
| | 101 | |
|
| 47 | 102 | | if (page.Value > handledTotalPages) return handledTotalPages; |
| | 103 | |
|
| 33 | 104 | | return page.Value; |
| | 105 | | } |
| | 106 | | } |