From fab756cc17c2b444a83c87b4728884bcc803f6ba Mon Sep 17 00:00:00 2001 From: Tobe O Date: Sun, 21 Oct 2018 04:44:51 -0400 Subject: [PATCH] Refactor service contract --- lib/src/core/anonymous_service.dart | 20 ++++++++++---------- lib/src/core/hooked_service.dart | 12 ++++++------ lib/src/core/service.dart | 18 ++++++++---------- test/anonymous_service_test.dart | 4 ++-- 4 files changed, 26 insertions(+), 28 deletions(-) diff --git a/lib/src/core/anonymous_service.dart b/lib/src/core/anonymous_service.dart index 408b5ca9..1d3117e2 100644 --- a/lib/src/core/anonymous_service.dart +++ b/lib/src/core/anonymous_service.dart @@ -6,18 +6,18 @@ import 'service.dart'; /// /// Well-suited for testing. class AnonymousService extends Service { - FutureOr Function([Map]) _index; - FutureOr Function(Id, [Map]) _read, _remove; - FutureOr Function(Data, [Map]) _create; - Function(Id, Data, [Map]) _modify, _update; + FutureOr> Function([Map]) _index; + FutureOr Function(Id, [Map]) _read, _remove; + FutureOr Function(Data, [Map]) _create; + FutureOr Function(Id, Data, [Map]) _modify, _update; AnonymousService( - {FutureOr index([Map params]), - FutureOr read(Id id, [Map params]), - FutureOr create(Data data, [Map params]), - FutureOr modify(Id id, Data data, [Map params]), - FutureOr update(Id id, Data data, [Map params]), - FutureOr remove(Id id, [Map params])}) + {FutureOr> index([Map params]), + FutureOr read(Id id, [Map params]), + FutureOr create(Data data, [Map params]), + FutureOr modify(Id id, Data data, [Map params]), + FutureOr update(Id id, Data data, [Map params]), + FutureOr remove(Id id, [Map params])}) : super() { _index = index; _read = read; diff --git a/lib/src/core/hooked_service.dart b/lib/src/core/hooked_service.dart index a8cd6fa6..e6cc202b 100644 --- a/lib/src/core/hooked_service.dart +++ b/lib/src/core/hooked_service.dart @@ -264,7 +264,7 @@ class HookedService> } @override - Future index([Map _params]) { + Future> index([Map _params]) { var params = _stripReq(_params); return beforeIndexed ._emit(new HookedServiceEvent(false, _getRequest(_params), @@ -290,7 +290,7 @@ class HookedService> } @override - Future read(Id id, [Map _params]) { + Future read(Id id, [Map _params]) { var params = _stripReq(_params); return beforeRead ._emit(new HookedServiceEvent(false, _getRequest(_params), @@ -316,7 +316,7 @@ class HookedService> } @override - Future create(Data data, [Map _params]) { + Future create(Data data, [Map _params]) { var params = _stripReq(_params); return beforeCreated ._emit(new HookedServiceEvent(false, _getRequest(_params), @@ -342,7 +342,7 @@ class HookedService> } @override - Future modify(Id id, Data data, [Map _params]) { + Future modify(Id id, Data data, [Map _params]) { var params = _stripReq(_params); return beforeModified ._emit(new HookedServiceEvent(false, _getRequest(_params), @@ -368,7 +368,7 @@ class HookedService> } @override - Future update(Id id, Data data, [Map _params]) { + Future update(Id id, Data data, [Map _params]) { var params = _stripReq(_params); return beforeUpdated ._emit(new HookedServiceEvent(false, _getRequest(_params), @@ -394,7 +394,7 @@ class HookedService> } @override - Future remove(Id id, [Map _params]) { + Future remove(Id id, [Map _params]) { var params = _stripReq(_params); return beforeRemoved ._emit(new HookedServiceEvent(false, _getRequest(_params), diff --git a/lib/src/core/service.dart b/lib/src/core/service.dart index 106be848..7f6cb952 100644 --- a/lib/src/core/service.dart +++ b/lib/src/core/service.dart @@ -78,51 +78,49 @@ class Service extends Routable { /// If the result is a non-empty [Iterable], [findOne] will return `it.first`, where `it` is the aforementioned [Iterable]. /// /// A custom [errorMessage] may be provided. - Future findOne( + Future findOne( [Map params, String errorMessage = 'No record was found matching the given query.']) { return index(params).then((result) { if (result == null) { throw new AngelHttpException.notFound(message: errorMessage); - } else if (result is Iterable) { + } else { if (result.isEmpty) { throw new AngelHttpException.notFound(message: errorMessage); } else { return result.first; } - } else { - return result; } }); } /// Retrieves all resources. - Future index([Map params]) { + Future> index([Map params]) { throw new AngelHttpException.methodNotAllowed(); } /// Retrieves the desired resource. - Future read(Id id, [Map params]) { + Future read(Id id, [Map params]) { throw new AngelHttpException.methodNotAllowed(); } /// Creates a resource. - Future create(Data data, [Map params]) { + Future create(Data data, [Map params]) { throw new AngelHttpException.methodNotAllowed(); } /// Modifies a resource. - Future modify(Id id, Data data, [Map params]) { + Future modify(Id id, Data data, [Map params]) { throw new AngelHttpException.methodNotAllowed(); } /// Overwrites a resource. - Future update(Id id, Data data, [Map params]) { + Future update(Id id, Data data, [Map params]) { throw new AngelHttpException.methodNotAllowed(); } /// Removes the given resource. - Future remove(Id id, [Map params]) { + Future remove(Id id, [Map params]) { throw new AngelHttpException.methodNotAllowed(); } diff --git a/test/anonymous_service_test.dart b/test/anonymous_service_test.dart index 7df3601b..3860f783 100644 --- a/test/anonymous_service_test.dart +++ b/test/anonymous_service_test.dart @@ -3,8 +3,8 @@ import 'package:test/test.dart'; main() { test('custom methods', () async { - var svc = new AnonymousService( - index: ([p]) async => 'index', + var svc = new AnonymousService( + index: ([p]) async => ['index'], read: (id, [p]) async => 'read', create: (data, [p]) async => 'create', modify: (id, data, [p]) async => 'modify',