| | | 1 | | namespace ArturRios.Output; |
| | | 2 | | |
| | | 3 | | /// <summary> |
| | | 4 | | /// Represents a paginated collection result and pagination metadata. |
| | | 5 | | /// </summary> |
| | | 6 | | /// <typeparam name="T">Type of the items in the paginated collection.</typeparam> |
| | | 7 | | public class PaginatedOutput<T> : DataOutput<List<T>> |
| | | 8 | | { |
| | | 9 | | /// <summary> |
| | | 10 | | /// Current page number (1-based). |
| | | 11 | | /// </summary> |
| | 15 | 12 | | public int PageNumber { get; set; } |
| | | 13 | | |
| | | 14 | | /// <summary> |
| | | 15 | | /// Number of items in the current page. |
| | | 16 | | /// </summary> |
| | 3 | 17 | | public int PageSize => Data?.Count ?? 0; |
| | | 18 | | |
| | | 19 | | /// <summary> |
| | | 20 | | /// Total number of items across all pages. |
| | | 21 | | /// </summary> |
| | 10 | 22 | | public int TotalItems { get; set; } |
| | | 23 | | |
| | | 24 | | /// <summary> |
| | | 25 | | /// Total number of pages computed from <see cref="TotalItems"/> and <see cref="PageSize"/>. |
| | | 26 | | /// </summary> |
| | 1 | 27 | | public int TotalPages => (int)Math.Ceiling((double)TotalItems / PageSize); |
| | | 28 | | |
| | | 29 | | /// <summary> |
| | | 30 | | /// Creates a new <see cref="PaginatedOutput{T}"/> instance. |
| | | 31 | | /// </summary> |
| | 15 | 32 | | public static new PaginatedOutput<T> New => new(); |
| | | 33 | | |
| | | 34 | | /// <summary> |
| | | 35 | | /// Sets pagination metadata for the current instance. |
| | | 36 | | /// </summary> |
| | | 37 | | /// <param name="pageNumber">The 1-based page number.</param> |
| | | 38 | | /// <param name="totalItems">The total number of items available.</param> |
| | | 39 | | /// <returns>The same <see cref="PaginatedOutput{T}"/> instance for chaining.</returns> |
| | | 40 | | public PaginatedOutput<T> WithPagination(int pageNumber, int totalItems) |
| | 8 | 41 | | { |
| | 8 | 42 | | PageNumber = pageNumber; |
| | 8 | 43 | | TotalItems = totalItems; |
| | | 44 | | |
| | 8 | 45 | | return this; |
| | 8 | 46 | | } |
| | | 47 | | |
| | | 48 | | /// <summary> |
| | | 49 | | /// Adds an item to the internal data list, creating the list if necessary. |
| | | 50 | | /// </summary> |
| | | 51 | | /// <param name="item">Item to add.</param> |
| | | 52 | | public void AddItem(T item) |
| | 1 | 53 | | { |
| | 1 | 54 | | Data ??= []; |
| | | 55 | | |
| | 1 | 56 | | Data.Add(item); |
| | 1 | 57 | | } |
| | | 58 | | |
| | | 59 | | /// <summary> |
| | | 60 | | /// Adds multiple items to the internal data list, creating the list if necessary. |
| | | 61 | | /// </summary> |
| | | 62 | | /// <param name="items">Items to add.</param> |
| | | 63 | | public void AddItems(IEnumerable<T> items) |
| | 1 | 64 | | { |
| | 1 | 65 | | Data ??= []; |
| | | 66 | | |
| | 1 | 67 | | Data.AddRange(items); |
| | 1 | 68 | | } |
| | | 69 | | |
| | | 70 | | /// <summary> |
| | | 71 | | /// Replaces the internal data list with the provided list and returns the instance. |
| | | 72 | | /// </summary> |
| | | 73 | | /// <param name="data">The list to set.</param> |
| | | 74 | | /// <returns>The same <see cref="PaginatedOutput{T}"/> instance for chaining.</returns> |
| | | 75 | | public new PaginatedOutput<T> WithData(List<T> data) |
| | 14 | 76 | | { |
| | 14 | 77 | | Data = data; |
| | | 78 | | |
| | 14 | 79 | | return this; |
| | 14 | 80 | | } |
| | | 81 | | |
| | | 82 | | /// <summary> |
| | | 83 | | /// Adds a single item to the data list and returns the instance. |
| | | 84 | | /// </summary> |
| | | 85 | | /// <param name="data">The item to add.</param> |
| | | 86 | | public PaginatedOutput<T> WithData(T data) |
| | 2 | 87 | | { |
| | 2 | 88 | | Data ??= []; |
| | | 89 | | |
| | 2 | 90 | | Data.Add(data); |
| | | 91 | | |
| | 2 | 92 | | return this; |
| | 2 | 93 | | } |
| | | 94 | | |
| | | 95 | | /// <summary> |
| | | 96 | | /// Sets the internal data list to an empty list and returns the instance. |
| | | 97 | | /// </summary> |
| | | 98 | | /// <returns>The same <see cref="PaginatedOutput{T}"/> instance for chaining.</returns> |
| | | 99 | | public PaginatedOutput<T> WithEmptyData() |
| | 1 | 100 | | { |
| | 1 | 101 | | Data = []; |
| | | 102 | | |
| | 1 | 103 | | return this; |
| | 1 | 104 | | } |
| | | 105 | | |
| | | 106 | | /// <summary> |
| | | 107 | | /// Fluent helper to add an error on the current instance. |
| | | 108 | | /// </summary> |
| | | 109 | | /// <param name="error">Error message to add.</param> |
| | | 110 | | public new PaginatedOutput<T> WithError(string error) |
| | 1 | 111 | | { |
| | 1 | 112 | | AddError(error); |
| | | 113 | | |
| | 1 | 114 | | return this; |
| | 1 | 115 | | } |
| | | 116 | | |
| | | 117 | | /// <summary> |
| | | 118 | | /// Fluent helper to add multiple errors on the current instance. |
| | | 119 | | /// </summary> |
| | | 120 | | /// <param name="errors">Errors to add.</param> |
| | | 121 | | public new PaginatedOutput<T> WithErrors(IEnumerable<string> errors) |
| | 1 | 122 | | { |
| | 1 | 123 | | AddErrors(errors); |
| | | 124 | | |
| | 1 | 125 | | return this; |
| | 1 | 126 | | } |
| | | 127 | | |
| | | 128 | | /// <summary> |
| | | 129 | | /// Fluent helper to add an informational message on the current instance. |
| | | 130 | | /// </summary> |
| | | 131 | | /// <param name="message">Message to add.</param> |
| | | 132 | | public new PaginatedOutput<T> WithMessage(string message) |
| | 1 | 133 | | { |
| | 1 | 134 | | AddMessage(message); |
| | | 135 | | |
| | 1 | 136 | | return this; |
| | 1 | 137 | | } |
| | | 138 | | |
| | | 139 | | /// <summary> |
| | | 140 | | /// Fluent helper to add multiple informational messages on the current instance. |
| | | 141 | | /// </summary> |
| | | 142 | | /// <param name="messages">Messages to add.</param> |
| | | 143 | | public new PaginatedOutput<T> WithMessages(IEnumerable<string> messages) |
| | 1 | 144 | | { |
| | 1 | 145 | | AddMessages(messages); |
| | | 146 | | |
| | 1 | 147 | | return this; |
| | 1 | 148 | | } |
| | | 149 | | } |