11import 'dart:convert' ;
22
33import 'package:http_interop/extensions.dart' ;
4- import 'package:http_interop/http_interop.dart' as http ;
4+ import 'package:http_interop/http_interop.dart' ;
55import 'package:json_api/document.dart' ;
66import 'package:json_api/query.dart' ;
77import 'package:json_api/routing.dart' ;
@@ -22,7 +22,7 @@ class RepositoryController implements Controller {
2222 final design = StandardUriDesign .pathOnly;
2323
2424 @override
25- Future <Response > fetchCollection (http. Request request, Target target) async {
25+ Future <Response > fetchCollection (Request request, Target target) async {
2626 final resources = await _fetchAll (target.type).toList ();
2727 final doc = OutboundDataDocument .collection (resources)
2828 ..links['self' ] = Link (design.collection (target.type));
@@ -32,128 +32,125 @@ class RepositoryController implements Controller {
3232 doc.included.add (r);
3333 }
3434 }
35- return Response . ok (doc);
35+ return ok (doc);
3636 }
3737
3838 @override
39- Future <Response > fetchResource (
40- http.Request request, ResourceTarget target) async {
39+ Future <Response > fetchResource (Request request, ResourceTarget target) async {
4140 final resource = await _fetchLinkedResource (target.type, target.id);
4241 final doc = OutboundDataDocument .resource (resource)
4342 ..links['self' ] = Link (design.resource (target.type, target.id));
4443 final forest = RelationshipNode .forest (Include .fromUri (request.uri));
4544 await for (final r in _getAllRelated (resource, forest)) {
4645 doc.included.add (r);
4746 }
48- return Response . ok (doc);
47+ return ok (doc);
4948 }
5049
5150 @override
52- Future <Response > createResource (http. Request request, Target target) async {
51+ Future <Response > createResource (Request request, Target target) async {
5352 final document = await _decode (request);
5453 final newResource = document.dataAsNewResource ();
5554 final res = newResource.toResource (getId);
5655 await repo.persist (
5756 res.type, Model (res.id)..setFrom (ModelProps .fromResource (res)));
5857 if (newResource.id != null ) {
59- return Response . noContent ();
58+ return noContent ();
6059 }
6160 final ref = Reference .of (res.toIdentifier ());
6261 final self = Link (design.resource (ref.type, ref.id));
6362 final resource = (await _fetchResource (ref.type, ref.id))
6463 ..links['self' ] = self;
65- return Response . created (
64+ return created (
6665 OutboundDataDocument .resource (resource)..links['self' ] = self,
6766 self.uri.toString ());
6867 }
6968
7069 @override
71- Future <Response > addMany (
72- http.Request request, RelationshipTarget target) async {
70+ Future <Response > addMany (Request request, RelationshipTarget target) async {
7371 final many = (await _decode (request)).asRelationship <ToMany >();
7472 final refs = await repo
7573 .addMany (target.type, target.id, target.relationship, many)
7674 .toList ();
77- return Response . ok (OutboundDataDocument .many (ToMany (refs)));
75+ return ok (OutboundDataDocument .many (ToMany (refs)));
7876 }
7977
8078 @override
8179 Future <Response > deleteResource (
82- http. Request request, ResourceTarget target) async {
80+ Request request, ResourceTarget target) async {
8381 await repo.delete (target.type, target.id);
84- return Response . noContent ();
82+ return noContent ();
8583 }
8684
8785 @override
8886 Future <Response > updateResource (
89- http. Request request, ResourceTarget target) async {
87+ Request request, ResourceTarget target) async {
9088 await repo.update (target.type, target.id,
9189 ModelProps .fromResource ((await _decode (request)).dataAsResource ()));
92- return Response . noContent ();
90+ return noContent ();
9391 }
9492
9593 @override
9694 Future <Response > replaceRelationship (
97- http. Request request, RelationshipTarget target) async {
95+ Request request, RelationshipTarget target) async {
9896 final rel = (await _decode (request)).asRelationship ();
9997 if (rel is ToOne ) {
10098 final ref = rel.identifier;
10199 await repo.replaceOne (target.type, target.id, target.relationship, ref);
102- return Response . ok (
100+ return ok (
103101 OutboundDataDocument .one (ref == null ? ToOne .empty () : ToOne (ref)));
104102 }
105103 if (rel is ToMany ) {
106104 final ids = await repo
107105 .replaceMany (target.type, target.id, target.relationship, rel)
108106 .toList ();
109- return Response . ok (OutboundDataDocument .many (ToMany (ids)));
107+ return ok (OutboundDataDocument .many (ToMany (ids)));
110108 }
111109 throw FormatException ('Incomplete relationship' );
112110 }
113111
114112 @override
115113 Future <Response > deleteMany (
116- http. Request request, RelationshipTarget target) async {
114+ Request request, RelationshipTarget target) async {
117115 final rel = (await _decode (request)).asToMany ();
118116 final ids = await repo
119117 .deleteMany (target.type, target.id, target.relationship, rel)
120118 .toList ();
121- return Response . ok (OutboundDataDocument .many (ToMany (ids)));
119+ return ok (OutboundDataDocument .many (ToMany (ids)));
122120 }
123121
124122 @override
125123 Future <Response > fetchRelationship (
126- http. Request request, RelationshipTarget target) async {
124+ Request request, RelationshipTarget target) async {
127125 final model = (await repo.fetch (target.type, target.id));
128126
129127 if (model.one.containsKey (target.relationship)) {
130- return Response . ok (OutboundDataDocument .one (
128+ return ok (OutboundDataDocument .one (
131129 ToOne (model.one[target.relationship]? .toIdentifier ())));
132130 }
133131 final many =
134132 model.many[target.relationship]? .map ((it) => it.toIdentifier ());
135133 if (many != null ) {
136134 final doc = OutboundDataDocument .many (ToMany (many));
137- return Response . ok (doc);
135+ return ok (doc);
138136 }
139137 throw RelationshipNotFound (target.type, target.id, target.relationship);
140138 }
141139
142140 @override
143- Future <Response > fetchRelated (
144- http.Request request, RelatedTarget target) async {
141+ Future <Response > fetchRelated (Request request, RelatedTarget target) async {
145142 final model = await repo.fetch (target.type, target.id);
146143 if (model.one.containsKey (target.relationship)) {
147144 final related =
148145 await nullable (_fetchRelatedResource)(model.one[target.relationship]);
149146 final doc = OutboundDataDocument .resource (related);
150- return Response . ok (doc);
147+ return ok (doc);
151148 }
152149 if (model.many.containsKey (target.relationship)) {
153150 final many = model.many[target.relationship] ?? {};
154151 final doc = OutboundDataDocument .collection (
155152 await _fetchRelatedCollection (many).toList ());
156- return Response . ok (doc);
153+ return ok (doc);
157154 }
158155 throw RelationshipNotFound (target.type, target.id, target.relationship);
159156 }
@@ -171,10 +168,10 @@ class RepositoryController implements Controller {
171168
172169 /// Returns a stream of related resources
173170 Stream <Resource > _getRelated (Resource resource, String relationship) async * {
174- for (final _ in resource.relationships[relationship] ??
171+ for (final rel in resource.relationships[relationship] ??
175172 (throw RelationshipNotFound (
176173 resource.type, resource.id, relationship))) {
177- yield await _fetchLinkedResource (_ .type, _ .id);
174+ yield await _fetchLinkedResource (rel .type, rel .id);
178175 }
179176 }
180177
@@ -185,7 +182,7 @@ class RepositoryController implements Controller {
185182 }
186183
187184 Stream <Resource > _fetchAll (String type) =>
188- repo.fetchCollection (type).map ((_ ) => _ .toResource (type));
185+ repo.fetchCollection (type).map ((model ) => model .toResource (type));
189186
190187 /// Fetches and builds a resource object
191188 Future <Resource > _fetchResource (String type, String id) async {
@@ -203,7 +200,7 @@ class RepositoryController implements Controller {
203200 }
204201 }
205202
206- Future <InboundDocument > _decode (http. Request r) => r.body
203+ Future <InboundDocument > _decode (Request r) => r.body
207204 .decode (utf8)
208205 .then (const PayloadCodec ().decode)
209206 .then (InboundDocument .new );
0 commit comments