aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorkumar <kumar.damani@mail.utoronto.ca>2016-07-24 00:55:56 +0000
committerkumar <kumar.damani@mail.utoronto.ca>2016-07-24 00:55:56 +0000
commitf94e655f733e3be2a5fa2b009e9899c1b8886f03 (patch)
treee6f309c9439a3a06a2ab7b093d28f2ab669ba965
parent17d743822746b047b94cd24e5407f1342ba01a7e (diff)
exam following and retrieveing -no comments added yet tho
-rw-r--r--node_simple.js63
-rw-r--r--testImports.js19
2 files changed, 80 insertions, 2 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) {
diff --git a/testImports.js b/testImports.js
index 90ed230..8946a2f 100644
--- a/testImports.js
+++ b/testImports.js
@@ -40,7 +40,7 @@ var questions_array = ["this is q1", "this is q2"];
});*/
-dbFile.retrieve_userSolutions_history("some_user name", function (bool, result) {
+/*dbFile.retrieve_userSolutions_history("some_user name", function (bool, result) {
if (bool == false) {
console.log(result);
}
@@ -54,7 +54,7 @@ dbFile.retrieve_userSolutions_count("some_user name", function (bool, result) {
else { // bool is true i.e no error occured. result contains an integer
console.log(result);
}
-});
+});*/
/*dbFile.retrieve_userComments_history("some_user name", function (bool, results) {
if (!bool) console.log(results);
@@ -114,3 +114,18 @@ dbFile.retrieve_userSolutions_count("some_user name", function (bool, result) {
}
});*/
+
+dbFile.followExam("nanalelfe@gmail.com", "578a44ff71ed097fc3079d6e", function (bool, mssg) {
+ if (!bool) console.log(mssg);
+ else {
+ console.log(mssg);
+ }
+});
+
+/*
+dbFile.retrieveFollows("some_user names", function (bool, result) {
+ if (!bool) console.log(result);
+ else {
+ console.log(result);
+ }
+ });*/