13
13
14
14
namespace JsonApiDotNetCore . Controllers
15
15
{
16
- public class JsonApiController < T >
16
+ public class JsonApiController < T >
17
17
: JsonApiController < T , int > where T : class , IIdentifiable < int >
18
18
{
19
19
public JsonApiController (
@@ -24,7 +24,7 @@ public JsonApiController(
24
24
{ }
25
25
}
26
26
27
- public class JsonApiController < T , TId >
27
+ public class JsonApiController < T , TId >
28
28
: JsonApiControllerMixin where T : class , IIdentifiable < TId >
29
29
{
30
30
private readonly IEntityRepository < T , TId > _entities ;
@@ -77,7 +77,7 @@ public virtual async Task<IActionResult> GetAsync()
77
77
public virtual async Task < IActionResult > GetAsync ( TId id )
78
78
{
79
79
T entity ;
80
- if ( _jsonApiContext . QuerySet ? . IncludedRelationships != null )
80
+ if ( _jsonApiContext . QuerySet ? . IncludedRelationships != null )
81
81
entity = await _getWithRelationshipsAsync ( id ) ;
82
82
else
83
83
entity = await _entities . GetAsync ( id ) ;
@@ -138,8 +138,7 @@ public virtual async Task<IActionResult> PostAsync([FromBody] T entity)
138
138
return UnprocessableEntity ( ) ;
139
139
}
140
140
141
- var stringId = entity . Id . ToString ( ) ;
142
- if ( stringId . Length > 0 && stringId != "0" )
141
+ if ( ! _jsonApiContext . Options . AllowClientGeneratedIds && ! string . IsNullOrEmpty ( entity . StringId ) )
143
142
return Forbidden ( ) ;
144
143
145
144
await _entities . CreateAsync ( entity ) ;
@@ -158,7 +157,7 @@ public virtual async Task<IActionResult> PatchAsync(TId id, [FromBody] T entity)
158
157
159
158
var updatedEntity = await _entities . UpdateAsync ( id , entity ) ;
160
159
161
- if ( updatedEntity == null ) return NotFound ( ) ;
160
+ if ( updatedEntity == null ) return NotFound ( ) ;
162
161
163
162
return Ok ( updatedEntity ) ;
164
163
}
@@ -185,12 +184,12 @@ public virtual async Task<IActionResult> PatchRelationshipsAsync(TId id, string
185
184
. Relationships
186
185
. FirstOrDefault ( r => r . InternalRelationshipName == relationshipName ) ;
187
186
188
- var relationshipIds = relationships . Select ( r=> r . Id ) ;
189
-
187
+ var relationshipIds = relationships . Select ( r => r . Id ) ;
188
+
190
189
await _entities . UpdateRelationshipsAsync ( entity , relationship , relationshipIds ) ;
191
190
192
191
return Ok ( ) ;
193
-
192
+
194
193
}
195
194
196
195
[ HttpDelete ( "{id}" ) ]
@@ -208,14 +207,14 @@ private IQueryable<T> ApplySortAndFilterQuery(IQueryable<T> entities)
208
207
{
209
208
var query = _jsonApiContext . QuerySet ;
210
209
211
- if ( _jsonApiContext . QuerySet == null )
210
+ if ( _jsonApiContext . QuerySet == null )
212
211
return entities ;
213
212
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 )
216
215
entities = _entities . Filter ( entities , filter ) ;
217
216
218
- if ( query . SortParameters != null && query . SortParameters . Count > 0 )
217
+ if ( query . SortParameters != null && query . SortParameters . Count > 0 )
219
218
entities = _entities . Sort ( entities , query . SortParameters ) ;
220
219
221
220
return entities ;
@@ -224,7 +223,7 @@ private IQueryable<T> ApplySortAndFilterQuery(IQueryable<T> entities)
224
223
private async Task < IEnumerable < T > > ApplyPageQueryAsync ( IQueryable < T > entities )
225
224
{
226
225
var pageManager = _jsonApiContext . PageManager ;
227
- if ( ! pageManager . IsPaginated )
226
+ if ( ! pageManager . IsPaginated )
228
227
return entities ;
229
228
230
229
var query = _jsonApiContext . QuerySet ? . PageQuery ?? new PageQuery ( ) ;
@@ -238,7 +237,7 @@ private IQueryable<T> IncludeRelationships(IQueryable<T> entities, List<string>
238
237
{
239
238
_jsonApiContext . IncludedRelationships = relationships ;
240
239
241
- foreach ( var r in relationships )
240
+ foreach ( var r in relationships )
242
241
entities = _entities . Include ( entities , r . ToProperCase ( ) ) ;
243
242
244
243
return entities ;
0 commit comments