| | | 1 | | namespace Elsa.Common.Models; |
| | | 2 | | |
| | | 3 | | /// <summary> |
| | | 4 | | /// Represents pagination arguments. |
| | | 5 | | /// </summary> |
| | | 6 | | public record PageArgs |
| | | 7 | | { |
| | | 8 | | /// <summary> |
| | | 9 | | /// Gets pagination arguments that returns all items. |
| | | 10 | | /// </summary> |
| | 9 | 11 | | public static PageArgs All => new() { Offset = null, Limit = null }; |
| | | 12 | | |
| | | 13 | | /// <summary> |
| | | 14 | | /// Creates pagination arguments from a page number and page size. |
| | | 15 | | /// </summary> |
| | | 16 | | /// <param name="page">The zero-based page number.</param> |
| | | 17 | | /// <param name="pageSize">The number of items per page.</param> |
| | 3 | 18 | | public static PageArgs FromPage(int? page, int? pageSize) => new() { Offset = page * pageSize, Limit = pageSize }; |
| | | 19 | | |
| | | 20 | | /// <summary> |
| | | 21 | | /// Creates pagination arguments from an offset and limit. |
| | | 22 | | /// </summary> |
| | | 23 | | /// <param name="offset">The number of items to skip.</param> |
| | | 24 | | /// <param name="limit">The number of items to take.</param> |
| | 38 | 25 | | public static PageArgs FromRange(int? offset, int? limit) => new() { Offset = offset, Limit = limit }; |
| | | 26 | | |
| | | 27 | | /// <summary> |
| | | 28 | | /// Creates pagination arguments from page and page size or offset and limit. |
| | | 29 | | /// </summary> |
| | | 30 | | /// <param name="page">The zero-based page number.</param> |
| | | 31 | | /// <param name="pageSize">The number of items per page.</param> |
| | | 32 | | /// <param name="offset">The number of items to skip.</param> |
| | | 33 | | /// <param name="limit">The number of items to take.</param> |
| | | 34 | | /// <exception cref="ArgumentException">Thrown when neither page and pageSize nor offset and limit are specified.</e |
| | | 35 | | public static PageArgs From(int? page, int? pageSize, int? offset, int? limit) |
| | | 36 | | { |
| | 0 | 37 | | if(page != null && pageSize != null) |
| | 0 | 38 | | return FromPage(page, pageSize); |
| | | 39 | | |
| | 0 | 40 | | if(offset != null && limit != null) |
| | 0 | 41 | | return FromRange(offset, limit); |
| | | 42 | | |
| | 0 | 43 | | return FromPage(0, 100); |
| | | 44 | | } |
| | | 45 | | |
| | | 46 | | /// <summary> |
| | | 47 | | /// Gets the offset of the page. |
| | | 48 | | /// </summary> |
| | 145 | 49 | | public int? Offset { get; set; } |
| | | 50 | | |
| | | 51 | | /// <summary> |
| | | 52 | | /// Gets or sets the limit of the page. |
| | | 53 | | /// </summary> |
| | 146 | 54 | | public int? Limit { get; set; } |
| | | 55 | | |
| | | 56 | | /// <summary> |
| | | 57 | | /// Gets the zero-based page number. |
| | | 58 | | /// </summary> |
| | 2 | 59 | | public int? Page => Offset.HasValue && Limit.HasValue ? Offset / Limit : null; |
| | | 60 | | |
| | | 61 | | /// <summary> |
| | | 62 | | /// Gets the number of items per page. |
| | | 63 | | /// </summary> |
| | 2 | 64 | | public int? PageSize => Limit; |
| | | 65 | | |
| | | 66 | | /// <summary> |
| | | 67 | | /// Returns pagination arguments for the next page. |
| | | 68 | | /// </summary> |
| | | 69 | | /// <returns>The arguments for the next page.</returns> |
| | 0 | 70 | | public PageArgs Next() => this with { Offset = Page + 1 }; |
| | | 71 | | } |