@@ -12,49 +12,83 @@ public class BaseJsonApiController<T, TId>
12
12
: JsonApiControllerMixin
13
13
where T : class , IIdentifiable < TId >
14
14
{
15
- private readonly IResourceQueryService < T , TId > _queryService ;
16
- private readonly IResourceCmdService < T , TId > _cmdService ;
15
+ private readonly IGetAllService < T , TId > _getAll ;
16
+ private readonly IGetByIdService < T , TId > _getById ;
17
+ private readonly IGetRelationshipService < T , TId > _getRelationship ;
18
+ private readonly IGetRelationshipsService < T , TId > _getRelationships ;
19
+ private readonly ICreateService < T , TId > _create ;
20
+ private readonly IUpdateService < T , TId > _update ;
21
+ private readonly IUpdateRelationshipService < T , TId > _updateRelationships ;
22
+ private readonly IDeleteService < T , TId > _delete ;
17
23
private readonly IJsonApiContext _jsonApiContext ;
18
24
19
25
protected BaseJsonApiController (
20
26
IJsonApiContext jsonApiContext ,
21
27
IResourceService < T , TId > resourceService )
22
28
{
23
29
_jsonApiContext = jsonApiContext . ApplyContext < T > ( ) ;
24
- _queryService = resourceService ;
25
- _cmdService = resourceService ;
30
+ _getAll = resourceService ;
31
+ _getById = resourceService ;
32
+ _getRelationship = resourceService ;
33
+ _getRelationships = resourceService ;
34
+ _create = resourceService ;
35
+ _update = resourceService ;
36
+ _updateRelationships = resourceService ;
37
+ _delete = resourceService ;
26
38
}
27
39
28
40
protected BaseJsonApiController (
29
41
IJsonApiContext jsonApiContext ,
30
- IResourceQueryService < T , TId > queryService )
42
+ IResourceQueryService < T , TId > queryService = null ,
43
+ IResourceCmdService < T , TId > cmdService = null )
31
44
{
32
45
_jsonApiContext = jsonApiContext . ApplyContext < T > ( ) ;
33
- _queryService = queryService ;
46
+ _getAll = queryService ;
47
+ _getById = queryService ;
48
+ _getRelationship = queryService ;
49
+ _getRelationships = queryService ;
50
+ _create = cmdService ;
51
+ _update = cmdService ;
52
+ _updateRelationships = cmdService ;
53
+ _delete = cmdService ;
34
54
}
35
55
36
56
protected BaseJsonApiController (
37
57
IJsonApiContext jsonApiContext ,
38
- IResourceCmdService < T , TId > cmdService )
58
+ IGetAllService < T , TId > getAll = null ,
59
+ IGetByIdService < T , TId > getById = null ,
60
+ IGetRelationshipService < T , TId > getRelationship = null ,
61
+ IGetRelationshipsService < T , TId > getRelationships = null ,
62
+ ICreateService < T , TId > create = null ,
63
+ IUpdateService < T , TId > update = null ,
64
+ IUpdateRelationshipService < T , TId > updateRelationships = null ,
65
+ IDeleteService < T , TId > delete = null )
39
66
{
40
67
_jsonApiContext = jsonApiContext . ApplyContext < T > ( ) ;
41
- _cmdService = cmdService ;
68
+ _getAll = getAll ;
69
+ _getById = getById ;
70
+ _getRelationship = getRelationship ;
71
+ _getRelationships = getRelationships ;
72
+ _create = create ;
73
+ _update = update ;
74
+ _updateRelationships = updateRelationships ;
75
+ _delete = delete ;
42
76
}
43
77
44
78
public virtual async Task < IActionResult > GetAsync ( )
45
79
{
46
- if ( _queryService == null ) throw new JsonApiException ( 405 , "Query requests are not supported" ) ;
80
+ if ( _getAll == null ) throw new JsonApiException ( 405 , "Query requests are not supported" ) ;
47
81
48
- var entities = await _queryService . GetAsync ( ) ;
82
+ var entities = await _getAll . GetAsync ( ) ;
49
83
50
84
return Ok ( entities ) ;
51
85
}
52
86
53
87
public virtual async Task < IActionResult > GetAsync ( TId id )
54
88
{
55
- if ( _queryService == null ) throw new JsonApiException ( 405 , "Query requests are not supported" ) ;
89
+ if ( _getById == null ) throw new JsonApiException ( 405 , "Query requests are not supported" ) ;
56
90
57
- var entity = await _queryService . GetAsync ( id ) ;
91
+ var entity = await _getById . GetAsync ( id ) ;
58
92
59
93
if ( entity == null )
60
94
return NotFound ( ) ;
@@ -64,9 +98,9 @@ public virtual async Task<IActionResult> GetAsync(TId id)
64
98
65
99
public virtual async Task < IActionResult > GetRelationshipsAsync ( TId id , string relationshipName )
66
100
{
67
- if ( _queryService == null ) throw new JsonApiException ( 405 , "Query requests are not supported" ) ;
101
+ if ( _getRelationships == null ) throw new JsonApiException ( 405 , "Query requests are not supported" ) ;
68
102
69
- var relationship = await _queryService . GetRelationshipsAsync ( id , relationshipName ) ;
103
+ var relationship = await _getRelationships . GetRelationshipsAsync ( id , relationshipName ) ;
70
104
if ( relationship == null )
71
105
return NotFound ( ) ;
72
106
@@ -75,36 +109,36 @@ public virtual async Task<IActionResult> GetRelationshipsAsync(TId id, string re
75
109
76
110
public virtual async Task < IActionResult > GetRelationshipAsync ( TId id , string relationshipName )
77
111
{
78
- if ( _queryService == null ) throw new JsonApiException ( 405 , "Query requests are not supported" ) ;
112
+ if ( _getRelationship == null ) throw new JsonApiException ( 405 , "Query requests are not supported" ) ;
79
113
80
- var relationship = await _queryService . GetRelationshipAsync ( id , relationshipName ) ;
114
+ var relationship = await _getRelationship . GetRelationshipAsync ( id , relationshipName ) ;
81
115
82
116
return Ok ( relationship ) ;
83
117
}
84
118
85
119
public virtual async Task < IActionResult > PostAsync ( [ FromBody ] T entity )
86
120
{
87
- if ( _cmdService == null ) throw new JsonApiException ( 405 , "Command requests are not supported" ) ;
121
+ if ( _create == null ) throw new JsonApiException ( 405 , "Command requests are not supported" ) ;
88
122
89
123
if ( entity == null )
90
124
return UnprocessableEntity ( ) ;
91
125
92
126
if ( ! _jsonApiContext . Options . AllowClientGeneratedIds && ! string . IsNullOrEmpty ( entity . StringId ) )
93
127
return Forbidden ( ) ;
94
128
95
- entity = await _cmdService . CreateAsync ( entity ) ;
129
+ entity = await _create . CreateAsync ( entity ) ;
96
130
97
131
return Created ( $ "{ HttpContext . Request . Path } /{ entity . Id } ", entity ) ;
98
132
}
99
133
100
134
public virtual async Task < IActionResult > PatchAsync ( TId id , [ FromBody ] T entity )
101
135
{
102
- if ( _cmdService == null ) throw new JsonApiException ( 405 , "Command requests are not supported" ) ;
136
+ if ( _update == null ) throw new JsonApiException ( 405 , "Command requests are not supported" ) ;
103
137
104
138
if ( entity == null )
105
139
return UnprocessableEntity ( ) ;
106
140
107
- var updatedEntity = await _cmdService . UpdateAsync ( id , entity ) ;
141
+ var updatedEntity = await _update . UpdateAsync ( id , entity ) ;
108
142
109
143
if ( updatedEntity == null )
110
144
return NotFound ( ) ;
@@ -114,18 +148,18 @@ public virtual async Task<IActionResult> PatchAsync(TId id, [FromBody] T entity)
114
148
115
149
public virtual async Task < IActionResult > PatchRelationshipsAsync ( TId id , string relationshipName , [ FromBody ] List < DocumentData > relationships )
116
150
{
117
- if ( _cmdService == null ) throw new JsonApiException ( 405 , "Command requests are not supported" ) ;
151
+ if ( _updateRelationships == null ) throw new JsonApiException ( 405 , "Command requests are not supported" ) ;
118
152
119
- await _cmdService . UpdateRelationshipsAsync ( id , relationshipName , relationships ) ;
153
+ await _updateRelationships . UpdateRelationshipsAsync ( id , relationshipName , relationships ) ;
120
154
121
155
return Ok ( ) ;
122
156
}
123
157
124
158
public virtual async Task < IActionResult > DeleteAsync ( TId id )
125
159
{
126
- if ( _cmdService == null ) throw new JsonApiException ( 405 , "Command requests are not supported" ) ;
160
+ if ( _delete == null ) throw new JsonApiException ( 405 , "Command requests are not supported" ) ;
127
161
128
- var wasDeleted = await _cmdService . DeleteAsync ( id ) ;
162
+ var wasDeleted = await _delete . DeleteAsync ( id ) ;
129
163
130
164
if ( ! wasDeleted )
131
165
return NotFound ( ) ;
0 commit comments