From e5a14cd75e2984ce23f8d608447d6def125757cf Mon Sep 17 00:00:00 2001 From: Jackson Veroneze Date: Tue, 21 May 2024 08:35:12 -0300 Subject: [PATCH] feat: ajustes --- .github/workflows/run-create-package.yml | 2 +- .github/workflows/run-unit-tests.yml | 2 +- global.json | 2 +- .../Extensions/PageMemoryExtension.cs | 7 +-- .../JacksonVeroneze.NET.Pagination.csproj | 6 +- src/JacksonVeroneze.NET.Pagination/Page.cs | 23 +++++++- .../PageInfo.cs | 56 ++++--------------- .../PaginationParameters.cs | 2 +- ...onVeroneze.NET.Pagination.UnitTests.csproj | 12 ++-- .../PageTests.cs | 7 ++- ...JacksonVeroneze.NET.Pagination.Util.csproj | 6 +- 11 files changed, 54 insertions(+), 71 deletions(-) diff --git a/.github/workflows/run-create-package.yml b/.github/workflows/run-create-package.yml index 03fb913..f41c380 100644 --- a/.github/workflows/run-create-package.yml +++ b/.github/workflows/run-create-package.yml @@ -17,7 +17,7 @@ jobs: - name: Setup .NET uses: actions/setup-dotnet@v1 with: - dotnet-version: '8.0.x' + dotnet-version: '6.0.x' - name: Restore dependencies run: dotnet restore diff --git a/.github/workflows/run-unit-tests.yml b/.github/workflows/run-unit-tests.yml index d73fcfa..022ce15 100644 --- a/.github/workflows/run-unit-tests.yml +++ b/.github/workflows/run-unit-tests.yml @@ -18,7 +18,7 @@ jobs: - name: Setup .NET uses: actions/setup-dotnet@v1 with: - dotnet-version: '7.0.x' + dotnet-version: '6.0.x' - name: Restore dependencies run: dotnet restore diff --git a/global.json b/global.json index 9c3262d..03dfa51 100644 --- a/global.json +++ b/global.json @@ -1,6 +1,6 @@ { "sdk": { - "version": "8.0.0", + "version": "6.0.0", "rollForward": "major", "allowPrerelease": true } diff --git a/src/JacksonVeroneze.NET.Pagination/Extensions/PageMemoryExtension.cs b/src/JacksonVeroneze.NET.Pagination/Extensions/PageMemoryExtension.cs index 64b23ff..a13128c 100644 --- a/src/JacksonVeroneze.NET.Pagination/Extensions/PageMemoryExtension.cs +++ b/src/JacksonVeroneze.NET.Pagination/Extensions/PageMemoryExtension.cs @@ -21,7 +21,7 @@ private static Page GetPage( { int count = totalElements ?? source.Count; - ICollection pageItems = + IEnumerable pageItems = GetPageItems(source, pagination); PageInfo pageInfo = new(pagination.Page, @@ -30,13 +30,12 @@ private static Page GetPage( return new Page(pageItems, pageInfo); } - private static ICollection GetPageItems( + private static IEnumerable GetPageItems( IEnumerable source, PaginationParameters pagination) { return source .Skip((pagination.Page - 1) * pagination.PageSize) - .Take(pagination.PageSize) - .ToArray(); + .Take(pagination.PageSize); } } \ No newline at end of file diff --git a/src/JacksonVeroneze.NET.Pagination/JacksonVeroneze.NET.Pagination.csproj b/src/JacksonVeroneze.NET.Pagination/JacksonVeroneze.NET.Pagination.csproj index 4c5fb2c..fd9eace 100644 --- a/src/JacksonVeroneze.NET.Pagination/JacksonVeroneze.NET.Pagination.csproj +++ b/src/JacksonVeroneze.NET.Pagination/JacksonVeroneze.NET.Pagination.csproj @@ -1,10 +1,10 @@ - net7.0;net8.0 + net6.0 latest enable enable - true + false false JacksonVeroneze.NET.Pagination @@ -21,7 +21,7 @@ - + diff --git a/src/JacksonVeroneze.NET.Pagination/Page.cs b/src/JacksonVeroneze.NET.Pagination/Page.cs index e8d3a0b..d2137fe 100644 --- a/src/JacksonVeroneze.NET.Pagination/Page.cs +++ b/src/JacksonVeroneze.NET.Pagination/Page.cs @@ -1,8 +1,25 @@ +using System.Collections.ObjectModel; + namespace JacksonVeroneze.NET.Pagination; -public class Page +public record Page { - public Page(IEnumerable data, PageInfo pagination) + public Page(List data, PageInfo pagination) : + this(data.AsReadOnly(), pagination) + { + } + + public Page(ICollection data, PageInfo pagination) : + this(((List)data).AsReadOnly(), pagination) + { + } + + public Page(IEnumerable data, PageInfo pagination) : + this((data.ToList()).AsReadOnly(), pagination) + { + } + + public Page(ReadOnlyCollection data, PageInfo pagination) { ArgumentNullException.ThrowIfNull(data); ArgumentNullException.ThrowIfNull(pagination); @@ -11,7 +28,7 @@ public Page(IEnumerable data, PageInfo pagination) Pagination = pagination; } - public IEnumerable Data { get; } + public ReadOnlyCollection Data { get; } public PageInfo Pagination { get; } } \ No newline at end of file diff --git a/src/JacksonVeroneze.NET.Pagination/PageInfo.cs b/src/JacksonVeroneze.NET.Pagination/PageInfo.cs index 11f15d6..fcb5574 100644 --- a/src/JacksonVeroneze.NET.Pagination/PageInfo.cs +++ b/src/JacksonVeroneze.NET.Pagination/PageInfo.cs @@ -1,13 +1,13 @@ namespace JacksonVeroneze.NET.Pagination; -public class PageInfo +public record PageInfo { - public PageInfo() - { - } - public PageInfo(int page, int pageSize, int totalElements) { + Guard.Against.NegativeOrZero(page, nameof(page)); + Guard.Against.NegativeOrZero(pageSize, nameof(pageSize)); + Guard.Against.Negative(totalElements, nameof(totalElements)); + Page = page; PageSize = pageSize; TotalElements = totalElements; @@ -21,49 +21,13 @@ public PageInfo(int page, int pageSize, int totalElements, Direction = direction; } - private readonly int _page; - private readonly int _pageSize; - private readonly int _totalElements; - - public int Page - { - get => _page; - init - { - Guard.Against.NegativeOrZero( - value, nameof(Page)); - - _page = value; - } - } - - public int PageSize - { - get => _pageSize; - init - { - Guard.Against.NegativeOrZero( - value, nameof(PageSize)); - - _pageSize = value; - } - } - - public int TotalElements - { - get => _totalElements; - init - { - Guard.Against.Negative( - value, nameof(TotalElements)); - - _totalElements = value; - } - } + public int Page { get; } + public int PageSize { get; } + public int TotalElements { get; } - public string? OrderBy { get; init; } + public string? OrderBy { get; } - public SortDirection? Direction { get; init; } + public SortDirection? Direction { get; } public int TotalPages => TotalElements > 0 ? (int)Math.Ceiling(TotalElements / (decimal)PageSize) : 0; diff --git a/src/JacksonVeroneze.NET.Pagination/PaginationParameters.cs b/src/JacksonVeroneze.NET.Pagination/PaginationParameters.cs index fec1f36..a4d0a2e 100644 --- a/src/JacksonVeroneze.NET.Pagination/PaginationParameters.cs +++ b/src/JacksonVeroneze.NET.Pagination/PaginationParameters.cs @@ -1,6 +1,6 @@ namespace JacksonVeroneze.NET.Pagination; -public class PaginationParameters +public record PaginationParameters { public PaginationParameters(int page, int pageSize) { diff --git a/tests/JacksonVeroneze.NET.Pagination.UnitTests/JacksonVeroneze.NET.Pagination.UnitTests.csproj b/tests/JacksonVeroneze.NET.Pagination.UnitTests/JacksonVeroneze.NET.Pagination.UnitTests.csproj index fd4df0f..4480122 100644 --- a/tests/JacksonVeroneze.NET.Pagination.UnitTests/JacksonVeroneze.NET.Pagination.UnitTests.csproj +++ b/tests/JacksonVeroneze.NET.Pagination.UnitTests/JacksonVeroneze.NET.Pagination.UnitTests.csproj @@ -1,7 +1,7 @@ - net8.0 + net6.0 latest enable enable @@ -12,18 +12,18 @@ - + - - + + runtime; build; native; contentfiles; analyzers; buildtransitive all - + runtime; build; native; contentfiles; analyzers; buildtransitive all - + all runtime; build; native; contentfiles; analyzers; buildtransitive diff --git a/tests/JacksonVeroneze.NET.Pagination.UnitTests/PageTests.cs b/tests/JacksonVeroneze.NET.Pagination.UnitTests/PageTests.cs index 7933816..0a352d6 100644 --- a/tests/JacksonVeroneze.NET.Pagination.UnitTests/PageTests.cs +++ b/tests/JacksonVeroneze.NET.Pagination.UnitTests/PageTests.cs @@ -41,13 +41,16 @@ public void Initialize_InvalidData_ThrowException() // ------------------------------------------------------- // Arrange // ------------------------------------------------------- - ICollection? data = null; + IReadOnlyCollection? data = null; PageInfo pageInfo = new(1, 1, 10); // ------------------------------------------------------- // Act // ------------------------------------------------------- - Action action = () => new Page(data!, pageInfo); + Action action = () => + { + Page _ = new (data!, pageInfo); + }; // ------------------------------------------------------- // Assert diff --git a/tests/JacksonVeroneze.NET.Pagination.Util/JacksonVeroneze.NET.Pagination.Util.csproj b/tests/JacksonVeroneze.NET.Pagination.Util/JacksonVeroneze.NET.Pagination.Util.csproj index 66bd655..7963bb9 100644 --- a/tests/JacksonVeroneze.NET.Pagination.Util/JacksonVeroneze.NET.Pagination.Util.csproj +++ b/tests/JacksonVeroneze.NET.Pagination.Util/JacksonVeroneze.NET.Pagination.Util.csproj @@ -1,15 +1,15 @@ - net8.0 + net6.0 latest enable enable - - + +