| | 1 | | using Plainquire.Page.Abstractions; |
| | 2 | | using Plainquire.Page.JsonConverters; |
| | 3 | | using System; |
| | 4 | | using System.Globalization; |
| | 5 | | using System.Text.Json; |
| | 6 | | using System.Text.Json.Serialization; |
| | 7 | |
|
| | 8 | | namespace Plainquire.Page; |
| | 9 | |
|
| | 10 | | /// <summary> |
| | 11 | | /// Hub to create pagination for <typeparamref name="TEntity"/>. |
| | 12 | | /// </summary> |
| | 13 | | /// <typeparam name="TEntity">The entity type to be paged.</typeparam> |
| | 14 | | [JsonConverter(typeof(EntityPageConverter.Factory))] |
| | 15 | | public class EntityPage<TEntity> : EntityPage |
| | 16 | | { |
| | 17 | | /// <inheritdoc /> |
| | 18 | | public EntityPage() { } |
| | 19 | |
|
| | 20 | | /// <inheritdoc /> |
| | 21 | | public EntityPage(PageConfiguration configuration) |
| | 22 | | : base(configuration) { } |
| | 23 | |
|
| | 24 | | /// <inheritdoc /> |
| | 25 | | public EntityPage(int? pageNumber, int? pageSize) |
| | 26 | | : base(pageNumber, pageSize) { } |
| | 27 | |
|
| | 28 | | /// <inheritdoc /> |
| | 29 | | public EntityPage(int? pageNumber, int? pageSize, PageConfiguration configuration) |
| | 30 | | : base(pageNumber, pageSize, configuration) { } |
| | 31 | |
|
| | 32 | | /// <summary> |
| | 33 | | /// Creates a deep clone of this page. |
| | 34 | | /// </summary> |
| | 35 | | public new EntityPage<TEntity> Clone() |
| | 36 | | => JsonSerializer.Deserialize<EntityPage<TEntity>>(JsonSerializer.Serialize(this))!; |
| | 37 | |
|
| | 38 | | /// <summary> |
| | 39 | | /// Casts this page to a different entity type (by creating a deep clone). |
| | 40 | | /// </summary> |
| | 41 | | /// <typeparam name="TDestination">The type of the destination entity to page.</typeparam> |
| | 42 | | public EntityPage<TDestination> Cast<TDestination>() |
| | 43 | | => JsonSerializer.Deserialize<EntityPage<TDestination>>(JsonSerializer.Serialize(this))!; |
| | 44 | | } |
| | 45 | |
|
| | 46 | | /// <inheritdoc cref="EntityPage{TEntity}" /> |
| | 47 | | [JsonConverter(typeof(EntityPageConverter))] |
| | 48 | | public class EntityPage : ICloneable |
| | 49 | | { |
| 242 | 50 | | internal string PageNumberValue = string.Empty; |
| 242 | 51 | | internal string PageSizeValue = string.Empty; |
| | 52 | |
|
| | 53 | | /// <summary> |
| | 54 | | /// Gets or sets the default configuration. Can be used to set a system-wide configuration. |
| | 55 | | /// </summary> |
| | 56 | | public PageConfiguration? Configuration { get; internal set; } |
| | 57 | |
|
| | 58 | | /// <summary> |
| | 59 | | /// The page number to get. |
| | 60 | | /// </summary> |
| | 61 | | public int? PageNumber |
| | 62 | | { |
| 397 | 63 | | get => int.TryParse(PageNumberValue, NumberStyles.None, CultureInfo.InvariantCulture, out var page) ? page : nul |
| 16 | 64 | | set => PageNumberValue = value?.ToString(CultureInfo.InvariantCulture) ?? string.Empty; |
| | 65 | | } |
| | 66 | |
|
| | 67 | | /// <summary> |
| | 68 | | /// The page size to use. |
| | 69 | | /// </summary> |
| | 70 | | public int? PageSize |
| | 71 | | { |
| 707 | 72 | | get => int.TryParse(PageSizeValue, NumberStyles.None, CultureInfo.InvariantCulture, out var pageSize) ? pageSize |
| 16 | 73 | | set => PageSizeValue = value?.ToString(CultureInfo.InvariantCulture) ?? string.Empty; |
| | 74 | | } |
| | 75 | |
|
| | 76 | | /// <summary> |
| | 77 | | /// Number of rows to skip when fetching the page. |
| | 78 | | /// </summary> |
| 4 | 79 | | public int? Skip => (PageNumber - 1) * PageSize; |
| | 80 | |
|
| | 81 | | /// <summary> |
| | 82 | | /// Number of rows to take when fetching the page. |
| | 83 | | /// </summary> |
| 4 | 84 | | public int? Take => PageSize; |
| | 85 | |
|
| | 86 | | /// <summary> |
| | 87 | | /// Creates a new instance of <see cref="EntityPage"/>. |
| | 88 | | /// </summary> |
| 232 | 89 | | public EntityPage() { } |
| | 90 | |
|
| | 91 | | /// <summary> |
| | 92 | | /// Creates a new instance of <see cref="EntityPage"/>. |
| | 93 | | /// </summary> |
| | 94 | | /// <param name="configuration">The configuration to use.</param> |
| | 95 | | public EntityPage(PageConfiguration configuration) |
| 0 | 96 | | : this() |
| 0 | 97 | | => Configuration = configuration; |
| | 98 | |
|
| | 99 | | /// <summary> |
| | 100 | | /// Creates a new instance of <see cref="EntityPage"/>. |
| | 101 | | /// </summary> |
| | 102 | | /// <param name="pageNumber">Page number.</param> |
| | 103 | | /// <param name="pageSize">Page size.</param> |
| | 104 | | public EntityPage(int? pageNumber, int? pageSize) |
| | 105 | | { |
| 10 | 106 | | PageNumber = pageNumber; |
| 10 | 107 | | PageSize = pageSize; |
| 10 | 108 | | } |
| | 109 | |
|
| | 110 | | /// <summary> |
| | 111 | | /// Creates a new instance of <see cref="EntityPage"/>. |
| | 112 | | /// </summary> |
| | 113 | | /// <param name="pageNumber">Page number.</param> |
| | 114 | | /// <param name="pageSize">Page size.</param> |
| | 115 | | /// <param name="configuration">The configuration to use.</param> |
| | 116 | | public EntityPage(int? pageNumber, int? pageSize, PageConfiguration configuration) |
| | 117 | | { |
| 0 | 118 | | PageNumber = pageNumber; |
| 0 | 119 | | PageSize = pageSize; |
| 0 | 120 | | Configuration = configuration; |
| 0 | 121 | | } |
| | 122 | |
|
| | 123 | | /// <inheritdoc /> |
| | 124 | | public object Clone() |
| 1 | 125 | | => JsonSerializer.Deserialize<EntityPage>(JsonSerializer.Serialize(this))!; |
| | 126 | | } |