diff options
| author | kumar <kumar.damani@mail.utoronto.ca> | 2016-07-24 00:55:56 +0000 |
|---|---|---|
| committer | kumar <kumar.damani@mail.utoronto.ca> | 2016-07-24 00:55:56 +0000 |
| commit | f94e655f733e3be2a5fa2b009e9899c1b8886f03 (patch) | |
| tree | e6f309c9439a3a06a2ab7b093d28f2ab669ba965 /node_simple.js | |
| parent | 17d743822746b047b94cd24e5407f1342ba01a7e (diff) | |
exam following and retrieveing -no comments added yet tho
Diffstat (limited to 'node_simple.js')
| -rw-r--r-- | node_simple.js | 63 |
1 files changed, 63 insertions, 0 deletions
diff --git a/node_simple.js b/node_simple.js index 2c57d69..0976791 100644 --- a/node_simple.js +++ b/node_simple.js @@ -93,6 +93,69 @@ var uri = exports.uri = 'mongodb://general:assignment4@ds057862.mlab.com:57862/ //****************************FUNCTIONS************************************************| +exports.followExam = function (user_name, exam_id, callback) { + + exports.retrieveFollows(user_name, function (bool, result) { + if (!bool) console.log(result); + else { // no err occured so far... + + var found = false; + for (var i = 0; i < result.length; i++) { // search through the list of exams followed + if (result[i] == exam_id) { + found = true; + } + } + + if (found) { // means exam is already followed by user + callback(false, "user is already following this exam"); + } + + else { // add it to the user follower list + mongoFactory.getConnection(uri).then(function (db) { + + // find the user table + var users = db.collection('users'); + // insert data into table + users.updateOne( {user_name: user_name}, {$push: {followers: exam_id}} , function (err) { + // if (err) throw err; + if (err) callback(false, "Error: some error occurred while following the exam"); + else { + // console.log("user is following this exam"); + callback(true, "Success: user is following this exam"); + } + }); + db.close(); + + }).catch(function (err) { + console.error(err); + }); + } + } + }); +}; + + +exports.retrieveFollows = function (user_name, callback) { + + mongoFactory.getConnection(uri).then(function (db) { + + // find the solutions table + var users = db.collection('users'); + // insert data into table + users.find( {user_name: user_name} ).toArray(function (err, docs) { + // if (err) throw err; + if (err) callback(false, "Error: followers could not be retrieved for some reason"); + else { + callback(true, docs[0].followers); + } + }); + + // db.close(); + }).catch(function (err) { + console.error(err); + }); +}; + // will add comments ASAP exports.retrieve_userComments_history = function (username, callback) { |
