1- using System . Collections ;
21using System . Collections . Generic ;
32using System . Linq ;
43using System . Threading . Tasks ;
@@ -139,9 +138,13 @@ public virtual async Task<IActionResult> PostAsync([FromBody] T entity)
139138 return UnprocessableEntity ( ) ;
140139 }
141140
141+ var stringId = entity . Id . ToString ( ) ;
142+ if ( stringId . Length > 0 && stringId != "0" )
143+ return Forbidden ( ) ;
144+
142145 await _entities . CreateAsync ( entity ) ;
143146
144- return Created ( HttpContext . Request . Path , entity ) ;
147+ return Created ( $ " { HttpContext . Request . Path } / { entity . Id } " , entity ) ;
145148 }
146149
147150 [ HttpPatch ( "{id}" ) ]
@@ -155,9 +158,41 @@ public virtual async Task<IActionResult> PatchAsync(TId id, [FromBody] T entity)
155158
156159 var updatedEntity = await _entities . UpdateAsync ( id , entity ) ;
157160
161+ if ( updatedEntity == null ) return NotFound ( ) ;
162+
158163 return Ok ( updatedEntity ) ;
159164 }
160165
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+
161196 [ HttpDelete ( "{id}" ) ]
162197 public virtual async Task < IActionResult > DeleteAsync ( TId id )
163198 {
0 commit comments