| | 1 | | using System.Runtime.CompilerServices; |
| | 2 | | using System.Text.Json.Serialization; |
| | 3 | |
|
| | 4 | | namespace Pozitron.QuerySpecification; |
| | 5 | |
|
| | 6 | | /// <summary> |
| | 7 | | /// Represents pagination information. |
| | 8 | | /// </summary> |
| | 9 | | public record Pagination |
| | 10 | | { |
| | 11 | | private readonly PaginationSettings _paginationSettings; |
| | 12 | |
|
| | 13 | | /// <summary> |
| | 14 | | /// Gets the total number of items. |
| | 15 | | /// </summary> |
| 181 | 16 | | public int TotalItems { get; } |
| | 17 | |
|
| | 18 | | /// <summary> |
| | 19 | | /// Gets the total number of pages. |
| | 20 | | /// </summary> |
| 124 | 21 | | public int TotalPages { get; } |
| | 22 | |
|
| | 23 | | /// <summary> |
| | 24 | | /// Gets the page size. |
| | 25 | | /// </summary> |
| 298 | 26 | | public int PageSize { get; } |
| | 27 | |
|
| | 28 | | /// <summary> |
| | 29 | | /// Gets the current page number. |
| | 30 | | /// </summary> |
| 298 | 31 | | public int Page { get; } |
| | 32 | |
|
| | 33 | | /// <summary> |
| | 34 | | /// Gets the start item number. |
| | 35 | | /// </summary> |
| 21 | 36 | | public int StartItem { get; } |
| | 37 | |
|
| | 38 | | /// <summary> |
| | 39 | | /// Gets the end item number. |
| | 40 | | /// </summary> |
| 21 | 41 | | public int EndItem { get; } |
| | 42 | |
|
| | 43 | | /// <summary> |
| | 44 | | /// Gets a value indicating whether there is a previous page. |
| | 45 | | /// </summary> |
| 21 | 46 | | public bool HasPrevious { get; } |
| | 47 | |
|
| | 48 | | /// <summary> |
| | 49 | | /// Gets a value indicating whether there is a next page. |
| | 50 | | /// </summary> |
| 21 | 51 | | public bool HasNext { get; } |
| | 52 | |
|
| | 53 | | /// <summary> |
| | 54 | | /// Gets the number of items to take. |
| | 55 | | /// </summary> |
| | 56 | | [JsonIgnore] |
| 29 | 57 | | public int Take { get; } |
| | 58 | |
|
| | 59 | | /// <summary> |
| | 60 | | /// Gets the number of items to skip. |
| | 61 | | /// </summary> |
| | 62 | | [JsonIgnore] |
| 29 | 63 | | public int Skip { get; } |
| | 64 | |
|
| | 65 | | /// <summary> |
| | 66 | | /// Initializes a new instance of the <see cref="Pagination"/> class. |
| | 67 | | /// </summary> |
| | 68 | | /// <param name="totalItems">The total number of items.</param> |
| | 69 | | /// <param name="totalPages">The total number of pages.</param> |
| | 70 | | /// <param name="pageSize">The page size.</param> |
| | 71 | | /// <param name="page">The current page number.</param> |
| | 72 | | /// <param name="startItem">The start item number.</param> |
| | 73 | | /// <param name="endItem">The end item number.</param> |
| | 74 | | /// <param name="hasPrevious">A value indicating whether there is a previous page.</param> |
| | 75 | | /// <param name="hasNext">A value indicating whether there is a next page.</param> |
| | 76 | | [JsonConstructor] |
| 4 | 77 | | public Pagination(int totalItems, int totalPages, int pageSize, int page, int startItem, int endItem, bool hasPrevio |
| | 78 | | { |
| 4 | 79 | | _paginationSettings = default!; |
| 4 | 80 | | TotalItems = totalItems; |
| 4 | 81 | | TotalPages = totalPages; |
| 4 | 82 | | PageSize = pageSize; |
| 4 | 83 | | Page = page; |
| 4 | 84 | | StartItem = startItem; |
| 4 | 85 | | EndItem = endItem; |
| 4 | 86 | | HasPrevious = hasPrevious; |
| 4 | 87 | | HasNext = hasNext; |
| 4 | 88 | | } |
| | 89 | |
|
| | 90 | | /// <summary> |
| | 91 | | /// Gets an pagination instance with zero items. |
| | 92 | | /// </summary> |
| 2 | 93 | | public static Pagination Empty { get; } = new Pagination(PaginationSettings.Default, 0, null, null); |
| | 94 | |
|
| | 95 | | /// <summary> |
| | 96 | | /// Initializes a new instance of the <see cref="Pagination"/> class with the specified items count and filter. |
| | 97 | | /// </summary> |
| | 98 | | /// <param name="itemsCount">The total number of items.</param> |
| | 99 | | /// <param name="filter">The paging filter.</param> |
| | 100 | | public Pagination(int itemsCount, PagingFilter filter) |
| 4 | 101 | | : this(PaginationSettings.Default, itemsCount, filter.PageSize, filter.Page) |
| | 102 | | { |
| 4 | 103 | | } |
| | 104 | |
|
| | 105 | | /// <summary> |
| | 106 | | /// Initializes a new instance of the <see cref="Pagination"/> class with the specified items count, page size, and |
| | 107 | | /// </summary> |
| | 108 | | /// <param name="itemsCount">The total number of items.</param> |
| | 109 | | /// <param name="pageSize">The page size.</param> |
| | 110 | | /// <param name="page">The current page number.</param> |
| | 111 | | public Pagination(int itemsCount, int? pageSize, int? page) |
| 6 | 112 | | : this(PaginationSettings.Default, itemsCount, pageSize, page) |
| | 113 | | { |
| 6 | 114 | | } |
| | 115 | |
|
| | 116 | | /// <summary> |
| | 117 | | /// Initializes a new instance of the <see cref="Pagination"/> class with the specified pagination settings, items c |
| | 118 | | /// </summary> |
| | 119 | | /// <param name="paginationSettings">The pagination settings.</param> |
| | 120 | | /// <param name="itemsCount">The total number of items.</param> |
| | 121 | | /// <param name="filter">The paging filter.</param> |
| | 122 | | public Pagination(PaginationSettings paginationSettings, int itemsCount, PagingFilter filter) |
| 9 | 123 | | : this(paginationSettings, itemsCount, filter.PageSize, filter.Page) |
| | 124 | | { |
| 9 | 125 | | } |
| | 126 | |
|
| | 127 | | /// <summary> |
| | 128 | | /// Initializes a new instance of the <see cref="Pagination"/> class with the specified pagination settings, items c |
| | 129 | | /// </summary> |
| | 130 | | /// <param name="paginationSettings">The pagination settings.</param> |
| | 131 | | /// <param name="itemsCount">The total number of items.</param> |
| | 132 | | /// <param name="pageSize">The page size.</param> |
| | 133 | | /// <param name="page">The current page number.</param> |
| 48 | 134 | | public Pagination(PaginationSettings paginationSettings, int itemsCount, int? pageSize, int? page) |
| | 135 | | { |
| 48 | 136 | | _paginationSettings = paginationSettings; |
| | 137 | |
|
| | 138 | | // The order of actions is important |
| 48 | 139 | | TotalItems = GetHandledTotalItems(itemsCount); |
| 48 | 140 | | PageSize = GetHandledPageSize(_paginationSettings, pageSize); |
| 48 | 141 | | TotalPages = GetHandledTotalPages(TotalItems, PageSize); |
| 48 | 142 | | Page = GetHandledPage(_paginationSettings, TotalPages, page); |
| | 143 | |
|
| 48 | 144 | | HasNext = Page != TotalPages; |
| 48 | 145 | | HasPrevious = Page != 1; |
| | 146 | |
|
| 48 | 147 | | StartItem = TotalItems == 0 ? 0 : (PageSize * (Page - 1)) + 1; |
| 48 | 148 | | EndItem = (PageSize * Page) > TotalItems ? TotalItems : PageSize * Page; |
| | 149 | |
|
| 48 | 150 | | Take = PageSize; |
| 48 | 151 | | Skip = PageSize * (Page - 1); |
| 48 | 152 | | } |
| | 153 | |
|
| | 154 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | 155 | | private static int GetHandledTotalItems(int itemsCount) |
| | 156 | | { |
| 48 | 157 | | return itemsCount < 0 ? 0 : itemsCount; |
| | 158 | | } |
| | 159 | |
|
| | 160 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | 161 | | private static int GetHandledPageSize(PaginationSettings settings, int? pageSize) |
| | 162 | | { |
| 57 | 163 | | if (!pageSize.HasValue || pageSize <= 0) return settings.DefaultPageSize; |
| | 164 | |
|
| 46 | 165 | | if (pageSize > settings.DefaultPageSizeLimit) return settings.DefaultPageSizeLimit; |
| | 166 | |
|
| 32 | 167 | | return pageSize.Value; |
| | 168 | | } |
| | 169 | |
|
| | 170 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | 171 | | private static int GetHandledTotalPages(int handledTotalItems, int handledPageSize) |
| | 172 | | { |
| 48 | 173 | | return handledTotalItems == 0 ? 1 : (int)Math.Ceiling((decimal)handledTotalItems / handledPageSize); |
| | 174 | | } |
| | 175 | |
|
| | 176 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | 177 | | private static int GetHandledPage(PaginationSettings settings, int handledTotalPages, int? page) |
| | 178 | | { |
| 56 | 179 | | if (!page.HasValue || page <= 0) return settings.DefaultPage; |
| | 180 | |
|
| 47 | 181 | | if (page.Value > handledTotalPages) return handledTotalPages; |
| | 182 | |
|
| 33 | 183 | | return page.Value; |
| | 184 | | } |
| | 185 | | } |