| | | 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 /> |
| | 430 | 18 | | public EntityPage() { } |
| | | 19 | | |
| | | 20 | | /// <inheritdoc /> |
| | | 21 | | public EntityPage(PageConfiguration configuration) |
| | 0 | 22 | | : base(configuration) { } |
| | | 23 | | |
| | | 24 | | /// <inheritdoc /> |
| | | 25 | | public EntityPage(uint? pageNumber, uint? pageSize) |
| | 14 | 26 | | : base(pageNumber, pageSize) { } |
| | | 27 | | |
| | | 28 | | /// <inheritdoc /> |
| | | 29 | | public EntityPage(uint? pageNumber, uint? pageSize, PageConfiguration configuration) |
| | 0 | 30 | | : base(pageNumber, pageSize, configuration) { } |
| | | 31 | | |
| | | 32 | | /// <summary> |
| | | 33 | | /// Creates a deep clone of this page. |
| | | 34 | | /// </summary> |
| | | 35 | | public new EntityPage<TEntity> Clone() |
| | 82 | 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>() |
| | 1 | 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 | | { |
| | | 50 | | internal string PageNumberValue = string.Empty; |
| | | 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 uint? PageNumber |
| | | 62 | | { |
| | | 63 | | get => uint.TryParse(PageNumberValue, NumberStyles.None, CultureInfo.InvariantCulture, out var page) ? page : nu |
| | | 64 | | set => PageNumberValue = value?.ToString(CultureInfo.InvariantCulture) ?? string.Empty; |
| | | 65 | | } |
| | | 66 | | |
| | | 67 | | /// <summary> |
| | | 68 | | /// The page size to use. |
| | | 69 | | /// </summary> |
| | | 70 | | public uint? PageSize |
| | | 71 | | { |
| | | 72 | | get => uint.TryParse(PageSizeValue, NumberStyles.None, CultureInfo.InvariantCulture, out var pageSize) ? pageSiz |
| | | 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> |
| | | 79 | | public uint? Skip => (PageNumber - 1) * PageSize; |
| | | 80 | | |
| | | 81 | | /// <summary> |
| | | 82 | | /// Number of rows to take when fetching the page. |
| | | 83 | | /// </summary> |
| | | 84 | | public uint? Take => PageSize; |
| | | 85 | | |
| | | 86 | | /// <summary> |
| | | 87 | | /// Creates a new instance of <see cref="EntityPage"/>. |
| | | 88 | | /// </summary> |
| | | 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) |
| | | 96 | | : this() |
| | | 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(uint? pageNumber, uint? pageSize) |
| | | 105 | | { |
| | | 106 | | PageNumber = pageNumber; |
| | | 107 | | PageSize = pageSize; |
| | | 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(uint? pageNumber, uint? pageSize, PageConfiguration configuration) |
| | | 117 | | { |
| | | 118 | | PageNumber = pageNumber; |
| | | 119 | | PageSize = pageSize; |
| | | 120 | | Configuration = configuration; |
| | | 121 | | } |
| | | 122 | | |
| | | 123 | | /// <inheritdoc /> |
| | | 124 | | public object Clone() |
| | | 125 | | => JsonSerializer.Deserialize<EntityPage>(JsonSerializer.Serialize(this))!; |
| | | 126 | | } |