Skip to content

Commit cce3149

Browse files
committed
feat(*): add page manager to hold pagination details
1 parent c729913 commit cce3149

File tree

5 files changed

+34
-11
lines changed

5 files changed

+34
-11
lines changed

src/JsonApiDotNetCore/Builders/DocumentBuilder.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ private Dictionary<string, object> _getMeta(IIdentifiable entity)
6868
meta = metaEntity.GetMeta(_jsonApiContext);
6969

7070
if(_jsonApiContext.Options.IncludeTotalRecordCount)
71-
meta["total-records"] = _jsonApiContext.TotalRecords;
71+
meta["total-records"] = _jsonApiContext.PageManager.TotalRecords;
7272

7373
if(meta.Count > 0) return meta;
7474
return null;

src/JsonApiDotNetCore/Controllers/JsonApiController.cs

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ public virtual async Task<IActionResult> GetAsync()
6666
entities = IncludeRelationships(entities, _jsonApiContext.QuerySet.IncludedRelationships);
6767

6868
if (_jsonApiContext.Options.IncludeTotalRecordCount)
69-
_jsonApiContext.TotalRecords = await entities.CountAsync();
69+
_jsonApiContext.PageManager.TotalRecords = await entities.CountAsync();
7070

7171
// pagination should be done last since it will execute the query
7272
var pagedEntities = await ApplyPageQueryAsync(entities);
@@ -188,17 +188,15 @@ private IQueryable<T> ApplySortAndFilterQuery(IQueryable<T> entities)
188188

189189
private async Task<IEnumerable<T>> ApplyPageQueryAsync(IQueryable<T> entities)
190190
{
191-
if(_jsonApiContext.Options.DefaultPageSize == 0 && (_jsonApiContext.QuerySet == null || _jsonApiContext.QuerySet.PageQuery.PageSize == 0))
191+
var pageManager = _jsonApiContext.PageManager;
192+
if(!pageManager.IsPaginated)
192193
return entities;
193194

194195
var query = _jsonApiContext.QuerySet?.PageQuery ?? new PageQuery();
195-
196-
var pageNumber = query.PageOffset > 0 ? query.PageOffset : 1;
197-
var pageSize = query.PageSize > 0 ? query.PageSize : _jsonApiContext.Options.DefaultPageSize;
198196

199-
_logger?.LogInformation($"Applying paging query. Fetching page {pageNumber} with {pageSize} entities");
197+
_logger?.LogInformation($"Applying paging query. Fetching page {pageManager.CurrentPage} with {pageManager.PageSize} entities");
200198

201-
return await _entities.PageAsync(entities, pageSize, pageNumber);
199+
return await _entities.PageAsync(entities, pageManager.PageSize, pageManager.CurrentPage);
202200
}
203201

204202
private IQueryable<T> IncludeRelationships(IQueryable<T> entities, List<string> relationships)
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
namespace JsonApiDotNetCore.Internal
2+
{
3+
public class PageManager
4+
{
5+
public int TotalRecords { get; set; }
6+
public int PageSize { get; set; }
7+
public int CurrentPage { get; set; }
8+
public bool IsPaginated { get { return PageSize > 0; } }
9+
}
10+
}

src/JsonApiDotNetCore/Services/IJsonApiContext.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,6 @@ public interface IJsonApiContext
1515
QuerySet QuerySet { get; set; }
1616
bool IsRelationshipData { get; set; }
1717
List<string> IncludedRelationships { get; set; }
18-
int TotalRecords { get; set; }
18+
PageManager PageManager { get; set; }
1919
}
2020
}

src/JsonApiDotNetCore/Services/JsonApiContext.cs

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
using System;
12
using System.Collections.Generic;
23
using System.Linq;
34
using JsonApiDotNetCore.Builders;
@@ -28,7 +29,7 @@ public JsonApiContext(
2829
public QuerySet QuerySet { get; set; }
2930
public bool IsRelationshipData { get; set; }
3031
public List<string> IncludedRelationships { get; set; }
31-
public int TotalRecords { get; set; }
32+
public PageManager PageManager { get; set; }
3233

3334
public IJsonApiContext ApplyContext<T>()
3435
{
@@ -40,12 +41,26 @@ public IJsonApiContext ApplyContext<T>()
4041
{
4142
QuerySet = new QuerySet(this, context.Request.Query);
4243
IncludedRelationships = QuerySet.IncludedRelationships;
43-
}
44+
}
4445

4546
var linkBuilder = new LinkBuilder(this);
4647
BasePath = linkBuilder.GetBasePath(context, RequestEntity.EntityName);
48+
PageManager = GetPageManager();
4749

4850
return this;
4951
}
52+
53+
private PageManager GetPageManager()
54+
{
55+
if(Options.DefaultPageSize == 0 && (QuerySet == null || QuerySet.PageQuery.PageSize == 0))
56+
return new PageManager();
57+
58+
var query = QuerySet?.PageQuery ?? new PageQuery();
59+
60+
return new PageManager {
61+
CurrentPage = query.PageOffset > 0 ? query.PageOffset : 1,
62+
PageSize = query.PageSize > 0 ? query.PageSize : Options.DefaultPageSize
63+
};
64+
}
5065
}
5166
}

0 commit comments

Comments
 (0)