11import 'dart:async' ;
22
3+ import 'package:json_api/document.dart' ;
34import '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' ;
85import 'package:uuid/uuid.dart' ;
96
107import 'dao.dart' ;
@@ -18,26 +15,27 @@ class CarsController implements Controller {
1815 CarsController (this ._dao, this ._pagination);
1916
2017 @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 );
2320 final collection = dao.fetchCollection (
2421 _pagination.limit (query.page), _pagination.offset (query.page));
2522 return CollectionResponse (collection.elements.map (dao.toResource),
2623 included: const [], total: collection.totalCount);
2724 }
2825
2926 @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);
3230
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];
3533 final resource = _dao[id.type].fetchByIdAsResource (id.id);
3634 return RelatedResourceResponse (resource);
3735 }
3836
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];
4139 final resources = relationships
4240 .skip (_pagination.offset (query.page))
4341 .take (_pagination.limit (query.page))
@@ -50,10 +48,10 @@ class CarsController implements Controller {
5048 }
5149
5250 @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 );
5553
56- final obj = dao.fetchById (target. id);
54+ final obj = dao.fetchById (id);
5755
5856 if (obj == null ) {
5957 return ErrorResponse .notFound (
@@ -74,43 +72,42 @@ class CarsController implements Controller {
7472 }
7573
7674 @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);
7978
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]);
8381 }
8482
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]);
8885 }
8986 return ErrorResponse .notFound (
9087 [JsonApiError (detail: 'Relationship not found' )]);
9188 }
9289
9390 @override
94- Response deleteResource (ResourceTarget target ) {
95- final dao = _getDaoOrThrow (target );
91+ Response deleteResource (String type, String id ) {
92+ final dao = _getDaoOrThrow (type );
9693
97- final res = dao.fetchByIdAsResource (target. id);
94+ final res = dao.fetchByIdAsResource (id);
9895 if (res == null ) {
9996 throw ErrorResponse .notFound (
10097 [JsonApiError (detail: 'Resource not found' )]);
10198 }
102- final dependenciesCount = dao.deleteById (target. id);
99+ final dependenciesCount = dao.deleteById (id);
103100 if (dependenciesCount == 0 ) {
104101 return NoContentResponse ();
105102 }
106103 return MetaResponse ({'dependenciesCount' : dependenciesCount});
107104 }
108105
109106 @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 );
112109
113- _throwIfIncompatibleTypes (target , resource);
110+ _throwIfIncompatibleTypes (type , resource);
114111
115112 if (resource.id != null ) {
116113 if (dao.fetchById (resource.id) != null ) {
@@ -126,7 +123,7 @@ class CarsController implements Controller {
126123 toMany: resource.toMany,
127124 toOne: resource.toOne));
128125
129- if (target. type == 'models' ) {
126+ if (type == 'models' ) {
130127 // Insertion is artificially delayed
131128 final job = Job (Future .delayed (Duration (milliseconds: 100 ), () {
132129 dao.insert (created);
@@ -142,62 +139,64 @@ class CarsController implements Controller {
142139 }
143140
144141 @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 );
147144
148- _throwIfIncompatibleTypes (target , resource);
149- if (dao.fetchById (target. id) == null ) {
145+ _throwIfIncompatibleTypes (type , resource);
146+ if (dao.fetchById (id) == null ) {
150147 return ErrorResponse .notFound (
151148 [JsonApiError (detail: 'Resource not found' )]);
152149 }
153- final updated = dao.update (target. id, resource);
150+ final updated = dao.update (id, resource);
154151 if (updated == null ) {
155152 return NoContentResponse ();
156153 }
157154 return ResourceUpdatedResponse (updated);
158155 }
159156
160157 @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);
163161
164- dao.replaceToOne (target. id, target. relationship, identifier);
162+ dao.replaceToOne (id, relationship, identifier);
165163 return NoContentResponse ();
166164 }
167165
168166 @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 );
172170
173- dao.replaceToMany (target. id, target. relationship, identifiers);
171+ dao.replaceToMany (id, relationship, identifiers);
174172 return NoContentResponse ();
175173 }
176174
177175 @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);
180179
181180 return ToManyResponse (
182- target, dao.addToMany (target. id, target. relationship, identifiers));
181+ type, id, relationship, dao.addToMany (id, relationship, identifiers));
183182 }
184183
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) {
187186 throw ErrorResponse .conflict ([JsonApiError (detail: 'Incompatible type' )]);
188187 }
189188 }
190189
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];
193192
194193 throw ErrorResponse .notFound (
195- [JsonApiError (detail: 'Unknown resource type ${target . type }' )]);
194+ [JsonApiError (detail: 'Unknown resource type ${type }' )]);
196195 }
197196
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);
201200 if (resource == null ) {
202201 throw ErrorResponse .notFound (
203202 [JsonApiError (detail: 'Resource not found' )]);
0 commit comments