From f94e655f733e3be2a5fa2b009e9899c1b8886f03 Mon Sep 17 00:00:00 2001 From: kumar Date: Sat, 23 Jul 2016 20:55:56 -0400 Subject: exam following and retrieveing -no comments added yet tho --- node_simple.js | 63 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ testImports.js | 19 ++++++++++++++++-- 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); + } + });*/ -- cgit v1.2.3 From 3dc5f9d58a548dcace9e9367caa5d0e18eacae92 Mon Sep 17 00:00:00 2001 From: HumairAK Date: Sat, 23 Jul 2016 21:06:00 -0400 Subject: adjusted callback for questions route --- routes/index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/routes/index.js b/routes/index.js index d3c55e6..359ba4f 100644 --- a/routes/index.js +++ b/routes/index.js @@ -96,7 +96,7 @@ router.get('/search', function(req, res, next) { router.get('/questions/:exam_id', function (req,res) { var examID = req.params.exam_id; console.log(examID); - dbFile.get_exam_byID(examID, function(exam){ + dbFile.get_exam_byID(examID, function(success, error, exam){ /* [ { q_id: 1, question: 'this is q1' }, -- cgit v1.2.3 From beb24e45b6ea43bb7f89b38631047df17a9931df Mon Sep 17 00:00:00 2001 From: kumar Date: Sat, 23 Jul 2016 21:38:32 -0400 Subject: added update vote count of solutions --- node_simple.js | 30 ++++++++++++++++++++++++++++-- testImports.js | 9 +++++++-- 2 files changed, 35 insertions(+), 4 deletions(-) diff --git a/node_simple.js b/node_simple.js index 58a114b..f57b3aa 100644 --- a/node_simple.js +++ b/node_simple.js @@ -11,7 +11,7 @@ * 5. solutions ? CURRENTLY WORKING ON -- need to add field for solutions provider, and updating. * 7. add university field to courses, exams ? PENDING * 8. make a user ? DONE - * 9. + * 9. update user info when they comment or post a solution ? PENDING * */ @@ -482,7 +482,7 @@ exports.get_exam_info_by_ID = function (exam_id, callback) { /* * This function will add a comment to the solutions table * Params: sol_id - id of the solution to which to add the comment - * fields - [text, by] + * fields - [text, by_username] * */ exports.add_comment = function (sol_id, fields) { var Data = { @@ -581,6 +581,32 @@ exports.add_solution = function (fields, callback) { }; + + + +exports.vote_solution = function (sol_id, upORdown , callback) { + var vote = (upORdown == "up") ? 1 : -1; + + mongoFactory.getConnection(uri).then(function (db) { + var solutions = db.collection('solutions'); + solutions.updateOne( + {_id: ObjectId(sol_id) }, + { $inc: { votes: vote} }, function (err) { + // if (err) throw err; + if (err) callback(false, "Error: couldnt update the vote count"); + else { + callback(true, "Success: updated vote count"); + } + }); + db.close(); + }).catch(function (err) { + console.log(err); + }); +}; + + + + /* * This function will retrieve all exams in the database given the course code ... * ... ordered by the year of the exam. diff --git a/testImports.js b/testImports.js index 8946a2f..7546f06 100644 --- a/testImports.js +++ b/testImports.js @@ -115,12 +115,12 @@ dbFile.retrieve_userSolutions_count("some_user name", function (bool, result) { });*/ -dbFile.followExam("nanalelfe@gmail.com", "578a44ff71ed097fc3079d6e", function (bool, mssg) { +/*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) { @@ -129,3 +129,8 @@ dbFile.retrieveFollows("some_user names", function (bool, result) { console.log(result); } });*/ + + +/*dbFile.vote_solution("5792d8a970040378d4e4b389" , "down", function (bool, mssg) { + console.log(mssg); +});*/ -- cgit v1.2.3