Skip to content

Commit 5dcd131

Browse files
committed
feat(controller): unprocessable entity method
1 parent 88f613e commit 5dcd131

File tree

2 files changed

+10
-5
lines changed

2 files changed

+10
-5
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@
1111
- [x] Fetching relationships
1212
- [x] Creating relationships
1313
- [x] Refactor relationships links to use an POCO that contains data and links objects
14-
- [ ] Include Entities
15-
- [ ] BadRequest should be 422 in POST
14+
- [x] Include Entities
15+
- [x] BadRequest should be 422 in POST
1616
- [ ] Add integration tests to example project
1717
- [ ] Add logging
1818
- [ ] Configure Different Repositories

src/JsonApiDotNetCore/Controllers/JsonApiController.cs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ public virtual async Task<IActionResult> GetRelationshipAsync(TId id, string rel
105105
.GetRelationshipName<T>(relationshipName);
106106

107107
if (relationshipName == null)
108-
return NotFound();
108+
return UnprocessableEntity();
109109

110110
var entity = await _entities.GetAndIncludeAsync(id, relationshipName);
111111

@@ -125,7 +125,7 @@ public virtual async Task<IActionResult> GetRelationshipAsync(TId id, string rel
125125
public virtual async Task<IActionResult> PostAsync([FromBody] T entity)
126126
{
127127
if (entity == null)
128-
return BadRequest();
128+
return UnprocessableEntity();
129129

130130
await _entities.CreateAsync(entity);
131131

@@ -136,7 +136,7 @@ public virtual async Task<IActionResult> PostAsync([FromBody] T entity)
136136
public virtual async Task<IActionResult> PatchAsync(TId id, [FromBody] T entity)
137137
{
138138
if (entity == null)
139-
return BadRequest();
139+
return UnprocessableEntity();
140140

141141
var updatedEntity = await _entities.UpdateAsync(id, entity);
142142

@@ -189,5 +189,10 @@ private IQueryable<T> IncludeRelationships(IQueryable<T> entities, List<string>
189189

190190
return entities;
191191
}
192+
193+
protected IActionResult UnprocessableEntity()
194+
{
195+
return new StatusCodeResult(422);
196+
}
192197
}
193198
}

0 commit comments

Comments
 (0)