< Summary

Information
Class: ArturRios.Output.PaginatedOutput<T>
Assembly: ArturRios.Output
File(s): /home/runner/work/dotnet-output/dotnet-output/src/PaginatedOutput.cs
Line coverage
100%
Covered lines: 34
Uncovered lines: 0
Coverable lines: 34
Total lines: 178
Line coverage: 100%
Branch coverage
75%
Covered branches: 6
Total branches: 8
Branch coverage: 75%
Method coverage
100%
Covered methods: 18
Fully covered methods: 18
Total methods: 18
Method coverage: 100%
Full method coverage: 100%

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
get_PageNumber()100%11100%
get_PageSize()100%11100%
get_TotalItems()100%11100%
get_TotalPages()100%22100%
get_NextPageToken()100%11100%
get_HasNextPage()100%11100%
get_New()100%11100%
WithPagination(...)100%11100%
WithNextPageToken(...)100%11100%
AddItem(...)50%22100%
AddItems(...)50%22100%
WithData(...)100%11100%
WithData(...)100%22100%
WithEmptyData()100%11100%
WithError(...)100%11100%
WithErrors(...)100%11100%
WithMessage(...)100%11100%
WithMessages(...)100%11100%

File(s)

/home/runner/work/dotnet-output/dotnet-output/src/PaginatedOutput.cs

#LineLine coverage
 1namespace 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>
 7public class PaginatedOutput<T> : DataOutput<List<T>>
 8{
 9    /// <summary>
 10    /// Current page number (1-based).
 11    /// </summary>
 20412    public int PageNumber { get; set; }
 13
 14    /// <summary>
 15    /// Number of items requested per page. This is the requested page size, not the
 16    /// number of items actually present in <see cref="DataOutput{T}.Data"/>, which may
 17    /// be smaller on the last page.
 18    /// </summary>
 24519    public int PageSize { get; set; }
 20
 21    /// <summary>
 22    /// Total number of items across all pages.
 23    /// </summary>
 23624    public int TotalItems { get; set; }
 25
 26    /// <summary>
 27    /// Total number of pages computed from <see cref="TotalItems"/> and <see cref="PageSize"/>.
 28    /// Evaluates to <c>0</c> when no page size has been set.
 29    /// </summary>
 3330    public int TotalPages => PageSize == 0 ? 0 : (int)Math.Ceiling((double)TotalItems / PageSize);
 31
 32    /// <summary>
 33    /// Opaque token that fetches the page after this one through
 34    /// <see cref="PaginatedOutputExtensions.PaginateByToken{T, TKey}"/>, or <c>null</c> when this is the last page
 35    /// (and always on outputs built by offset pagination).
 36    /// </summary>
 26837    public string? NextPageToken { get; set; }
 38
 39    /// <summary>
 40    /// Indicates whether a page follows this one, i.e. whether <see cref="NextPageToken"/> is set.
 41    /// </summary>
 1742    public bool HasNextPage => NextPageToken is not null;
 43
 44    /// <summary>
 45    /// Creates a new <see cref="PaginatedOutput{T}"/> instance.
 46    /// </summary>
 18247    public static new PaginatedOutput<T> New => new();
 48
 49    /// <summary>
 50    /// Sets pagination metadata for the current instance.
 51    /// </summary>
 52    /// <param name="pageNumber">The 1-based page number.</param>
 53    /// <param name="pageSize">The number of items requested per page.</param>
 54    /// <param name="totalItems">The total number of items available.</param>
 55    /// <returns>The same <see cref="PaginatedOutput{T}"/> instance for chaining.</returns>
 56    public PaginatedOutput<T> WithPagination(int pageNumber, int pageSize, int totalItems)
 57    {
 15958        PageNumber = pageNumber;
 15959        PageSize = pageSize;
 15960        TotalItems = totalItems;
 61
 15962        return this;
 63    }
 64
 65    /// <summary>
 66    /// Sets the token that fetches the next page and returns the instance.
 67    /// </summary>
 68    /// <param name="token">The next page's token, or <c>null</c> when there is no next page.</param>
 69    /// <returns>The same <see cref="PaginatedOutput{T}"/> instance for chaining.</returns>
 70    public PaginatedOutput<T> WithNextPageToken(string? token)
 71    {
 11972        NextPageToken = token;
 73
 11974        return this;
 75    }
 76
 77    /// <summary>
 78    /// Adds an item to the internal data list, creating the list if necessary.
 79    /// </summary>
 80    /// <param name="item">Item to add.</param>
 81    public void AddItem(T item)
 82    {
 183        Data ??= [];
 84
 185        Data.Add(item);
 186    }
 87
 88    /// <summary>
 89    /// Adds multiple items to the internal data list, creating the list if necessary.
 90    /// </summary>
 91    /// <param name="items">Items to add.</param>
 92    public void AddItems(IEnumerable<T> items)
 93    {
 294        Data ??= [];
 95
 296        Data.AddRange(items);
 297    }
 98
 99    /// <summary>
 100    /// Replaces the internal data list with the provided list and returns the instance.
 101    /// </summary>
 102    /// <param name="data">The list to set.</param>
 103    /// <returns>The same <see cref="PaginatedOutput{T}"/> instance for chaining.</returns>
 104    public new PaginatedOutput<T> WithData(List<T> data)
 105    {
 168106        Data = data;
 107
 168108        return this;
 109    }
 110
 111    /// <summary>
 112    /// Adds a single item to the data list and returns the instance.
 113    /// </summary>
 114    /// <param name="data">The item to add.</param>
 115    public PaginatedOutput<T> WithData(T data)
 116    {
 2117        Data ??= [];
 118
 2119        Data.Add(data);
 120
 2121        return this;
 122    }
 123
 124    /// <summary>
 125    /// Sets the internal data list to an empty list and returns the instance.
 126    /// </summary>
 127    /// <returns>The same <see cref="PaginatedOutput{T}"/> instance for chaining.</returns>
 128    public PaginatedOutput<T> WithEmptyData()
 129    {
 12130        Data = [];
 131
 12132        return this;
 133    }
 134
 135    /// <summary>
 136    /// Fluent helper to add an error on the current instance.
 137    /// </summary>
 138    /// <param name="error">Error message to add.</param>
 139    public new PaginatedOutput<T> WithError(string error)
 140    {
 11141        AddError(error);
 142
 11143        return this;
 144    }
 145
 146    /// <summary>
 147    /// Fluent helper to add multiple errors on the current instance.
 148    /// </summary>
 149    /// <param name="errors">Errors to add.</param>
 150    public new PaginatedOutput<T> WithErrors(IEnumerable<string>? errors)
 151    {
 1152        AddErrors(errors);
 153
 1154        return this;
 155    }
 156
 157    /// <summary>
 158    /// Fluent helper to add an informational message on the current instance.
 159    /// </summary>
 160    /// <param name="message">Message to add.</param>
 161    public new PaginatedOutput<T> WithMessage(string message)
 162    {
 1163        AddMessage(message);
 164
 1165        return this;
 166    }
 167
 168    /// <summary>
 169    /// Fluent helper to add multiple informational messages on the current instance.
 170    /// </summary>
 171    /// <param name="messages">Messages to add.</param>
 172    public new PaginatedOutput<T> WithMessages(IEnumerable<string>? messages)
 173    {
 1174        AddMessages(messages);
 175
 1176        return this;
 177    }
 178}