2017-02-01 21:43:18 +00:00
|
|
|
import 'dart:async';
|
2018-09-11 20:25:07 +00:00
|
|
|
|
2017-02-01 21:43:18 +00:00
|
|
|
import 'service.dart';
|
|
|
|
|
|
|
|
/// An easy helper class to create one-off services without having to create an entire class.
|
|
|
|
///
|
|
|
|
/// Well-suited for testing.
|
2018-09-11 20:25:07 +00:00
|
|
|
class AnonymousService<Id, Data> extends Service<Id, Data> {
|
2018-10-21 08:44:51 +00:00
|
|
|
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;
|
2017-02-01 21:43:18 +00:00
|
|
|
|
|
|
|
AnonymousService(
|
2018-10-22 16:18:10 +00:00
|
|
|
{FutureOr<List<Data>> index([Map<String, dynamic> params]),
|
|
|
|
FutureOr<Data> read(Id id, [Map<String, dynamic> params]),
|
|
|
|
FutureOr<Data> create(Data data, [Map<String, dynamic> params]),
|
|
|
|
FutureOr<Data> modify(Id id, Data data, [Map<String, dynamic> params]),
|
|
|
|
FutureOr<Data> update(Id id, Data data, [Map<String, dynamic> params]),
|
|
|
|
FutureOr<Data> remove(Id id, [Map<String, dynamic> params])})
|
2017-02-01 21:43:18 +00:00
|
|
|
: super() {
|
|
|
|
_index = index;
|
|
|
|
_read = read;
|
|
|
|
_create = create;
|
|
|
|
_modify = modify;
|
|
|
|
_update = update;
|
|
|
|
_remove = remove;
|
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
2018-09-11 20:25:07 +00:00
|
|
|
index([Map<String, dynamic> params]) => new Future.sync(
|
2018-07-12 15:19:25 +00:00
|
|
|
() => _index != null ? _index(params) : super.index(params));
|
2017-02-01 21:43:18 +00:00
|
|
|
|
|
|
|
@override
|
2018-09-11 20:25:07 +00:00
|
|
|
read(Id id, [Map<String, dynamic> params]) => new Future.sync(
|
2018-07-12 15:19:25 +00:00
|
|
|
() => _read != null ? _read(id, params) : super.read(id, params));
|
2017-02-01 21:43:18 +00:00
|
|
|
|
|
|
|
@override
|
2018-09-11 20:25:07 +00:00
|
|
|
create(Data data, [Map<String, dynamic> params]) => new Future.sync(() =>
|
2018-07-12 15:19:25 +00:00
|
|
|
_create != null ? _create(data, params) : super.create(data, params));
|
2017-02-01 21:43:18 +00:00
|
|
|
|
|
|
|
@override
|
2018-09-11 20:25:07 +00:00
|
|
|
modify(Id id, Data data, [Map<String, dynamic> params]) =>
|
|
|
|
new Future.sync(() => _modify != null
|
|
|
|
? _modify(id, data, params)
|
|
|
|
: super.modify(id, data, params));
|
2017-02-01 21:43:18 +00:00
|
|
|
|
|
|
|
@override
|
2018-09-11 20:25:07 +00:00
|
|
|
update(Id id, Data data, [Map<String, dynamic> params]) =>
|
|
|
|
new Future.sync(() => _update != null
|
|
|
|
? _update(id, data, params)
|
|
|
|
: super.update(id, data, params));
|
2017-02-01 21:43:18 +00:00
|
|
|
|
|
|
|
@override
|
2018-09-11 20:25:07 +00:00
|
|
|
remove(Id id, [Map<String, dynamic> params]) => new Future.sync(
|
2018-07-12 15:19:25 +00:00
|
|
|
() => _remove != null ? _remove(id, params) : super.remove(id, params));
|
2017-02-01 21:43:18 +00:00
|
|
|
}
|