1313
1414namespace JsonApiDotNetCore . Controllers
1515{
16- public class JsonApiController < T >
16+ public class JsonApiController < T >
1717 : JsonApiController < T , int > where T : class , IIdentifiable < int >
1818 {
1919 public JsonApiController (
@@ -24,7 +24,7 @@ public JsonApiController(
2424 { }
2525 }
2626
27- public class JsonApiController < T , TId >
27+ public class JsonApiController < T , TId >
2828 : JsonApiControllerMixin where T : class , IIdentifiable < TId >
2929 {
3030 private readonly IEntityRepository < T , TId > _entities ;
@@ -77,7 +77,7 @@ public virtual async Task<IActionResult> GetAsync()
7777 public virtual async Task < IActionResult > GetAsync ( TId id )
7878 {
7979 T entity ;
80- if ( _jsonApiContext . QuerySet ? . IncludedRelationships != null )
80+ if ( _jsonApiContext . QuerySet ? . IncludedRelationships != null )
8181 entity = await _getWithRelationshipsAsync ( id ) ;
8282 else
8383 entity = await _entities . GetAsync ( id ) ;
@@ -138,8 +138,7 @@ public virtual async Task<IActionResult> PostAsync([FromBody] T entity)
138138 return UnprocessableEntity ( ) ;
139139 }
140140
141- var stringId = entity . Id . ToString ( ) ;
142- if ( stringId . Length > 0 && stringId != "0" )
141+ if ( ! _jsonApiContext . Options . AllowClientGeneratedIds && ! string . IsNullOrEmpty ( entity . StringId ) )
143142 return Forbidden ( ) ;
144143
145144 await _entities . CreateAsync ( entity ) ;
@@ -158,7 +157,7 @@ public virtual async Task<IActionResult> PatchAsync(TId id, [FromBody] T entity)
158157
159158 var updatedEntity = await _entities . UpdateAsync ( id , entity ) ;
160159
161- if ( updatedEntity == null ) return NotFound ( ) ;
160+ if ( updatedEntity == null ) return NotFound ( ) ;
162161
163162 return Ok ( updatedEntity ) ;
164163 }
@@ -185,12 +184,12 @@ public virtual async Task<IActionResult> PatchRelationshipsAsync(TId id, string
185184 . Relationships
186185 . FirstOrDefault ( r => r . InternalRelationshipName == relationshipName ) ;
187186
188- var relationshipIds = relationships . Select ( r=> r . Id ) ;
189-
187+ var relationshipIds = relationships . Select ( r => r . Id ) ;
188+
190189 await _entities . UpdateRelationshipsAsync ( entity , relationship , relationshipIds ) ;
191190
192191 return Ok ( ) ;
193-
192+
194193 }
195194
196195 [ HttpDelete ( "{id}" ) ]
@@ -208,14 +207,14 @@ private IQueryable<T> ApplySortAndFilterQuery(IQueryable<T> entities)
208207 {
209208 var query = _jsonApiContext . QuerySet ;
210209
211- if ( _jsonApiContext . QuerySet == null )
210+ if ( _jsonApiContext . QuerySet == null )
212211 return entities ;
213212
214- if ( query . Filters . Count > 0 )
215- foreach ( var filter in query . Filters )
213+ if ( query . Filters . Count > 0 )
214+ foreach ( var filter in query . Filters )
216215 entities = _entities . Filter ( entities , filter ) ;
217216
218- if ( query . SortParameters != null && query . SortParameters . Count > 0 )
217+ if ( query . SortParameters != null && query . SortParameters . Count > 0 )
219218 entities = _entities . Sort ( entities , query . SortParameters ) ;
220219
221220 return entities ;
@@ -224,7 +223,7 @@ private IQueryable<T> ApplySortAndFilterQuery(IQueryable<T> entities)
224223 private async Task < IEnumerable < T > > ApplyPageQueryAsync ( IQueryable < T > entities )
225224 {
226225 var pageManager = _jsonApiContext . PageManager ;
227- if ( ! pageManager . IsPaginated )
226+ if ( ! pageManager . IsPaginated )
228227 return entities ;
229228
230229 var query = _jsonApiContext . QuerySet ? . PageQuery ?? new PageQuery ( ) ;
@@ -238,7 +237,7 @@ private IQueryable<T> IncludeRelationships(IQueryable<T> entities, List<string>
238237 {
239238 _jsonApiContext . IncludedRelationships = relationships ;
240239
241- foreach ( var r in relationships )
240+ foreach ( var r in relationships )
242241 entities = _entities . Include ( entities , r . ToProperCase ( ) ) ;
243242
244243 return entities ;
0 commit comments