1+ using System ;
12using System . Collections . Generic ;
23using System . Linq ;
34using System . Threading . Tasks ;
45using JsonApiDotNetCore . Data ;
56using JsonApiDotNetCore . Extensions ;
7+ using JsonApiDotNetCore . Internal . Query ;
68using JsonApiDotNetCore . Models ;
79using JsonApiDotNetCore . Services ;
810using 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
0 commit comments