Refactor service contract

This commit is contained in:
Tobe O 2018-10-21 04:44:51 -04:00
parent 411ff21dca
commit fab756cc17
4 changed files with 26 additions and 28 deletions

View file

@ -6,18 +6,18 @@ import 'service.dart';
///
/// Well-suited for testing.
class AnonymousService<Id, Data> extends Service<Id, Data> {
FutureOr Function([Map<String, dynamic>]) _index;
FutureOr Function(Id, [Map<String, dynamic>]) _read, _remove;
FutureOr Function(Data, [Map<String, dynamic>]) _create;
Function(Id, Data, [Map<String, dynamic>]) _modify, _update;
FutureOr<List<Data>> Function([Map<String, dynamic>]) _index;
FutureOr<Data> Function(Id, [Map<String, dynamic>]) _read, _remove;
FutureOr<Data> Function(Data, [Map<String, dynamic>]) _create;
FutureOr<Data> Function(Id, Data, [Map<String, dynamic>]) _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<List<Data>> index([Map params]),
FutureOr<Data> read(Id id, [Map params]),
FutureOr<Data> create(Data data, [Map params]),
FutureOr<Data> modify(Id id, Data data, [Map params]),
FutureOr<Data> update(Id id, Data data, [Map params]),
FutureOr<Data> remove(Id id, [Map params])})
: super() {
_index = index;
_read = read;

View file

@ -264,7 +264,7 @@ class HookedService<Id, Data, T extends Service<Id, Data>>
}
@override
Future index([Map<String, dynamic> _params]) {
Future<List<Data>> index([Map<String, dynamic> _params]) {
var params = _stripReq(_params);
return beforeIndexed
._emit(new HookedServiceEvent(false, _getRequest(_params),
@ -290,7 +290,7 @@ class HookedService<Id, Data, T extends Service<Id, Data>>
}
@override
Future read(Id id, [Map<String, dynamic> _params]) {
Future<Data> read(Id id, [Map<String, dynamic> _params]) {
var params = _stripReq(_params);
return beforeRead
._emit(new HookedServiceEvent(false, _getRequest(_params),
@ -316,7 +316,7 @@ class HookedService<Id, Data, T extends Service<Id, Data>>
}
@override
Future create(Data data, [Map<String, dynamic> _params]) {
Future<Data> create(Data data, [Map<String, dynamic> _params]) {
var params = _stripReq(_params);
return beforeCreated
._emit(new HookedServiceEvent(false, _getRequest(_params),
@ -342,7 +342,7 @@ class HookedService<Id, Data, T extends Service<Id, Data>>
}
@override
Future modify(Id id, Data data, [Map<String, dynamic> _params]) {
Future<Data> modify(Id id, Data data, [Map<String, dynamic> _params]) {
var params = _stripReq(_params);
return beforeModified
._emit(new HookedServiceEvent(false, _getRequest(_params),
@ -368,7 +368,7 @@ class HookedService<Id, Data, T extends Service<Id, Data>>
}
@override
Future update(Id id, Data data, [Map<String, dynamic> _params]) {
Future<Data> update(Id id, Data data, [Map<String, dynamic> _params]) {
var params = _stripReq(_params);
return beforeUpdated
._emit(new HookedServiceEvent(false, _getRequest(_params),
@ -394,7 +394,7 @@ class HookedService<Id, Data, T extends Service<Id, Data>>
}
@override
Future remove(Id id, [Map<String, dynamic> _params]) {
Future<Data> remove(Id id, [Map<String, dynamic> _params]) {
var params = _stripReq(_params);
return beforeRemoved
._emit(new HookedServiceEvent(false, _getRequest(_params),

View file

@ -78,51 +78,49 @@ class Service<Id, Data> 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<Data> findOne(
[Map<String, dynamic> 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<String, dynamic> params]) {
Future<List<Data>> index([Map<String, dynamic> params]) {
throw new AngelHttpException.methodNotAllowed();
}
/// Retrieves the desired resource.
Future read(Id id, [Map<String, dynamic> params]) {
Future<Data> read(Id id, [Map<String, dynamic> params]) {
throw new AngelHttpException.methodNotAllowed();
}
/// Creates a resource.
Future create(Data data, [Map<String, dynamic> params]) {
Future<Data> create(Data data, [Map<String, dynamic> params]) {
throw new AngelHttpException.methodNotAllowed();
}
/// Modifies a resource.
Future modify(Id id, Data data, [Map<String, dynamic> params]) {
Future<Data> modify(Id id, Data data, [Map<String, dynamic> params]) {
throw new AngelHttpException.methodNotAllowed();
}
/// Overwrites a resource.
Future update(Id id, Data data, [Map<String, dynamic> params]) {
Future<Data> update(Id id, Data data, [Map<String, dynamic> params]) {
throw new AngelHttpException.methodNotAllowed();
}
/// Removes the given resource.
Future remove(Id id, [Map<String, dynamic> params]) {
Future<Data> remove(Id id, [Map<String, dynamic> params]) {
throw new AngelHttpException.methodNotAllowed();
}

View file

@ -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<String, String>(
index: ([p]) async => ['index'],
read: (id, [p]) async => 'read',
create: (data, [p]) async => 'create',
modify: (id, data, [p]) async => 'modify',