2016-09-21 04:26:22 +00:00
|
|
|
part of angel_mongo.services;
|
2016-05-09 22:51:07 +00:00
|
|
|
|
2016-06-22 18:34:28 +00:00
|
|
|
/// Manipulates data from MongoDB as Maps.
|
2016-05-09 22:51:07 +00:00
|
|
|
class MongoService extends Service {
|
|
|
|
DbCollection collection;
|
|
|
|
|
2016-06-23 04:58:21 +00:00
|
|
|
MongoService(DbCollection this.collection) : super();
|
2016-06-22 18:34:28 +00:00
|
|
|
|
|
|
|
_jsonify(Map doc, [Map params]) {
|
2016-05-09 22:51:07 +00:00
|
|
|
Map result = {};
|
|
|
|
for (var key in doc.keys) {
|
|
|
|
if (doc[key] is ObjectId) {
|
|
|
|
result[key] = doc[key].toHexString();
|
2016-06-22 18:34:28 +00:00
|
|
|
} else
|
|
|
|
result[key] = doc[key];
|
2016-05-09 22:51:07 +00:00
|
|
|
}
|
|
|
|
|
2016-06-22 18:34:28 +00:00
|
|
|
return _transformId(result);
|
2016-05-09 22:51:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
Future<List> index([Map params]) async {
|
|
|
|
return await (await collection.find(_makeQuery(params)))
|
2016-06-22 18:34:28 +00:00
|
|
|
.map((x) => _jsonify(x, params))
|
2016-05-09 22:51:07 +00:00
|
|
|
.toList();
|
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
2016-06-22 18:34:28 +00:00
|
|
|
Future create(Map data, [Map params]) async {
|
|
|
|
Map item = (data is Map) ? data : god.serializeObject(data);
|
|
|
|
item = _removeSensitive(item);
|
2016-05-09 22:51:07 +00:00
|
|
|
|
|
|
|
try {
|
2016-06-22 18:34:28 +00:00
|
|
|
item['createdAt'] = new DateTime.now();
|
|
|
|
await collection.insert(item);
|
|
|
|
return await _lastItem(collection, _jsonify, params);
|
|
|
|
} catch (e, st) {
|
|
|
|
throw new AngelHttpException(e, stackTrace: st);
|
2016-05-09 22:51:07 +00:00
|
|
|
}
|
2016-06-22 18:34:28 +00:00
|
|
|
}
|
2016-05-09 22:51:07 +00:00
|
|
|
|
2016-06-22 18:34:28 +00:00
|
|
|
@override
|
|
|
|
Future read(id, [Map params]) async {
|
|
|
|
ObjectId _id = _makeId(id);
|
|
|
|
Map found = await collection.findOne(where.id(_id).and(_makeQuery(params)));
|
2016-05-09 22:51:07 +00:00
|
|
|
|
|
|
|
if (found == null) {
|
|
|
|
throw new AngelHttpException.NotFound(
|
2016-06-22 18:34:28 +00:00
|
|
|
message: 'No record found for ID ${_id.toHexString()}');
|
2016-05-09 22:51:07 +00:00
|
|
|
}
|
|
|
|
|
2016-06-22 18:34:28 +00:00
|
|
|
return _jsonify(found, params);
|
2016-05-09 22:51:07 +00:00
|
|
|
}
|
2016-06-22 18:34:28 +00:00
|
|
|
|
|
|
|
@override
|
|
|
|
Future modify(id, Map data, [Map params]) async {
|
|
|
|
Map target = await read(id, params);
|
|
|
|
Map result = mergeMap([target, _removeSensitive(data)]);
|
|
|
|
result['updatedAt'] = new DateTime.now();
|
|
|
|
|
|
|
|
try {
|
|
|
|
await collection.update(where.id(_makeId(id)), result);
|
|
|
|
result = _jsonify(result, params);
|
|
|
|
result['id'] = id;
|
|
|
|
return result;
|
|
|
|
} catch (e, st) {
|
|
|
|
throw new AngelHttpException(e, stackTrace: st);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
Future update(id, data, [Map params]) async {
|
|
|
|
Map target = await read(id, params);
|
|
|
|
Map result = _removeSensitive(data);
|
|
|
|
result['_id'] = _makeId(id);
|
|
|
|
result['createdAt'] = target['createdAt'];
|
|
|
|
result['updatedAt'] = new DateTime.now();
|
|
|
|
|
|
|
|
try {
|
|
|
|
await collection.update(where.id(_makeId(id)), result);
|
|
|
|
result = _jsonify(result, params);
|
|
|
|
result['id'] = id;
|
|
|
|
return result;
|
|
|
|
} catch (e, st) {
|
|
|
|
throw new AngelHttpException(e, stackTrace: st);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-06-23 04:58:21 +00:00
|
|
|
@override
|
|
|
|
Future remove(id, [Map params]) async {
|
|
|
|
Map result = await read(id, params);
|
2016-06-22 18:34:28 +00:00
|
|
|
|
2016-06-23 04:58:21 +00:00
|
|
|
try {
|
|
|
|
await collection.remove(where.id(_makeId(id)).and(_makeQuery(params)));
|
|
|
|
return result;
|
|
|
|
} catch (e, st) {
|
|
|
|
throw new AngelHttpException(e, stackTrace: st);
|
|
|
|
}
|
|
|
|
}
|
2016-05-09 22:51:07 +00:00
|
|
|
}
|