Add readMany

This commit is contained in:
Tobe O 2018-11-07 21:34:03 -05:00
parent a396f16de3
commit 7fec4c243d
3 changed files with 26 additions and 1 deletions

View file

@ -1,3 +1,6 @@
# 2.0.0-alpha.11
* Add `readMany` to `Service`.
# 2.0.0-alpha.10 # 2.0.0-alpha.10
* All calls to `Service.parseId` are now affixed with the `<Id>` argument. * All calls to `Service.parseId` are now affixed with the `<Id>` argument.
* Added `uri` getter to `AngelHttp`. * Added `uri` getter to `AngelHttp`.

View file

@ -105,6 +105,14 @@ class Service<Id, Data> extends Routable {
throw new AngelHttpException.methodNotAllowed(); throw new AngelHttpException.methodNotAllowed();
} }
/// Reads multiple resources at once.
///
/// Service implementations should override this to ensure data is fetched within a
/// single round trip.
Future<List<Data>> readMany(List<Id> ids, [Map<String, dynamic> params]) {
return Future.wait(ids.map((id) => read(id, params)));
}
/// Creates a resource. /// Creates a resource.
Future<Data> create(Data data, [Map<String, dynamic> params]) { Future<Data> create(Data data, [Map<String, dynamic> params]) {
throw new AngelHttpException.methodNotAllowed(); throw new AngelHttpException.methodNotAllowed();

View file

@ -1,3 +1,5 @@
import 'dart:async';
import 'package:angel_container/mirrors.dart'; import 'package:angel_container/mirrors.dart';
import 'package:angel_framework/angel_framework.dart'; import 'package:angel_framework/angel_framework.dart';
import 'package:angel_framework/http.dart'; import 'package:angel_framework/http.dart';
@ -17,12 +19,13 @@ main() {
'Content-Type': 'application/json' 'Content-Type': 'application/json'
}; };
Angel app; Angel app;
MapService service;
String url; String url;
http.Client client; http.Client client;
setUp(() async { setUp(() async {
app = new Angel(reflector: MirrorsReflector()) app = new Angel(reflector: MirrorsReflector())
..use('/todos', new MapService()) ..use('/todos', service = new MapService())
..errorHandler = (e, req, res) { ..errorHandler = (e, req, res) {
print('Whoops: ${e.error}'); print('Whoops: ${e.error}');
if (e.stackTrace != null) print(new Chain.forTrace(e.stackTrace).terse); if (e.stackTrace != null) print(new Chain.forTrace(e.stackTrace).terse);
@ -98,6 +101,17 @@ main() {
expect(jsons['over'], equals('write')); expect(jsons['over'], equals('write'));
}); });
test('readMany', () async {
var items = <Map>[
await service.create({'foo': 'bar'}),
await service.create({'bar': 'baz'}),
await service.create({'baz': 'quux'})
];
var ids = items.map((m) => m['id'] as String).toList();
expect(await service.readMany(ids), items);
});
test('can delete data', () async { test('can delete data', () async {
String postData = json.encode({'text': 'Hello, world!'}); String postData = json.encode({'text': 'Hello, world!'});
var created = await client var created = await client