1
- using System . Collections ;
2
1
using System . Collections . Generic ;
3
2
using System . Linq ;
4
3
using System . Threading . Tasks ;
@@ -139,9 +138,13 @@ public virtual async Task<IActionResult> PostAsync([FromBody] T entity)
139
138
return UnprocessableEntity ( ) ;
140
139
}
141
140
141
+ var stringId = entity . Id . ToString ( ) ;
142
+ if ( stringId . Length > 0 && stringId != "0" )
143
+ return Forbidden ( ) ;
144
+
142
145
await _entities . CreateAsync ( entity ) ;
143
146
144
- return Created ( HttpContext . Request . Path , entity ) ;
147
+ return Created ( $ " { HttpContext . Request . Path } / { entity . Id } " , entity ) ;
145
148
}
146
149
147
150
[ HttpPatch ( "{id}" ) ]
@@ -155,9 +158,41 @@ public virtual async Task<IActionResult> PatchAsync(TId id, [FromBody] T entity)
155
158
156
159
var updatedEntity = await _entities . UpdateAsync ( id , entity ) ;
157
160
161
+ if ( updatedEntity == null ) return NotFound ( ) ;
162
+
158
163
return Ok ( updatedEntity ) ;
159
164
}
160
165
166
+ [ HttpPatch ( "{id}/relationships/{relationshipName}" ) ]
167
+ public virtual async Task < IActionResult > PatchRelationshipsAsync ( TId id , string relationshipName , [ FromBody ] List < DocumentData > relationships )
168
+ {
169
+ relationshipName = _jsonApiContext . ContextGraph
170
+ . GetRelationshipName < T > ( relationshipName . ToProperCase ( ) ) ;
171
+
172
+ if ( relationshipName == null )
173
+ {
174
+ _logger ? . LogInformation ( $ "Relationship name not specified returning 422") ;
175
+ return UnprocessableEntity ( ) ;
176
+ }
177
+
178
+ var entity = await _entities . GetAndIncludeAsync ( id , relationshipName ) ;
179
+
180
+ if ( entity == null )
181
+ return NotFound ( ) ;
182
+
183
+ var relationship = _jsonApiContext . ContextGraph
184
+ . GetContextEntity ( typeof ( T ) )
185
+ . Relationships
186
+ . FirstOrDefault ( r => r . RelationshipName == relationshipName ) ;
187
+
188
+ var relationshipIds = relationships . Select ( r=> r . Id ) ;
189
+
190
+ await _entities . UpdateRelationshipsAsync ( entity , relationship , relationshipIds ) ;
191
+
192
+ return Ok ( ) ;
193
+
194
+ }
195
+
161
196
[ HttpDelete ( "{id}" ) ]
162
197
public virtual async Task < IActionResult > DeleteAsync ( TId id )
163
198
{
0 commit comments