Skip to content

fix: #641 #642

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 28, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,17 @@ namespace JsonApiDotNetCore.Query
public interface IPageService : IQueryParameterService
{
/// <summary>
/// What the total records are for this output
/// Gets the requested or default page size
/// </summary>
int? TotalRecords { get; set; }
int CurrentPageSize { get; }
/// <summary>
/// Default size to be used in pagination
/// </summary>
int DefaultPageSize { get; set; }
/// <summary>
/// How many records per page should be shown
/// Currently requested page size to be used in pagination
/// </summary>
int PageSize { get; set; }
int? RequestedPageSize { get; set; }
/// <summary>
/// The page requested. Note that the page number is one-based.
/// </summary>
Expand All @@ -29,5 +33,9 @@ public interface IPageService : IQueryParameterService
/// Denotes if pagination is backwards
/// </summary>
bool Backwards { get; }
/// <summary>
/// What the total records are for this output
/// </summary>
int? TotalRecords { get; set; }
}
}
31 changes: 23 additions & 8 deletions src/JsonApiDotNetCore/QueryParameterServices/PageService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,10 @@ namespace JsonApiDotNetCore.Query
public class PageService : QueryParameterService, IPageService
{
private readonly IJsonApiOptions _options;

public PageService(IJsonApiOptions options, IResourceGraph resourceGraph, ICurrentRequest currentRequest) : base(resourceGraph, currentRequest)
{
_options = options;
PageSize = _options.DefaultPageSize;
DefaultPageSize = _options.DefaultPageSize;
}

/// <summary>
Expand All @@ -26,14 +25,27 @@ public PageService(IJsonApiOptions options, IResourceGraph resourceGraph, ICurre
internal PageService(IJsonApiOptions options)
{
_options = options;
PageSize = _options.DefaultPageSize;
DefaultPageSize = _options.DefaultPageSize;
}

/// <inheritdoc/>
public int? TotalRecords { get; set; }
public int CurrentPageSize
{
get
{
if (RequestedPageSize.HasValue)
{
return RequestedPageSize.Value;
}
return DefaultPageSize;
}
}

/// <inheritdoc/>
public int DefaultPageSize { get; set; }

/// <inheritdoc/>
public int PageSize { get; set; }
public int? RequestedPageSize { get; set; }

/// <inheritdoc/>
public int CurrentPage { get; set; } = 1;
Expand All @@ -42,17 +54,20 @@ internal PageService(IJsonApiOptions options)
public bool Backwards { get; set; }

/// <inheritdoc/>
public int TotalPages => (TotalRecords == null || PageSize == 0) ? -1 : (int)Math.Ceiling(decimal.Divide(TotalRecords.Value, PageSize));
public int TotalPages => (TotalRecords == null || CurrentPageSize == 0) ? -1 : (int)Math.Ceiling(decimal.Divide(TotalRecords.Value, CurrentPageSize));

/// <inheritdoc/>
public bool CanPaginate { get { return TotalPages > 1; } }

/// <inheritdoc/>
public int? TotalRecords { get; set; }

/// <inheritdoc/>
public virtual void Parse(KeyValuePair<string, StringValues> queryParameter)
{
EnsureNoNestedResourceRoute();
// expected input = page[size]=<integer>
// page[number]=<integer > 0>
// page[number]=<integer greater than zero>
var propertyName = queryParameter.Key.Split(QueryConstants.OPEN_BRACKET, QueryConstants.CLOSE_BRACKET)[1];

const string SIZE = "size";
Expand All @@ -70,7 +85,7 @@ public virtual void Parse(KeyValuePair<string, StringValues> queryParameter)
}
else
{
PageSize = size;
RequestedPageSize = size;
}
}
else if (propertyName == NUMBER)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,19 +63,19 @@ private void SetPageLinks(ResourceContext primaryResource, TopLevelLinks links)
{
if (_pageService.CurrentPage > 1)
{
links.Prev = GetPageLink(primaryResource, _pageService.CurrentPage - 1, _pageService.PageSize);
links.Prev = GetPageLink(primaryResource, _pageService.CurrentPage - 1, _pageService.CurrentPageSize);
}

if (_pageService.CurrentPage < _pageService.TotalPages)
{
links.Next = GetPageLink(primaryResource, _pageService.CurrentPage + 1, _pageService.PageSize);
links.Next = GetPageLink(primaryResource, _pageService.CurrentPage + 1, _pageService.CurrentPageSize);
}

if (_pageService.TotalPages > 0)
{
links.Self = GetPageLink(primaryResource, _pageService.CurrentPage, _pageService.PageSize);
links.First = GetPageLink(primaryResource, 1, _pageService.PageSize);
links.Last = GetPageLink(primaryResource, _pageService.TotalPages, _pageService.PageSize);
links.Self = GetPageLink(primaryResource, _pageService.CurrentPage, _pageService.CurrentPageSize);
links.First = GetPageLink(primaryResource, 1, _pageService.CurrentPageSize);
links.Last = GetPageLink(primaryResource, _pageService.TotalPages, _pageService.CurrentPageSize);
}
}

Expand Down
6 changes: 3 additions & 3 deletions src/JsonApiDotNetCore/Services/DefaultResourceService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ public virtual async Task UpdateRelationshipsAsync(TId id, string relationshipNa

protected virtual async Task<IEnumerable<TResource>> ApplyPageQueryAsync(IQueryable<TResource> entities)
{
if (!(_pageService.PageSize > 0))
if (!(_pageService.CurrentPageSize > 0))
{
var allEntities = await _repository.ToListAsync(entities);
return allEntities as IEnumerable<TResource>;
Expand All @@ -214,10 +214,10 @@ protected virtual async Task<IEnumerable<TResource>> ApplyPageQueryAsync(IQuerya
if (_logger?.IsEnabled(LogLevel.Information) == true)
{
_logger?.LogInformation($"Applying paging query. Fetching page {pageOffset} " +
$"with {_pageService.PageSize} entities");
$"with {_pageService.CurrentPageSize} entities");
}

return await _repository.PageAsync(entities, _pageService.PageSize, pageOffset);
return await _repository.PageAsync(entities, _pageService.CurrentPageSize, pageOffset);
}

/// <summary>
Expand Down
2 changes: 1 addition & 1 deletion test/UnitTests/Builders/LinkBuilderTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ private IPageService GetPageManager()
mock.Setup(m => m.CanPaginate).Returns(true);
mock.Setup(m => m.CurrentPage).Returns(2);
mock.Setup(m => m.TotalPages).Returns(3);
mock.Setup(m => m.PageSize).Returns(10);
mock.Setup(m => m.CurrentPageSize).Returns(10);
return mock.Object;

}
Expand Down
2 changes: 1 addition & 1 deletion test/UnitTests/QueryParameters/PageServiceTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public void Parse_PageSize_CanParse(string value, int expectedValue, bool should
else
{
service.Parse(query);
Assert.Equal(expectedValue, service.PageSize);
Assert.Equal(expectedValue, service.CurrentPageSize);
}
}

Expand Down