Refactor service contract
This commit is contained in:
parent
411ff21dca
commit
fab756cc17
4 changed files with 26 additions and 28 deletions
|
@ -6,18 +6,18 @@ import 'service.dart';
|
||||||
///
|
///
|
||||||
/// Well-suited for testing.
|
/// Well-suited for testing.
|
||||||
class AnonymousService<Id, Data> extends Service<Id, Data> {
|
class AnonymousService<Id, Data> extends Service<Id, Data> {
|
||||||
FutureOr Function([Map<String, dynamic>]) _index;
|
FutureOr<List<Data>> Function([Map<String, dynamic>]) _index;
|
||||||
FutureOr Function(Id, [Map<String, dynamic>]) _read, _remove;
|
FutureOr<Data> Function(Id, [Map<String, dynamic>]) _read, _remove;
|
||||||
FutureOr Function(Data, [Map<String, dynamic>]) _create;
|
FutureOr<Data> Function(Data, [Map<String, dynamic>]) _create;
|
||||||
Function(Id, Data, [Map<String, dynamic>]) _modify, _update;
|
FutureOr<Data> Function(Id, Data, [Map<String, dynamic>]) _modify, _update;
|
||||||
|
|
||||||
AnonymousService(
|
AnonymousService(
|
||||||
{FutureOr index([Map params]),
|
{FutureOr<List<Data>> index([Map params]),
|
||||||
FutureOr read(Id id, [Map params]),
|
FutureOr<Data> read(Id id, [Map params]),
|
||||||
FutureOr create(Data data, [Map params]),
|
FutureOr<Data> create(Data data, [Map params]),
|
||||||
FutureOr modify(Id id, Data data, [Map params]),
|
FutureOr<Data> modify(Id id, Data data, [Map params]),
|
||||||
FutureOr update(Id id, Data data, [Map params]),
|
FutureOr<Data> update(Id id, Data data, [Map params]),
|
||||||
FutureOr remove(Id id, [Map params])})
|
FutureOr<Data> remove(Id id, [Map params])})
|
||||||
: super() {
|
: super() {
|
||||||
_index = index;
|
_index = index;
|
||||||
_read = read;
|
_read = read;
|
||||||
|
|
|
@ -264,7 +264,7 @@ class HookedService<Id, Data, T extends Service<Id, Data>>
|
||||||
}
|
}
|
||||||
|
|
||||||
@override
|
@override
|
||||||
Future index([Map<String, dynamic> _params]) {
|
Future<List<Data>> index([Map<String, dynamic> _params]) {
|
||||||
var params = _stripReq(_params);
|
var params = _stripReq(_params);
|
||||||
return beforeIndexed
|
return beforeIndexed
|
||||||
._emit(new HookedServiceEvent(false, _getRequest(_params),
|
._emit(new HookedServiceEvent(false, _getRequest(_params),
|
||||||
|
@ -290,7 +290,7 @@ class HookedService<Id, Data, T extends Service<Id, Data>>
|
||||||
}
|
}
|
||||||
|
|
||||||
@override
|
@override
|
||||||
Future read(Id id, [Map<String, dynamic> _params]) {
|
Future<Data> read(Id id, [Map<String, dynamic> _params]) {
|
||||||
var params = _stripReq(_params);
|
var params = _stripReq(_params);
|
||||||
return beforeRead
|
return beforeRead
|
||||||
._emit(new HookedServiceEvent(false, _getRequest(_params),
|
._emit(new HookedServiceEvent(false, _getRequest(_params),
|
||||||
|
@ -316,7 +316,7 @@ class HookedService<Id, Data, T extends Service<Id, Data>>
|
||||||
}
|
}
|
||||||
|
|
||||||
@override
|
@override
|
||||||
Future create(Data data, [Map<String, dynamic> _params]) {
|
Future<Data> create(Data data, [Map<String, dynamic> _params]) {
|
||||||
var params = _stripReq(_params);
|
var params = _stripReq(_params);
|
||||||
return beforeCreated
|
return beforeCreated
|
||||||
._emit(new HookedServiceEvent(false, _getRequest(_params),
|
._emit(new HookedServiceEvent(false, _getRequest(_params),
|
||||||
|
@ -342,7 +342,7 @@ class HookedService<Id, Data, T extends Service<Id, Data>>
|
||||||
}
|
}
|
||||||
|
|
||||||
@override
|
@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);
|
var params = _stripReq(_params);
|
||||||
return beforeModified
|
return beforeModified
|
||||||
._emit(new HookedServiceEvent(false, _getRequest(_params),
|
._emit(new HookedServiceEvent(false, _getRequest(_params),
|
||||||
|
@ -368,7 +368,7 @@ class HookedService<Id, Data, T extends Service<Id, Data>>
|
||||||
}
|
}
|
||||||
|
|
||||||
@override
|
@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);
|
var params = _stripReq(_params);
|
||||||
return beforeUpdated
|
return beforeUpdated
|
||||||
._emit(new HookedServiceEvent(false, _getRequest(_params),
|
._emit(new HookedServiceEvent(false, _getRequest(_params),
|
||||||
|
@ -394,7 +394,7 @@ class HookedService<Id, Data, T extends Service<Id, Data>>
|
||||||
}
|
}
|
||||||
|
|
||||||
@override
|
@override
|
||||||
Future remove(Id id, [Map<String, dynamic> _params]) {
|
Future<Data> remove(Id id, [Map<String, dynamic> _params]) {
|
||||||
var params = _stripReq(_params);
|
var params = _stripReq(_params);
|
||||||
return beforeRemoved
|
return beforeRemoved
|
||||||
._emit(new HookedServiceEvent(false, _getRequest(_params),
|
._emit(new HookedServiceEvent(false, _getRequest(_params),
|
||||||
|
|
|
@ -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].
|
/// 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.
|
/// A custom [errorMessage] may be provided.
|
||||||
Future findOne(
|
Future<Data> findOne(
|
||||||
[Map<String, dynamic> params,
|
[Map<String, dynamic> params,
|
||||||
String errorMessage = 'No record was found matching the given query.']) {
|
String errorMessage = 'No record was found matching the given query.']) {
|
||||||
return index(params).then((result) {
|
return index(params).then((result) {
|
||||||
if (result == null) {
|
if (result == null) {
|
||||||
throw new AngelHttpException.notFound(message: errorMessage);
|
throw new AngelHttpException.notFound(message: errorMessage);
|
||||||
} else if (result is Iterable) {
|
} else {
|
||||||
if (result.isEmpty) {
|
if (result.isEmpty) {
|
||||||
throw new AngelHttpException.notFound(message: errorMessage);
|
throw new AngelHttpException.notFound(message: errorMessage);
|
||||||
} else {
|
} else {
|
||||||
return result.first;
|
return result.first;
|
||||||
}
|
}
|
||||||
} else {
|
|
||||||
return result;
|
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Retrieves all resources.
|
/// Retrieves all resources.
|
||||||
Future index([Map<String, dynamic> params]) {
|
Future<List<Data>> index([Map<String, dynamic> params]) {
|
||||||
throw new AngelHttpException.methodNotAllowed();
|
throw new AngelHttpException.methodNotAllowed();
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Retrieves the desired resource.
|
/// 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();
|
throw new AngelHttpException.methodNotAllowed();
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Creates a resource.
|
/// Creates a resource.
|
||||||
Future create(Data data, [Map<String, dynamic> params]) {
|
Future<Data> create(Data data, [Map<String, dynamic> params]) {
|
||||||
throw new AngelHttpException.methodNotAllowed();
|
throw new AngelHttpException.methodNotAllowed();
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Modifies a resource.
|
/// 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();
|
throw new AngelHttpException.methodNotAllowed();
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Overwrites a resource.
|
/// 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();
|
throw new AngelHttpException.methodNotAllowed();
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Removes the given resource.
|
/// 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();
|
throw new AngelHttpException.methodNotAllowed();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -3,8 +3,8 @@ import 'package:test/test.dart';
|
||||||
|
|
||||||
main() {
|
main() {
|
||||||
test('custom methods', () async {
|
test('custom methods', () async {
|
||||||
var svc = new AnonymousService(
|
var svc = new AnonymousService<String, String>(
|
||||||
index: ([p]) async => 'index',
|
index: ([p]) async => ['index'],
|
||||||
read: (id, [p]) async => 'read',
|
read: (id, [p]) async => 'read',
|
||||||
create: (data, [p]) async => 'create',
|
create: (data, [p]) async => 'create',
|
||||||
modify: (id, data, [p]) async => 'modify',
|
modify: (id, data, [p]) async => 'modify',
|
||||||
|
|
Loading…
Reference in a new issue