platform/packages/relations/test/common.dart

70 lines
1.4 KiB
Dart
Raw Normal View History

2021-02-16 02:10:09 +00:00
import 'dart:convert';
2017-01-30 02:39:11 +00:00
import 'package:angel_framework/angel_framework.dart';
2021-02-16 02:10:09 +00:00
//import 'package:json_god/json_god.dart' as god;
2017-01-30 02:39:11 +00:00
2017-07-09 17:11:17 +00:00
@deprecated
2017-03-04 21:00:17 +00:00
class CustomMapService extends Service {
2017-01-30 02:39:11 +00:00
final List<Map> _items = [];
2021-06-20 12:37:20 +00:00
Iterable<Map> tailor(Iterable<Map> items, Map? params) {
2017-01-30 02:39:11 +00:00
if (params == null) return items;
var r = items;
2021-06-26 13:13:43 +00:00
if (params['query'] is Map) {
2021-06-20 12:37:20 +00:00
var query = params['query'] as Map;
2017-01-30 02:39:11 +00:00
for (var key in query.keys) {
r = r.where((m) => m[key] == query[key]);
}
}
return r;
}
@override
2021-06-20 12:37:20 +00:00
Future<List<Map>> index([params]) async => tailor(_items, params).toList();
2017-01-30 02:39:11 +00:00
@override
2021-06-20 12:37:20 +00:00
Future<Map> read(id, [Map? params]) async {
2017-01-30 02:39:11 +00:00
return tailor(_items, params).firstWhere((m) => m['id'] == id,
2021-06-26 13:13:43 +00:00
orElse: (() => throw AngelHttpException.notFound()));
2017-01-30 02:39:11 +00:00
}
@override
2021-06-20 12:37:20 +00:00
Future<Map> create(data, [params]) async {
var d = data is Map ? data : (jsonDecode(data as String) as Map?)!;
2017-01-30 02:39:11 +00:00
d['id'] = _items.length.toString();
_items.add(d);
return d;
}
@override
2021-06-20 12:37:20 +00:00
Future remove(id, [params]) async {
2017-01-30 02:39:11 +00:00
if (id == null) _items.clear();
}
}
class Author {
2021-06-20 12:37:20 +00:00
String? id, name;
2017-01-30 02:39:11 +00:00
Author({this.id, this.name});
Map toJson() => {'id': id, 'name': name};
}
class Book {
2021-06-20 12:37:20 +00:00
String? authorId, title;
2017-01-30 02:39:11 +00:00
Book({this.authorId, this.title});
Map toJson() => {'authorId': authorId, 'title': title};
}
class Chapter {
2021-06-20 12:37:20 +00:00
String? bookId, title;
int? pageCount;
2017-01-30 02:39:11 +00:00
Chapter({this.bookId, this.title, this.pageCount});
}