From 026d75cf7ab9619373165bbf7b940c640e8bca36 Mon Sep 17 00:00:00 2001 From: thosakwe Date: Sun, 19 Feb 2017 07:32:21 -0500 Subject: [PATCH] 1.1.0 --- .vscode/tasks.json | 10 ++++++++++ README.md | 4 ++-- lib/mongo_service.dart | 15 ++++++++++----- lib/mongo_service_typed.dart | 30 +++++++++++++++++++++++++----- pubspec.yaml | 2 +- test/generic_test.dart | 3 +-- 6 files changed, 49 insertions(+), 15 deletions(-) create mode 100644 .vscode/tasks.json diff --git a/.vscode/tasks.json b/.vscode/tasks.json new file mode 100644 index 00000000..6969f9b5 --- /dev/null +++ b/.vscode/tasks.json @@ -0,0 +1,10 @@ +{ + // See https://go.microsoft.com/fwlink/?LinkId=733558 + // for the documentation about the tasks.json format + "version": "0.1.0", + "command": "test", + "isShellCommand": true, + "args": ["pub", "run", "test"], + "showOutput": "always", + "suppressTaskName": true +} \ No newline at end of file diff --git a/README.md b/README.md index 3fc5f217..0a73a211 100644 --- a/README.md +++ b/README.md @@ -1,7 +1,7 @@ # angel_mongo -![version 1.0.0-dev+6](https://img.shields.io/badge/version-1.0.0--dev+6-red.svg) -![build status](https://travis-ci.org/angel-dart/mongo.svg?branch=master) +[![version 1.1.0](https://img.shields.io/badge/pub-1.1.0-red.svg)](https://pub.dartlang.org/packages/angel_mongo) +[![build status](https://travis-ci.org/angel-dart/mongo.svg?branch=master)](https://travis-ci.org/angel-dart/mongo) MongoDB-enabled services for the Angel framework. diff --git a/lib/mongo_service.dart b/lib/mongo_service.dart index c836cf40..b8a8219c 100644 --- a/lib/mongo_service.dart +++ b/lib/mongo_service.dart @@ -42,7 +42,7 @@ class MongoService extends Service { item = _removeSensitive(item); try { - item['createdAt'] = new DateTime.now(); + item['createdAt'] = new DateTime.now().toIso8601String(); await collection.insert(item); return await _lastItem(collection, _jsonify, params); } catch (e, st) { @@ -71,7 +71,7 @@ class MongoService extends Service { target is Map ? target : god.serializeObject(target), _removeSensitive(data) ]); - result['updatedAt'] = new DateTime.now(); + result['updatedAt'] = new DateTime.now().toIso8601String(); try { await collection.update(where.id(_makeId(id)), result); @@ -89,8 +89,13 @@ class MongoService extends Service { var target = await read(id, params); Map result = _removeSensitive(data); result['_id'] = _makeId(id); - result['createdAt'] = target is Map ? ['createdAt'] : target.createdAt; - result['updatedAt'] = new DateTime.now(); + result['createdAt'] = + target is Map ? target['createdAt'] : target.createdAt; + + if (result['createdAt'] is DateTime) + result['createdAt'] = result['createdAt'].toIso8601String(); + + result['updatedAt'] = new DateTime.now().toIso8601String(); try { await collection.update(where.id(_makeId(id)), result); @@ -105,7 +110,7 @@ class MongoService extends Service { @override Future remove(id, [Map params]) async { - Map result = await read(id, params); + var result = await read(id, params); try { await collection.remove(where.id(_makeId(id)).and(_makeQuery(params))); diff --git a/lib/mongo_service_typed.dart b/lib/mongo_service_typed.dart index 09b232fe..d1c2d00c 100644 --- a/lib/mongo_service_typed.dart +++ b/lib/mongo_service_typed.dart @@ -9,6 +9,7 @@ class MongoTypedService extends MongoService { } _deserialize(x) { + // print('DESERIALIZE: $x (${x.runtimeType})'); if (x == dynamic || x == Object || x is T) return x; else if (x is Map) { @@ -16,13 +17,30 @@ class MongoTypedService extends MongoService { var value = x[key]; if ((key == 'createdAt' || key == 'updatedAt') && value is String) { - return map..[key] = '44'; // DateTime.parse(value).toIso8601String(); - } else + return map..[key] = DateTime.parse(value).toIso8601String(); + } else if (value is DateTime) { + return map..[key] = value.toIso8601String(); + } else { return map..[key] = value; + } }); - print('x: $x\ndata: $data'); - return god.deserializeDatum(data, outputType: T); + Model result = god.deserializeDatum(data, outputType: T); + + if (x['createdAt'] is String) { + result.createdAt = DateTime.parse(x['createdAt']); + } else if (x['createdAt'] is DateTime) { + result.createdAt = x['createdAt']; + } + + if (x['updatedAt'] is String) { + result.updatedAt = DateTime.parse(x['updatedAt']); + } else if (x['updatedAt'] is DateTime) { + result.updatedAt = x['updatedAt']; + } + + // print('x: $x\nresult: $result'); + return result; } else return x; } @@ -30,8 +48,10 @@ class MongoTypedService extends MongoService { _serialize(x) { if (x is Model) return god.serializeObject(x); - else + else if (x is Map) return x; + else + throw new ArgumentError('Cannot serialize ${x.runtimeType}'); } @override diff --git a/pubspec.yaml b/pubspec.yaml index aa15a840..ba351e7c 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -1,5 +1,5 @@ name: angel_mongo -version: 1.0.0 +version: 1.1.0 description: MongoDB-enabled services for the Angel framework. author: Tobe O homepage: https://github.com/angel-dart/angel_mongo diff --git a/test/generic_test.dart b/test/generic_test.dart index 56e99df6..3adc3eaf 100644 --- a/test/generic_test.dart +++ b/test/generic_test.dart @@ -149,8 +149,7 @@ main() { expect(queried[0].keys.length, equals(3)); expect(queried[0]["id"], equals(world["id"])); expect(queried[0]["to"], equals(world["to"])); - expect(queried[0]["createdAt"], - equals(world["createdAt"].toIso8601String())); + expect(queried[0]["createdAt"], equals(world["createdAt"])); /*response = await client.get("$url/api?\$sort.createdAt=-1"); print(response.body);