From e1b85092d55098ab0cc6a639ea3c6de64e5a4b37 Mon Sep 17 00:00:00 2001 From: Tobe O Date: Mon, 10 Dec 2018 14:19:20 -0500 Subject: [PATCH] 2.0.1 --- CHANGELOG.md | 5 ++++- lib/mongo_service.dart | 24 ++++++++++++++++++++++++ lib/services.dart | 9 +++++++-- pubspec.yaml | 2 +- test/generic_test.dart | 25 +++++++++++++++++++++++++ 5 files changed, 61 insertions(+), 4 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 1c587589..93ad432d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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. \ No newline at end of file diff --git a/lib/mongo_service.dart b/lib/mongo_service.dart index 98b3449f..38e25951 100644 --- a/lib/mongo_service.dart +++ b/lib/mongo_service.dart @@ -111,6 +111,20 @@ class MongoService extends Service> { } } + @override + Future> findOne( + [Map 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> read(String id, [Map params]) async { @@ -125,6 +139,16 @@ class MongoService extends Service> { return _jsonify(found, params); } + @override + Future>> readMany(List ids, + [Map 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> modify(String id, data, [Map params]) async { diff --git a/lib/services.dart b/lib/services.dart index d322a289..a44dedd4 100644 --- a/lib/services.dart +++ b/lib/services.dart @@ -24,11 +24,16 @@ ObjectId _makeId(id) { } } -const List _SENSITIVE = const ['id', '_id', 'createdAt', 'updatedAt']; +const List _sensitiveFieldNames = const [ + 'id', + '_id', + 'createdAt', + 'updatedAt' +]; Map _removeSensitive(Map data) { return data.keys - .where((k) => !_SENSITIVE.contains(k)) + .where((k) => !_sensitiveFieldNames.contains(k)) .fold({}, (map, key) => map..[key] = data[key]); } diff --git a/pubspec.yaml b/pubspec.yaml index 4f22cb85..d34ceacd 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -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 homepage: https://github.com/angel-dart/angel_mongo diff --git a/test/generic_test.dart b/test/generic_test.dart index cd9592a6..e47fdf2c 100644 --- a/test/generic_test.dart +++ b/test/generic_test.dart @@ -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);