Skip to content

Commit

Permalink
feat: ajustes
Browse files Browse the repository at this point in the history
  • Loading branch information
jacksonveroneze committed May 21, 2024
1 parent 438f859 commit e5a14cd
Show file tree
Hide file tree
Showing 11 changed files with 54 additions and 71 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/run-create-package.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/run-unit-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion global.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"sdk": {
"version": "8.0.0",
"version": "6.0.0",
"rollForward": "major",
"allowPrerelease": true
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ private static Page<TType> GetPage<TType>(
{
int count = totalElements ?? source.Count;

ICollection<TType> pageItems =
IEnumerable<TType> pageItems =
GetPageItems(source, pagination);

PageInfo pageInfo = new(pagination.Page,
Expand All @@ -30,13 +30,12 @@ private static Page<TType> GetPage<TType>(
return new Page<TType>(pageItems, pageInfo);
}

private static ICollection<TType> GetPageItems<TType>(
private static IEnumerable<TType> GetPageItems<TType>(
IEnumerable<TType> source,
PaginationParameters pagination)
{
return source
.Skip((pagination.Page - 1) * pagination.PageSize)
.Take(pagination.PageSize)
.ToArray();
.Take(pagination.PageSize);
}
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFrameworks>net7.0;net8.0</TargetFrameworks>
<TargetFramework>net6.0</TargetFramework>
<LangVersion>latest</LangVersion>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<PublishAot>true</PublishAot>
<PublishAot>false</PublishAot>

<OpenApiGenerateDocuments>false</OpenApiGenerateDocuments>
<PackageId>JacksonVeroneze.NET.Pagination</PackageId>
Expand All @@ -21,7 +21,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Ardalis.GuardClauses" Version="4.2.0"/>
<PackageReference Include="Ardalis.GuardClauses" Version="4.0.1" />
</ItemGroup>

<ItemGroup>
Expand Down
23 changes: 20 additions & 3 deletions src/JacksonVeroneze.NET.Pagination/Page.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,25 @@
using System.Collections.ObjectModel;

namespace JacksonVeroneze.NET.Pagination;

public class Page<T>
public record Page<T>
{
public Page(IEnumerable<T> data, PageInfo pagination)
public Page(List<T> data, PageInfo pagination) :
this(data.AsReadOnly(), pagination)
{
}

public Page(ICollection<T> data, PageInfo pagination) :
this(((List<T>)data).AsReadOnly(), pagination)
{
}

public Page(IEnumerable<T> data, PageInfo pagination) :
this((data.ToList()).AsReadOnly(), pagination)
{
}

public Page(ReadOnlyCollection<T> data, PageInfo pagination)
{
ArgumentNullException.ThrowIfNull(data);
ArgumentNullException.ThrowIfNull(pagination);
Expand All @@ -11,7 +28,7 @@ public Page(IEnumerable<T> data, PageInfo pagination)
Pagination = pagination;
}

public IEnumerable<T> Data { get; }
public ReadOnlyCollection<T> Data { get; }

public PageInfo Pagination { get; }
}
56 changes: 10 additions & 46 deletions src/JacksonVeroneze.NET.Pagination/PageInfo.cs
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion src/JacksonVeroneze.NET.Pagination/PaginationParameters.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
namespace JacksonVeroneze.NET.Pagination;

public class PaginationParameters
public record PaginationParameters
{
public PaginationParameters(int page, int pageSize)
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<TargetFramework>net6.0</TargetFramework>
<LangVersion>latest</LangVersion>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
Expand All @@ -12,18 +12,18 @@
<PackageReference Include="FluentAssertions" Version="6.12.0"/>
<PackageReference Include="Moq" Version="4.20.70"/>

<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.8.0"/>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.9.0" />

<PackageReference Include="xunit" Version="2.6.2"/>
<PackageReference Include="xunit.runner.visualstudio" Version="2.5.4">
<PackageReference Include="xunit" Version="2.8.0" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.8.0">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="coverlet.collector" Version="6.0.0">
<PackageReference Include="coverlet.collector" Version="6.0.2">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="coverlet.msbuild" Version="6.0.0">
<PackageReference Include="coverlet.msbuild" Version="6.0.2">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
Expand Down
7 changes: 5 additions & 2 deletions tests/JacksonVeroneze.NET.Pagination.UnitTests/PageTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,16 @@ public void Initialize_InvalidData_ThrowException()
// -------------------------------------------------------
// Arrange
// -------------------------------------------------------
ICollection<User>? data = null;
IReadOnlyCollection<User>? data = null;
PageInfo pageInfo = new(1, 1, 10);

// -------------------------------------------------------
// Act
// -------------------------------------------------------
Action action = () => new Page<User>(data!, pageInfo);
Action action = () =>
{
Page<User> _ = new (data!, pageInfo);
};

// -------------------------------------------------------
// Assert
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<TargetFramework>net6.0</TargetFramework>
<LangVersion>latest</LangVersion>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Bogus" Version="34.0.2"/>
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="8.0.0"/>
<PackageReference Include="Bogus" Version="35.5.1" />
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="6.0.4" />
<PackageReference Include="Moq" Version="4.20.70"/>
</ItemGroup>

Expand Down

0 comments on commit e5a14cd

Please sign in to comment.