Skip to content

Commit b88e4f2

Browse files
committed
fix(paging): X.PagedList does not support .Net Core, pagin done via EF
1 parent dc1fe77 commit b88e4f2

File tree

6 files changed

+41
-28
lines changed

6 files changed

+41
-28
lines changed

src/JsonApiDotNetCore/Controllers/JsonApiController.cs

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
1+
using System;
12
using System.Collections.Generic;
23
using System.Linq;
34
using System.Threading.Tasks;
45
using JsonApiDotNetCore.Data;
56
using JsonApiDotNetCore.Extensions;
7+
using JsonApiDotNetCore.Internal.Query;
68
using JsonApiDotNetCore.Models;
79
using JsonApiDotNetCore.Services;
810
using Microsoft.AspNetCore.Mvc;
@@ -36,6 +38,7 @@ public JsonApiController(
3638
ILoggerFactory loggerFactory)
3739
{
3840
_jsonApiContext = jsonApiContext.ApplyContext<T>();
41+
3942
_entities = entityRepository;
4043

4144
_logger = loggerFactory.CreateLogger<JsonApiController<T, TId>>();
@@ -47,6 +50,7 @@ public JsonApiController(
4750
IJsonApiContext jsonApiContext,
4851
IEntityRepository<T, TId> entityRepository)
4952
{
53+
_jsonApiContext = jsonApiContext.ApplyContext<T>();
5054
_jsonApiContext = jsonApiContext;
5155
_entities = entityRepository;
5256
}
@@ -58,7 +62,7 @@ public virtual async Task<IActionResult> GetAsync()
5862

5963
entities = ApplySortAndFilterQuery(entities);
6064

61-
if (_jsonApiContext.QuerySet != null)
65+
if (_jsonApiContext.QuerySet != null && _jsonApiContext.QuerySet.IncludedRelationships != null && _jsonApiContext.QuerySet.IncludedRelationships.Count > 0)
6266
entities = IncludeRelationships(entities, _jsonApiContext.QuerySet.IncludedRelationships);
6367

6468
// pagination should be done last since it will execute the query
@@ -163,9 +167,11 @@ private IQueryable<T> ApplySortAndFilterQuery(IQueryable<T> entities)
163167
if(_jsonApiContext.QuerySet == null)
164168
return entities;
165169

166-
entities = _entities.Filter(entities, query.Filter);
170+
if(query.Filter != null)
171+
entities = _entities.Filter(entities, query.Filter);
167172

168-
entities = _entities.Sort(entities, query.SortParameters);
173+
if(query.SortParameters != null && query.SortParameters.Count > 0)
174+
entities = _entities.Sort(entities, query.SortParameters);
169175

170176
return entities;
171177
}
@@ -175,10 +181,13 @@ private async Task<IEnumerable<T>> ApplyPageQueryAsync(IQueryable<T> entities)
175181
if(_jsonApiContext.Options.DefaultPageSize == 0 && (_jsonApiContext.QuerySet == null || _jsonApiContext.QuerySet.PageQuery.PageSize == 0))
176182
return entities;
177183

178-
var query = _jsonApiContext.QuerySet.PageQuery;
184+
var query = _jsonApiContext.QuerySet?.PageQuery ?? new PageQuery();
185+
179186
var pageNumber = query.PageOffset > 0 ? query.PageOffset : 1;
180187
var pageSize = query.PageSize > 0 ? query.PageSize : _jsonApiContext.Options.DefaultPageSize;
181188

189+
_logger?.LogInformation($"Applying paging query. Fetching page {pageNumber} with {pageSize} entities");
190+
182191
return await _entities.PageAsync(entities, pageSize, pageNumber);
183192
}
184193

src/JsonApiDotNetCore/Data/DefaultEntityRepository.cs

Lines changed: 8 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 System.Threading.Tasks;
@@ -8,7 +9,6 @@
89
using JsonApiDotNetCore.Services;
910
using Microsoft.EntityFrameworkCore;
1011
using Microsoft.Extensions.Logging;
11-
using X.PagedList;
1212

1313
namespace JsonApiDotNetCore.Data
1414
{
@@ -135,7 +135,13 @@ public IQueryable<TEntity> Include(IQueryable<TEntity> entities, string relation
135135

136136
public async Task<IEnumerable<TEntity>> PageAsync(IQueryable<TEntity> entities, int pageSize, int pageNumber)
137137
{
138-
return await entities.ToPagedListAsync(pageNumber, pageSize);
138+
if(pageSize > 0)
139+
return await entities
140+
.Skip((pageNumber - 1) * pageSize)
141+
.Take(pageSize)
142+
.ToListAsync();
143+
144+
return await entities.ToListAsync();
139145
}
140146
}
141147
}

