diff --git a/CHANGELOG.md b/CHANGELOG.md index 8986a106..b8545e00 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,6 @@ +# 2.0.0-alpha.11 +* Add `readMany` to `Service`. + # 2.0.0-alpha.10 * All calls to `Service.parseId` are now affixed with the `` argument. * Added `uri` getter to `AngelHttp`. diff --git a/lib/src/core/service.dart b/lib/src/core/service.dart index 56e5eef3..a5c9306f 100644 --- a/lib/src/core/service.dart +++ b/lib/src/core/service.dart @@ -105,6 +105,14 @@ class Service extends Routable { 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> readMany(List ids, [Map params]) { + return Future.wait(ids.map((id) => read(id, params))); + } + /// Creates a resource. Future create(Data data, [Map params]) { throw new AngelHttpException.methodNotAllowed(); diff --git a/test/services_test.dart b/test/services_test.dart index 025441ec..4a8c601e 100644 --- a/test/services_test.dart +++ b/test/services_test.dart @@ -1,3 +1,5 @@ +import 'dart:async'; + import 'package:angel_container/mirrors.dart'; import 'package:angel_framework/angel_framework.dart'; import 'package:angel_framework/http.dart'; @@ -17,12 +19,13 @@ main() { 'Content-Type': 'application/json' }; Angel app; + MapService service; String url; http.Client client; setUp(() async { app = new Angel(reflector: MirrorsReflector()) - ..use('/todos', new MapService()) + ..use('/todos', service = new MapService()) ..errorHandler = (e, req, res) { print('Whoops: ${e.error}'); if (e.stackTrace != null) print(new Chain.forTrace(e.stackTrace).terse); @@ -98,6 +101,17 @@ main() { expect(jsons['over'], equals('write')); }); + test('readMany', () async { + var items = [ + 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 { String postData = json.encode({'text': 'Hello, world!'}); var created = await client