< Summary

Information
Class: ArturRios.Output.PaginatedOutput<T>
Assembly: ArturRios.Output
File(s): D:\Repositories\dotnet-output\src\PaginatedOutput.cs
Line coverage
100%
Covered lines: 48
Uncovered lines: 0
Coverable lines: 48
Total lines: 154
Line coverage: 100%
Branch coverage
75%
Covered branches: 6
Total branches: 8
Branch coverage: 75%
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()100%11100%
get_TotalItems()100%11100%
get_TotalPages()100%22100%
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>
 2812    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>
 4619    public int PageSize { get; set; }
 20
 21    /// <summary>
 22    /// Total number of items across all pages.
 23    /// </summary>
 3324    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>
 1030    public int TotalPages => PageSize == 0 ? 0 : (int)Math.Ceiling((double)TotalItems / PageSize);
 31
 32    /// <summary>
 33    /// Creates a new <see cref="PaginatedOutput{T}"/> instance.
 34    /// </summary>
 2335    public static new PaginatedOutput<T> New => new();
 36
 37    /// <summary>
 38    /// Sets pagination metadata for the current instance.
 39    /// </summary>
 40    /// <param name="pageNumber">The 1-based page number.</param>
 41    /// <param name="pageSize">The number of items requested per page.</param>
 42    /// <param name="totalItems">The total number of items available.</param>
 43    /// <returns>The same <see cref="PaginatedOutput{T}"/> instance for chaining.</returns>
 44    public PaginatedOutput<T> WithPagination(int pageNumber, int pageSize, int totalItems)
 1545    {
 1546        PageNumber = pageNumber;
 1547        PageSize = pageSize;
 1548        TotalItems = totalItems;
 49
 1550        return this;
 1551    }
 52
 53    /// <summary>
 54    /// Adds an item to the internal data list, creating the list if necessary.
 55    /// </summary>
 56    /// <param name="item">Item to add.</param>
 57    public void AddItem(T item)
 158    {
 159        Data ??= [];
 60
 161        Data.Add(item);
 162    }
 63
 64    /// <summary>
 65    /// Adds multiple items to the internal data list, creating the list if necessary.
 66    /// </summary>
 67    /// <param name="items">Items to add.</param>
 68    public void AddItems(IEnumerable<T> items)
 269    {
 270        Data ??= [];
 71
 272        Data.AddRange(items);
 273    }
 74
 75    /// <summary>
 76    /// Replaces the internal data list with the provided list and returns the instance.
 77    /// </summary>
 78    /// <param name="data">The list to set.</param>
 79    /// <returns>The same <see cref="PaginatedOutput{T}"/> instance for chaining.</returns>
 80    public new PaginatedOutput<T> WithData(List<T> data)
 2081    {
 2082        Data = data;
 83
 2084        return this;
 2085    }
 86
 87    /// <summary>
 88    /// Adds a single item to the data list and returns the instance.
 89    /// </summary>
 90    /// <param name="data">The item to add.</param>
 91    public PaginatedOutput<T> WithData(T data)
 292    {
 293        Data ??= [];
 94
 295        Data.Add(data);
 96
 297        return this;
 298    }
 99
 100    /// <summary>
 101    /// Sets the internal data list to an empty list and returns the instance.
 102    /// </summary>
 103    /// <returns>The same <see cref="PaginatedOutput{T}"/> instance for chaining.</returns>
 104    public PaginatedOutput<T> WithEmptyData()
 2105    {
 2106        Data = [];
 107
 2108        return this;
 2109    }
 110
 111    /// <summary>
 112    /// Fluent helper to add an error on the current instance.
 113    /// </summary>
 114    /// <param name="error">Error message to add.</param>
 115    public new PaginatedOutput<T> WithError(string error)
 1116    {
 1117        AddError(error);
 118
 1119        return this;
 1120    }
 121
 122    /// <summary>
 123    /// Fluent helper to add multiple errors on the current instance.
 124    /// </summary>
 125    /// <param name="errors">Errors to add.</param>
 126    public new PaginatedOutput<T> WithErrors(IEnumerable<string> errors)
 1127    {
 1128        AddErrors(errors);
 129
 1130        return this;
 1131    }
 132
 133    /// <summary>
 134    /// Fluent helper to add an informational message on the current instance.
 135    /// </summary>
 136    /// <param name="message">Message to add.</param>
 137    public new PaginatedOutput<T> WithMessage(string message)
 1138    {
 1139        AddMessage(message);
 140
 1141        return this;
 1142    }
 143
 144    /// <summary>
 145    /// Fluent helper to add multiple informational messages on the current instance.
 146    /// </summary>
 147    /// <param name="messages">Messages to add.</param>
 148    public new PaginatedOutput<T> WithMessages(IEnumerable<string> messages)
 1149    {
 1150        AddMessages(messages);
 151
 1152        return this;
 1153    }
 154}