1
1
using System . Collections . Generic ;
2
+ using System . Net ;
2
3
using System . Threading . Tasks ;
3
4
using JsonApiDotNetCore . Configuration ;
4
5
using JsonApiDotNetCore . Controllers ;
6
+ using JsonApiDotNetCore . Exceptions ;
5
7
using JsonApiDotNetCore . Models ;
6
8
using JsonApiDotNetCore . Services ;
7
9
using JsonApiDotNetCoreExample . Models ;
8
10
using Microsoft . AspNetCore . Mvc ;
9
- using Microsoft . Extensions . Logging ;
10
11
11
12
namespace JsonApiDotNetCoreExample . Controllers
12
13
{
14
+ [ ApiController ]
13
15
[ DisableRoutingConvention , Route ( "custom/route/todoItems" ) ]
14
16
public class TodoItemsCustomController : CustomJsonApiController < TodoItem >
15
17
{
16
18
public TodoItemsCustomController (
17
19
IJsonApiOptions options ,
18
- IResourceService < TodoItem > resourceService ,
19
- ILoggerFactory loggerFactory )
20
- : base ( options , resourceService , loggerFactory )
20
+ IResourceService < TodoItem > resourceService )
21
+ : base ( options , resourceService )
21
22
{ }
22
23
}
23
24
@@ -26,8 +27,7 @@ public class CustomJsonApiController<T>
26
27
{
27
28
public CustomJsonApiController (
28
29
IJsonApiOptions options ,
29
- IResourceService < T , int > resourceService ,
30
- ILoggerFactory loggerFactory )
30
+ IResourceService < T , int > resourceService )
31
31
: base ( options , resourceService )
32
32
{
33
33
}
@@ -41,7 +41,7 @@ public class CustomJsonApiController<T, TId>
41
41
42
42
private IActionResult Forbidden ( )
43
43
{
44
- return new StatusCodeResult ( 403 ) ;
44
+ return new StatusCodeResult ( ( int ) HttpStatusCode . Forbidden ) ;
45
45
}
46
46
47
47
public CustomJsonApiController (
@@ -68,22 +68,29 @@ public async Task<IActionResult> GetAsync()
68
68
[ HttpGet ( "{id}" ) ]
69
69
public async Task < IActionResult > GetAsync ( TId id )
70
70
{
71
- var entity = await _resourceService . GetAsync ( id ) ;
72
-
73
- if ( entity == null )
71
+ try
72
+ {
73
+ var entity = await _resourceService . GetAsync ( id ) ;
74
+ return Ok ( entity ) ;
75
+ }
76
+ catch ( ResourceNotFoundException )
77
+ {
74
78
return NotFound ( ) ;
75
-
76
- return Ok ( entity ) ;
79
+ }
77
80
}
78
81
79
82
[ HttpGet ( "{id}/relationships/{relationshipName}" ) ]
80
83
public async Task < IActionResult > GetRelationshipsAsync ( TId id , string relationshipName )
81
84
{
82
- var relationship = _resourceService . GetRelationshipAsync ( id , relationshipName ) ;
83
- if ( relationship == null )
85
+ try
86
+ {
87
+ var relationship = await _resourceService . GetRelationshipsAsync ( id , relationshipName ) ;
88
+ return Ok ( relationship ) ;
89
+ }
90
+ catch ( ResourceNotFoundException )
91
+ {
84
92
return NotFound ( ) ;
85
-
86
- return await GetRelationshipAsync ( id , relationshipName ) ;
93
+ }
87
94
}
88
95
89
96
[ HttpGet ( "{id}/{relationshipName}" ) ]
@@ -113,12 +120,15 @@ public async Task<IActionResult> PatchAsync(TId id, [FromBody] T entity)
113
120
if ( entity == null )
114
121
return UnprocessableEntity ( ) ;
115
122
116
- var updatedEntity = await _resourceService . UpdateAsync ( id , entity ) ;
117
-
118
- if ( updatedEntity == null )
123
+ try
124
+ {
125
+ var updatedEntity = await _resourceService . UpdateAsync ( id , entity ) ;
126
+ return Ok ( updatedEntity ) ;
127
+ }
128
+ catch ( ResourceNotFoundException )
129
+ {
119
130
return NotFound ( ) ;
120
-
121
- return Ok ( updatedEntity ) ;
131
+ }
122
132
}
123
133
124
134
[ HttpPatch ( "{id}/relationships/{relationshipName}" ) ]
@@ -131,11 +141,7 @@ public async Task<IActionResult> PatchRelationshipsAsync(TId id, string relation
131
141
[ HttpDelete ( "{id}" ) ]
132
142
public async Task < IActionResult > DeleteAsync ( TId id )
133
143
{
134
- var wasDeleted = await _resourceService . DeleteAsync ( id ) ;
135
-
136
- if ( ! wasDeleted )
137
- return NotFound ( ) ;
138
-
144
+ await _resourceService . DeleteAsync ( id ) ;
139
145
return NoContent ( ) ;
140
146
}
141
147
}
0 commit comments