< Summary

Information
Class: ArturRios.Output.PaginatedOutput<T>
Assembly: ArturRios.Output
File(s): D:\Repositories\dotnet-output\src\PaginatedOutput.cs
Line coverage
100%
Covered lines: 47
Uncovered lines: 0
Coverable lines: 47
Total lines: 149
Line coverage: 100%
Branch coverage
62%
Covered branches: 5
Total branches: 8
Branch coverage: 62.5%
Method coverage
100%
Covered methods: 15
Fully covered methods: 5
Total methods: 15
Method coverage: 100%
Full method coverage: 33.3%

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
get_PageNumber()100%11100%
get_PageSize()50%22100%
get_TotalItems()100%11100%
get_TotalPages()100%11100%
get_New()100%11100%
WithPagination(...)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)

D:\Repositories\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>
 1512    public int PageNumber { get; set; }
 13
 14    /// <summary>
 15    /// Number of items in the current page.
 16    /// </summary>
 317    public int PageSize => Data?.Count ?? 0;
 18
 19    /// <summary>
 20    /// Total number of items across all pages.
 21    /// </summary>
 1022    public int TotalItems { get; set; }
 23
 24    /// <summary>
 25    /// Total number of pages computed from <see cref="TotalItems"/> and <see cref="PageSize"/>.
 26    /// </summary>
 127    public int TotalPages => (int)Math.Ceiling((double)TotalItems / PageSize);
 28
 29    /// <summary>
 30    /// Creates a new <see cref="PaginatedOutput{T}"/> instance.
 31    /// </summary>
 1532    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)
 841    {
 842        PageNumber = pageNumber;
 843        TotalItems = totalItems;
 44
 845        return this;
 846    }
 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)
 153    {
 154        Data ??= [];
 55
 156        Data.Add(item);
 157    }
 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)
 164    {
 165        Data ??= [];
 66
 167        Data.AddRange(items);
 168    }
 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)
 1476    {
 1477        Data = data;
 78
 1479        return this;
 1480    }
 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)
 287    {
 288        Data ??= [];
 89
 290        Data.Add(data);
 91
 292        return this;
 293    }
 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()
 1100    {
 1101        Data = [];
 102
 1103        return this;
 1104    }
 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)
 1111    {
 1112        AddError(error);
 113
 1114        return this;
 1115    }
 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)
 1122    {
 1123        AddErrors(errors);
 124
 1125        return this;
 1126    }
 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)
 1133    {
 1134        AddMessage(message);
 135
 1136        return this;
 1137    }
 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)
 1144    {
 1145        AddMessages(messages);
 146
 1147        return this;
 1148    }
 149}