1
1
import 'dart:async' ;
2
2
3
+ import 'package:json_api/document.dart' ;
3
4
import 'package:json_api/server.dart' ;
4
- import 'package:json_api/src/document/identifier.dart' ;
5
- import 'package:json_api/src/document/json_api_error.dart' ;
6
- import 'package:json_api/src/document/resource.dart' ;
7
- import 'package:json_api/src/pagination/pagination.dart' ;
8
5
import 'package:uuid/uuid.dart' ;
9
6
10
7
import 'dao.dart' ;
@@ -18,26 +15,27 @@ class CarsController implements Controller {
18
15
CarsController (this ._dao, this ._pagination);
19
16
20
17
@override
21
- Response fetchCollection (CollectionTarget target , Query query) {
22
- final dao = _getDaoOrThrow (target );
18
+ Response fetchCollection (String type , Query query) {
19
+ final dao = _getDaoOrThrow (type );
23
20
final collection = dao.fetchCollection (
24
21
_pagination.limit (query.page), _pagination.offset (query.page));
25
22
return CollectionResponse (collection.elements.map (dao.toResource),
26
23
included: const [], total: collection.totalCount);
27
24
}
28
25
29
26
@override
30
- Response fetchRelated (RelationshipTarget target, Query query) {
31
- final res = _fetchResourceOrThrow (target);
27
+ Response fetchRelated (
28
+ String type, String id, String relationship, Query query) {
29
+ final res = _fetchResourceOrThrow (type, id);
32
30
33
- if (res.toOne.containsKey (target. relationship)) {
34
- final id = res.toOne[target. relationship];
31
+ if (res.toOne.containsKey (relationship)) {
32
+ final id = res.toOne[relationship];
35
33
final resource = _dao[id.type].fetchByIdAsResource (id.id);
36
34
return RelatedResourceResponse (resource);
37
35
}
38
36
39
- if (res.toMany.containsKey (target. relationship)) {
40
- final relationships = res.toMany[target. relationship];
37
+ if (res.toMany.containsKey (relationship)) {
38
+ final relationships = res.toMany[relationship];
41
39
final resources = relationships
42
40
.skip (_pagination.offset (query.page))
43
41
.take (_pagination.limit (query.page))
@@ -50,10 +48,10 @@ class CarsController implements Controller {
50
48
}
51
49
52
50
@override
53
- Response fetchResource (ResourceTarget target , Query query) {
54
- final dao = _getDaoOrThrow (target );
51
+ Response fetchResource (String type, String id , Query query) {
52
+ final dao = _getDaoOrThrow (type );
55
53
56
- final obj = dao.fetchById (target. id);
54
+ final obj = dao.fetchById (id);
57
55
58
56
if (obj == null ) {
59
57
return ErrorResponse .notFound (
@@ -74,43 +72,42 @@ class CarsController implements Controller {
74
72
}
75
73
76
74
@override
77
- Response fetchRelationship (RelationshipTarget target, Query query) {
78
- final res = _fetchResourceOrThrow (target);
75
+ Response fetchRelationship (
76
+ String type, String id, String relationship, Query query) {
77
+ final res = _fetchResourceOrThrow (type, id);
79
78
80
- if (res.toOne.containsKey (target.relationship)) {
81
- final id = res.toOne[target.relationship];
82
- return ToOneResponse (target, id);
79
+ if (res.toOne.containsKey (relationship)) {
80
+ return ToOneResponse (type, id, relationship, res.toOne[relationship]);
83
81
}
84
82
85
- if (res.toMany.containsKey (target.relationship)) {
86
- final ids = res.toMany[target.relationship];
87
- return ToManyResponse (target, ids);
83
+ if (res.toMany.containsKey (relationship)) {
84
+ return ToManyResponse (type, id, relationship, res.toMany[relationship]);
88
85
}
89
86
return ErrorResponse .notFound (
90
87
[JsonApiError (detail: 'Relationship not found' )]);
91
88
}
92
89
93
90
@override
94
- Response deleteResource (ResourceTarget target ) {
95
- final dao = _getDaoOrThrow (target );
91
+ Response deleteResource (String type, String id ) {
92
+ final dao = _getDaoOrThrow (type );
96
93
97
- final res = dao.fetchByIdAsResource (target. id);
94
+ final res = dao.fetchByIdAsResource (id);
98
95
if (res == null ) {
99
96
throw ErrorResponse .notFound (
100
97
[JsonApiError (detail: 'Resource not found' )]);
101
98
}
102
- final dependenciesCount = dao.deleteById (target. id);
99
+ final dependenciesCount = dao.deleteById (id);
103
100
if (dependenciesCount == 0 ) {
104
101
return NoContentResponse ();
105
102
}
106
103
return MetaResponse ({'dependenciesCount' : dependenciesCount});
107
104
}
108
105
109
106
@override
110
- Response createResource (CollectionTarget target , Resource resource) {
111
- final dao = _getDaoOrThrow (target );
107
+ Response createResource (String type , Resource resource) {
108
+ final dao = _getDaoOrThrow (type );
112
109
113
- _throwIfIncompatibleTypes (target , resource);
110
+ _throwIfIncompatibleTypes (type , resource);
114
111
115
112
if (resource.id != null ) {
116
113
if (dao.fetchById (resource.id) != null ) {
@@ -126,7 +123,7 @@ class CarsController implements Controller {
126
123
toMany: resource.toMany,
127
124
toOne: resource.toOne));
128
125
129
- if (target. type == 'models' ) {
126
+ if (type == 'models' ) {
130
127
// Insertion is artificially delayed
131
128
final job = Job (Future .delayed (Duration (milliseconds: 100 ), () {
132
129
dao.insert (created);
@@ -142,62 +139,64 @@ class CarsController implements Controller {
142
139
}
143
140
144
141
@override
145
- Response updateResource (ResourceTarget target , Resource resource) {
146
- final dao = _getDaoOrThrow (target );
142
+ Response updateResource (String type, String id , Resource resource) {
143
+ final dao = _getDaoOrThrow (type );
147
144
148
- _throwIfIncompatibleTypes (target , resource);
149
- if (dao.fetchById (target. id) == null ) {
145
+ _throwIfIncompatibleTypes (type , resource);
146
+ if (dao.fetchById (id) == null ) {
150
147
return ErrorResponse .notFound (
151
148
[JsonApiError (detail: 'Resource not found' )]);
152
149
}
153
- final updated = dao.update (target. id, resource);
150
+ final updated = dao.update (id, resource);
154
151
if (updated == null ) {
155
152
return NoContentResponse ();
156
153
}
157
154
return ResourceUpdatedResponse (updated);
158
155
}
159
156
160
157
@override
161
- Response replaceToOne (RelationshipTarget target, Identifier identifier) {
162
- final dao = _getDaoOrThrow (target);
158
+ Response replaceToOne (
159
+ String type, String id, String relationship, Identifier identifier) {
160
+ final dao = _getDaoOrThrow (type);
163
161
164
- dao.replaceToOne (target. id, target. relationship, identifier);
162
+ dao.replaceToOne (id, relationship, identifier);
165
163
return NoContentResponse ();
166
164
}
167
165
168
166
@override
169
- Response replaceToMany (
170
- RelationshipTarget target, List <Identifier > identifiers) {
171
- final dao = _getDaoOrThrow (target );
167
+ Response replaceToMany (String type, String id, String relationship,
168
+ List <Identifier > identifiers) {
169
+ final dao = _getDaoOrThrow (type );
172
170
173
- dao.replaceToMany (target. id, target. relationship, identifiers);
171
+ dao.replaceToMany (id, relationship, identifiers);
174
172
return NoContentResponse ();
175
173
}
176
174
177
175
@override
178
- Response addToMany (RelationshipTarget target, List <Identifier > identifiers) {
179
- final dao = _getDaoOrThrow (target);
176
+ Response addToMany (String type, String id, String relationship,
177
+ List <Identifier > identifiers) {
178
+ final dao = _getDaoOrThrow (type);
180
179
181
180
return ToManyResponse (
182
- target, dao.addToMany (target. id, target. relationship, identifiers));
181
+ type, id, relationship, dao.addToMany (id, relationship, identifiers));
183
182
}
184
183
185
- void _throwIfIncompatibleTypes (CollectionTarget target , Resource resource) {
186
- if (target. type != resource.type) {
184
+ void _throwIfIncompatibleTypes (String type , Resource resource) {
185
+ if (type != resource.type) {
187
186
throw ErrorResponse .conflict ([JsonApiError (detail: 'Incompatible type' )]);
188
187
}
189
188
}
190
189
191
- DAO _getDaoOrThrow (CollectionTarget target ) {
192
- if (_dao.containsKey (target. type)) return _dao[target. type];
190
+ DAO _getDaoOrThrow (String type ) {
191
+ if (_dao.containsKey (type)) return _dao[type];
193
192
194
193
throw ErrorResponse .notFound (
195
- [JsonApiError (detail: 'Unknown resource type ${target . type }' )]);
194
+ [JsonApiError (detail: 'Unknown resource type ${type }' )]);
196
195
}
197
196
198
- Resource _fetchResourceOrThrow (ResourceTarget target ) {
199
- final dao = _getDaoOrThrow (target );
200
- final resource = dao.fetchByIdAsResource (target. id);
197
+ Resource _fetchResourceOrThrow (String type, String id ) {
198
+ final dao = _getDaoOrThrow (type );
199
+ final resource = dao.fetchByIdAsResource (id);
201
200
if (resource == null ) {
202
201
throw ErrorResponse .notFound (
203
202
[JsonApiError (detail: 'Resource not found' )]);
0 commit comments