This commit is contained in:
Tobe O 2018-12-10 14:19:20 -05:00
parent 0a3fc8d6ce
commit e1b85092d5
5 changed files with 61 additions and 4 deletions

View file

@ -1,3 +1,6 @@
# 2.0.0-rc.0
# 2.0.1
* Override `readMany` and `findOne`.
# 2.0.0-
* Delete `mongo_service_typed`.
* Update for Angel 2.

View file

@ -111,6 +111,20 @@ class MongoService extends Service<String, Map<String, dynamic>> {
}
}
@override
Future<Map<String, dynamic>> findOne(
[Map<String, dynamic> params,
String errorMessage =
'No record was found matching the given query.']) async {
var found = await collection.findOne(_makeQuery(params));
if (found == null) {
throw new AngelHttpException.notFound(message: errorMessage);
}
return _jsonify(found, params);
}
@override
Future<Map<String, dynamic>> read(String id,
[Map<String, dynamic> params]) async {
@ -125,6 +139,16 @@ class MongoService extends Service<String, Map<String, dynamic>> {
return _jsonify(found, params);
}
@override
Future<List<Map<String, dynamic>>> readMany(List<String> ids,
[Map<String, dynamic> params]) async {
var q = _makeQuery(params);
q = ids.fold(q, (q, id) => q.or(where.id(_makeId(id))));
return await (await collection.find(q))
.map((x) => _jsonify(x, params))
.toList();
}
@override
Future<Map<String, dynamic>> modify(String id, data,
[Map<String, dynamic> params]) async {

View file

@ -24,11 +24,16 @@ ObjectId _makeId(id) {
}
}
const List<String> _SENSITIVE = const ['id', '_id', 'createdAt', 'updatedAt'];
const List<String> _sensitiveFieldNames = const [
'id',
'_id',
'createdAt',
'updatedAt'
];
Map<String, dynamic> _removeSensitive(Map<String, dynamic> data) {
return data.keys
.where((k) => !_SENSITIVE.contains(k))
.where((k) => !_sensitiveFieldNames.contains(k))
.fold({}, (map, key) => map..[key] = data[key]);
}

View file

@ -1,5 +1,5 @@
name: angel_mongo
version: 2.0.0
version: 2.0.1
description: MongoDB-enabled services for the Angel framework. Well-tested.
author: Tobe O <thosakwe@gmail.com>
homepage: https://github.com/angel-dart/angel_mongo

View file

@ -99,6 +99,31 @@ main() {
//expect(read['createdAt'], isNot(null));
});
test('findOne', () async {
var response = await client.post("$url/api",
body: god.serialize(testGreeting), headers: headers);
expect(response.statusCode, isIn([200, 201]));
Map created = god.deserialize(response.body);
var id = new ObjectId.fromHexString(created['id'] as String);
var read = await greetingService.findOne({'query': where.id(id)});
expect(read['id'], equals(created['id']));
expect(read['to'], equals('world'));
//expect(read['createdAt'], isNot(null));
});
test('readMany', () async {
var response = await client.post("$url/api",
body: god.serialize(testGreeting), headers: headers);
expect(response.statusCode, isIn([200, 201]));
Map created = god.deserialize(response.body);
var id = new ObjectId.fromHexString(created['id'] as String);
var read = await greetingService.readMany([id.toHexString()]);
expect(read, [created]);
//expect(read['createdAt'], isNot(null));
});
test('modify item', () async {
var response = await client.post("$url/api",
body: god.serialize(testGreeting), headers: headers);