forked from tntp/Tntp.CodingExercise
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdata.js
More file actions
47 lines (41 loc) · 1.75 KB
/
data.js
File metadata and controls
47 lines (41 loc) · 1.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
var MongoClient = require('mongodb').MongoClient,
settings = require('./config.js'),
Promise = require('promise'),
Guid = require('guid');
var fullMongoUrl = settings.mongoConfig.serverUrl + settings.mongoConfig.database;
var exports = module.exports = {};
MongoClient.connect(fullMongoUrl)
.then(function(db) {
var chat_collection = db.collection("tntpchatroomdb1");
// setup your body
exports.createComment = function(_comment, _name) {
console.log('In Create Comment');
// throws an error if there has been invalid input
if (_comment == '' || _comment == undefined) {
console.log('Comment was empty.');
return Promise.reject('Can not enter EMPTY string');
} else if (_name == '' || _name == undefined) {
console.log('name was empty.');
return Promise.reject('name can not be null.');
} else {
// return a promise that resolves the new comment
var _date = new Date();
console.log('Create a post at : ' + _date);
var jsonData = {
_id: Guid.create().toString(),
comment: _comment,
name: _name,
date: _date
};
return chat_collection.insertOne(jsonData).then(function(newDoc) {
return newDoc.insertedId;
});
}
};
exports.getAllComments = function() {
return chat_collection.find().sort({ date: -1 }).toArray().then(function(params) {
//console.log(" All posts : " + JSON.stringify(params));
return params;
});
}
});