< Summary

Information
Class: Pozitron.QuerySpecification.Pagination
Assembly: Pozitron.QuerySpecification
File(s): /home/runner/work/QuerySpecification/QuerySpecification/src/QuerySpecification/Paging/Pagination.cs
Tag: 44_11195777782
Line coverage
100%
Covered lines: 49
Uncovered lines: 0
Coverable lines: 49
Total lines: 106
Line coverage: 100%
Branch coverage
100%
Covered branches: 20
Total branches: 20
Branch coverage: 100%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
get_TotalItems()100%11100%
get_TotalPages()100%11100%
get_PageSize()100%11100%
get_Page()100%11100%
get_StartItem()100%11100%
get_EndItem()100%11100%
get_HasPrevious()100%11100%
get_HasNext()100%11100%
get_Take()100%11100%
get_Skip()100%11100%
.ctor(...)100%11100%
get_Empty()100%11100%
.ctor(...)100%11100%
.ctor(...)100%11100%
.ctor(...)100%11100%
.ctor(...)100%44100%
GetHandledTotalItems(...)100%22100%
GetHandledPageSize(...)100%66100%
GetHandledTotalPages(...)100%22100%
GetHandledPage(...)100%66100%

File(s)

/home/runner/work/QuerySpecification/QuerySpecification/src/QuerySpecification/Paging/Pagination.cs

#LineLine coverage
 1using System.Runtime.CompilerServices;
 2using System.Text.Json.Serialization;
 3
 4namespace Pozitron.QuerySpecification;
 5
 6public record Pagination
 7{
 8    private readonly PaginationSettings _paginationSettings;
 9
 18110    public int TotalItems { get; }
 12411    public int TotalPages { get; }
 29812    public int PageSize { get; }
 29813    public int Page { get; }
 2114    public int StartItem { get; }
 2115    public int EndItem { get; }
 2116    public bool HasPrevious { get; }
 2117    public bool HasNext { get; }
 18
 19    [JsonIgnore]
 2920    public int Take { get; }
 21    [JsonIgnore]
 2922    public int Skip { get; }
 23
 24    [JsonConstructor]
 425    public Pagination(int totalItems, int totalPages, int pageSize, int page, int startItem, int endItem, bool hasPrevio
 26    {
 427        _paginationSettings = default!;
 428        TotalItems = totalItems;
 429        TotalPages = totalPages;
 430        PageSize = pageSize;
 431        Page = page;
 432        StartItem = startItem;
 433        EndItem = endItem;
 434        HasPrevious = hasPrevious;
 435        HasNext = hasNext;
 436    }
 37
 238    public static Pagination Empty { get; } = new Pagination(PaginationSettings.Default, 0, null, null);
 39
 40    public Pagination(int itemsCount, PagingFilter filter)
 441        : this(PaginationSettings.Default, itemsCount, filter.PageSize, filter.Page)
 42    {
 443    }
 44
 45    public Pagination(int itemsCount, int? pageSize, int? page)
 646        : this(PaginationSettings.Default, itemsCount, pageSize, page)
 47    {
 648    }
 49
 50    public Pagination(PaginationSettings paginationSettings, int itemsCount, PagingFilter filter)
 951        : this(paginationSettings, itemsCount, filter.PageSize, filter.Page)
 52    {
 953    }
 54
 4855    public Pagination(PaginationSettings paginationSettings, int itemsCount, int? pageSize, int? page)
 56    {
 4857        _paginationSettings = paginationSettings;
 58
 59        // The order of actions is important
 4860        TotalItems = GetHandledTotalItems(itemsCount);
 4861        PageSize = GetHandledPageSize(_paginationSettings, pageSize);
 4862        TotalPages = GetHandledTotalPages(TotalItems, PageSize);
 4863        Page = GetHandledPage(_paginationSettings, TotalPages, page);
 64
 4865        HasNext = Page != TotalPages;
 4866        HasPrevious = Page != 1;
 67
 4868        StartItem = TotalItems == 0 ? 0 : (PageSize * (Page - 1)) + 1;
 4869        EndItem = (PageSize * Page) > TotalItems ? TotalItems : PageSize * Page;
 70
 4871        Take = PageSize;
 4872        Skip = PageSize * (Page - 1);
 4873    }
 74
 75    [MethodImpl(MethodImplOptions.AggressiveInlining)]
 76    private static int GetHandledTotalItems(int itemsCount)
 77    {
 4878        return itemsCount < 0 ? 0 : itemsCount;
 79    }
 80
 81    [MethodImpl(MethodImplOptions.AggressiveInlining)]
 82    private static int GetHandledPageSize(PaginationSettings settings, int? pageSize)
 83    {
 5784        if (!pageSize.HasValue || pageSize <= 0) return settings.DefaultPageSize;
 85
 4686        if (pageSize > settings.DefaultPageSizeLimit) return settings.DefaultPageSizeLimit;
 87
 3288        return pageSize.Value;
 89    }
 90
 91    [MethodImpl(MethodImplOptions.AggressiveInlining)]
 92    private static int GetHandledTotalPages(int handledTotalItems, int handledPageSize)
 93    {
 4894        return handledTotalItems == 0 ? 1 : (int)Math.Ceiling((decimal)handledTotalItems / handledPageSize);
 95    }
 96
 97    [MethodImpl(MethodImplOptions.AggressiveInlining)]
 98    private static int GetHandledPage(PaginationSettings settings, int handledTotalPages, int? page)
 99    {
 56100        if (!page.HasValue || page <= 0) return settings.DefaultPage;
 101
 47102        if (page.Value > handledTotalPages) return handledTotalPages;
 103
 33104        return page.Value;
 105    }
 106}