1
1
import 'dart:convert' ;
2
2
3
3
import 'package:http_interop/extensions.dart' ;
4
- import 'package:http_interop/http_interop.dart' as http ;
4
+ import 'package:http_interop/http_interop.dart' ;
5
5
import 'package:json_api/document.dart' ;
6
6
import 'package:json_api/query.dart' ;
7
7
import 'package:json_api/routing.dart' ;
@@ -22,7 +22,7 @@ class RepositoryController implements Controller {
22
22
final design = StandardUriDesign .pathOnly;
23
23
24
24
@override
25
- Future <Response > fetchCollection (http. Request request, Target target) async {
25
+ Future <Response > fetchCollection (Request request, Target target) async {
26
26
final resources = await _fetchAll (target.type).toList ();
27
27
final doc = OutboundDataDocument .collection (resources)
28
28
..links['self' ] = Link (design.collection (target.type));
@@ -32,128 +32,125 @@ class RepositoryController implements Controller {
32
32
doc.included.add (r);
33
33
}
34
34
}
35
- return Response . ok (doc);
35
+ return ok (doc);
36
36
}
37
37
38
38
@override
39
- Future <Response > fetchResource (
40
- http.Request request, ResourceTarget target) async {
39
+ Future <Response > fetchResource (Request request, ResourceTarget target) async {
41
40
final resource = await _fetchLinkedResource (target.type, target.id);
42
41
final doc = OutboundDataDocument .resource (resource)
43
42
..links['self' ] = Link (design.resource (target.type, target.id));
44
43
final forest = RelationshipNode .forest (Include .fromUri (request.uri));
45
44
await for (final r in _getAllRelated (resource, forest)) {
46
45
doc.included.add (r);
47
46
}
48
- return Response . ok (doc);
47
+ return ok (doc);
49
48
}
50
49
51
50
@override
52
- Future <Response > createResource (http. Request request, Target target) async {
51
+ Future <Response > createResource (Request request, Target target) async {
53
52
final document = await _decode (request);
54
53
final newResource = document.dataAsNewResource ();
55
54
final res = newResource.toResource (getId);
56
55
await repo.persist (
57
56
res.type, Model (res.id)..setFrom (ModelProps .fromResource (res)));
58
57
if (newResource.id != null ) {
59
- return Response . noContent ();
58
+ return noContent ();
60
59
}
61
60
final ref = Reference .of (res.toIdentifier ());
62
61
final self = Link (design.resource (ref.type, ref.id));
63
62
final resource = (await _fetchResource (ref.type, ref.id))
64
63
..links['self' ] = self;
65
- return Response . created (
64
+ return created (
66
65
OutboundDataDocument .resource (resource)..links['self' ] = self,
67
66
self.uri.toString ());
68
67
}
69
68
70
69
@override
71
- Future <Response > addMany (
72
- http.Request request, RelationshipTarget target) async {
70
+ Future <Response > addMany (Request request, RelationshipTarget target) async {
73
71
final many = (await _decode (request)).asRelationship <ToMany >();
74
72
final refs = await repo
75
73
.addMany (target.type, target.id, target.relationship, many)
76
74
.toList ();
77
- return Response . ok (OutboundDataDocument .many (ToMany (refs)));
75
+ return ok (OutboundDataDocument .many (ToMany (refs)));
78
76
}
79
77
80
78
@override
81
79
Future <Response > deleteResource (
82
- http. Request request, ResourceTarget target) async {
80
+ Request request, ResourceTarget target) async {
83
81
await repo.delete (target.type, target.id);
84
- return Response . noContent ();
82
+ return noContent ();
85
83
}
86
84
87
85
@override
88
86
Future <Response > updateResource (
89
- http. Request request, ResourceTarget target) async {
87
+ Request request, ResourceTarget target) async {
90
88
await repo.update (target.type, target.id,
91
89
ModelProps .fromResource ((await _decode (request)).dataAsResource ()));
92
- return Response . noContent ();
90
+ return noContent ();
93
91
}
94
92
95
93
@override
96
94
Future <Response > replaceRelationship (
97
- http. Request request, RelationshipTarget target) async {
95
+ Request request, RelationshipTarget target) async {
98
96
final rel = (await _decode (request)).asRelationship ();
99
97
if (rel is ToOne ) {
100
98
final ref = rel.identifier;
101
99
await repo.replaceOne (target.type, target.id, target.relationship, ref);
102
- return Response . ok (
100
+ return ok (
103
101
OutboundDataDocument .one (ref == null ? ToOne .empty () : ToOne (ref)));
104
102
}
105
103
if (rel is ToMany ) {
106
104
final ids = await repo
107
105
.replaceMany (target.type, target.id, target.relationship, rel)
108
106
.toList ();
109
- return Response . ok (OutboundDataDocument .many (ToMany (ids)));
107
+ return ok (OutboundDataDocument .many (ToMany (ids)));
110
108
}
111
109
throw FormatException ('Incomplete relationship' );
112
110
}
113
111
114
112
@override
115
113
Future <Response > deleteMany (
116
- http. Request request, RelationshipTarget target) async {
114
+ Request request, RelationshipTarget target) async {
117
115
final rel = (await _decode (request)).asToMany ();
118
116
final ids = await repo
119
117
.deleteMany (target.type, target.id, target.relationship, rel)
120
118
.toList ();
121
- return Response . ok (OutboundDataDocument .many (ToMany (ids)));
119
+ return ok (OutboundDataDocument .many (ToMany (ids)));
122
120
}
123
121
124
122
@override
125
123
Future <Response > fetchRelationship (
126
- http. Request request, RelationshipTarget target) async {
124
+ Request request, RelationshipTarget target) async {
127
125
final model = (await repo.fetch (target.type, target.id));
128
126
129
127
if (model.one.containsKey (target.relationship)) {
130
- return Response . ok (OutboundDataDocument .one (
128
+ return ok (OutboundDataDocument .one (
131
129
ToOne (model.one[target.relationship]? .toIdentifier ())));
132
130
}
133
131
final many =
134
132
model.many[target.relationship]? .map ((it) => it.toIdentifier ());
135
133
if (many != null ) {
136
134
final doc = OutboundDataDocument .many (ToMany (many));
137
- return Response . ok (doc);
135
+ return ok (doc);
138
136
}
139
137
throw RelationshipNotFound (target.type, target.id, target.relationship);
140
138
}
141
139
142
140
@override
143
- Future <Response > fetchRelated (
144
- http.Request request, RelatedTarget target) async {
141
+ Future <Response > fetchRelated (Request request, RelatedTarget target) async {
145
142
final model = await repo.fetch (target.type, target.id);
146
143
if (model.one.containsKey (target.relationship)) {
147
144
final related =
148
145
await nullable (_fetchRelatedResource)(model.one[target.relationship]);
149
146
final doc = OutboundDataDocument .resource (related);
150
- return Response . ok (doc);
147
+ return ok (doc);
151
148
}
152
149
if (model.many.containsKey (target.relationship)) {
153
150
final many = model.many[target.relationship] ?? {};
154
151
final doc = OutboundDataDocument .collection (
155
152
await _fetchRelatedCollection (many).toList ());
156
- return Response . ok (doc);
153
+ return ok (doc);
157
154
}
158
155
throw RelationshipNotFound (target.type, target.id, target.relationship);
159
156
}
@@ -171,10 +168,10 @@ class RepositoryController implements Controller {
171
168
172
169
/// Returns a stream of related resources
173
170
Stream <Resource > _getRelated (Resource resource, String relationship) async * {
174
- for (final _ in resource.relationships[relationship] ??
171
+ for (final rel in resource.relationships[relationship] ??
175
172
(throw RelationshipNotFound (
176
173
resource.type, resource.id, relationship))) {
177
- yield await _fetchLinkedResource (_ .type, _ .id);
174
+ yield await _fetchLinkedResource (rel .type, rel .id);
178
175
}
179
176
}
180
177
@@ -185,7 +182,7 @@ class RepositoryController implements Controller {
185
182
}
186
183
187
184
Stream <Resource > _fetchAll (String type) =>
188
- repo.fetchCollection (type).map ((_ ) => _ .toResource (type));
185
+ repo.fetchCollection (type).map ((model ) => model .toResource (type));
189
186
190
187
/// Fetches and builds a resource object
191
188
Future <Resource > _fetchResource (String type, String id) async {
@@ -203,7 +200,7 @@ class RepositoryController implements Controller {
203
200
}
204
201
}
205
202
206
- Future <InboundDocument > _decode (http. Request r) => r.body
203
+ Future <InboundDocument > _decode (Request r) => r.body
207
204
.decode (utf8)
208
205
.then (const PayloadCodec ().decode)
209
206
.then (InboundDocument .new );
0 commit comments