src/JsonApiDotNetCore/Internal/Query/QuerySet.cs

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,12 @@ public class QuerySet
1111
{
1212
IJsonApiContext _jsonApiContext;
1313

14-
public QuerySet(IJsonApiContext jsonApiContext, IQueryCollection query)
14+
public QuerySet(
15+
IJsonApiContext jsonApiContext,
16+
IQueryCollection query)
1517
{
1618
_jsonApiContext = jsonApiContext;
1719
BuildQuerySet(query);
18-
PageQuery = new PageQuery();
1920
}
2021

2122
public FilterQuery Filter { get; set; }
@@ -44,7 +45,7 @@ private void BuildQuerySet(IQueryCollection query)
4445
IncludedRelationships = ParseIncludedRelationships(pair.Value);
4546
}
4647

47-
if(pair.Key.StartsWith("page"))
48+
if (pair.Key.StartsWith("page"))
4849
{
4950
PageQuery = ParsePageQuery(pair.Key, pair.Value);
5051
}
@@ -57,20 +58,21 @@ private FilterQuery ParseFilterQuery(string key, string value)
5758
var propertyName = key.Split('[', ']')[1];
5859
var attribute = GetAttribute(propertyName);
5960

60-
if(attribute == null)
61+
if (attribute == null)
6162
return null;
62-
63+
6364
return new FilterQuery(attribute, value);
6465
}
6566

6667
private PageQuery ParsePageQuery(string key, string value)
6768
{
6869
// expected input = page[size]=10
6970
// page[number]=1
71+
PageQuery = PageQuery ?? new PageQuery();
7072

7173
var propertyName = key.Split('[', ']')[1];
7274

73-
if(propertyName == "size")
75+
if (propertyName == "size")
7476
PageQuery.PageSize = Convert.ToInt32(value);
7577
else if (propertyName == "number")
7678
PageQuery.PageOffset = Convert.ToInt32(value);
@@ -111,7 +113,7 @@ private List<string> ParseIncludedRelationships(string value)
111113
private AttrAttribute GetAttribute(string propertyName)
112114
{
113115
return _jsonApiContext.RequestEntity.Attributes
114-
.FirstOrDefault(attr =>
116+
.FirstOrDefault(attr =>
115117
attr.InternalAttributeName.ToLower() == propertyName.ToLower()
116118
);
117119
}

src/JsonApiDotNetCore/Middleware/JsonApiExceptionFilter.cs

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,16 +17,17 @@ public JsonApiExceptionFilter(ILoggerFactory loggerFactory)
1717

1818
public void OnException(ExceptionContext context)
1919
{
20-
_logger.LogError(context.Exception.Message);
20+
_logger.LogError(new EventId(), context.Exception, "An unhandled exception occurred during the request");
2121

22-
var jsonApiException = (JsonApiException)context.Exception;
23-
if(jsonApiException != null)
24-
{
25-
var error = jsonApiException.GetError();
26-
var result = new ObjectResult(error);
27-
result.StatusCode = Convert.ToInt16(error.Status);
28-
context.Result = result;
29-
}
22+
var jsonApiException = context.Exception as JsonApiException;
23+
24+
if(jsonApiException == null)
25+
jsonApiException = new JsonApiException("500", context.Exception.Message);
26+
27+
var error = jsonApiException.GetError();
28+
var result = new ObjectResult(error);
29+
result.StatusCode = Convert.ToInt16(error.Status);
30+
context.Result = result;
3031
}
3132
}
3233
}

src/JsonApiDotNetCore/project.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,7 @@
99
"Microsoft.AspNetCore.Routing": "1.1.0",
1010
"Microsoft.AspNetCore.Mvc": "1.1.0",
1111
"Microsoft.EntityFrameworkCore": "1.1.0",
12-
"Microsoft.Extensions.Logging": "1.1.0",
13-
"X.PagedList": "5.3.0.5300"
12+
"Microsoft.Extensions.Logging": "1.1.0"
1413
},
1514

1615
"frameworks": {

src/JsonApiDotNetCoreExample/Startup.cs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,6 @@
77
using JsonApiDotNetCoreExample.Data;
88
using Microsoft.EntityFrameworkCore;
99
using JsonApiDotNetCore.Extensions;
10-
using Microsoft.AspNetCore.Http;
11-
using System.Threading.Tasks;
12-
using System.Reflection;
13-
using System;
1410

1511
namespace JsonApiDotNetCoreExample
1612
{

0 commit comments

Comments
 (0)