diff options
| author | nanalelfe <nargiza.nosirova@mail.utoronto.ca> | 2016-07-24 03:10:12 +0000 |
|---|---|---|
| committer | nanalelfe <nargiza.nosirova@mail.utoronto.ca> | 2016-07-24 03:10:12 +0000 |
| commit | 43f7514bb2ae00d3805f1468f89bbdab6fc595fa (patch) | |
| tree | 94832654416ffcd8bcd430428479b4c931794b91 | |
| parent | 64d9ccb6ae21a12d6fbd61b371397d8d5f2e783f (diff) | |
| parent | c5fa21db9ad2b129adceacddfbde62724d15886c (diff) | |
Merge branch 'master' of https://github.com/HumairAK/solutions_repo
| -rw-r--r-- | node_simple.js | 93 | ||||
| -rw-r--r-- | routes/index.js | 2 | ||||
| -rw-r--r-- | testImports.js | 24 |
3 files changed, 114 insertions, 5 deletions
diff --git a/node_simple.js b/node_simple.js index fe045d2..2976b58 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 * */ @@ -94,6 +94,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 /* callback(success, data/message) => callback(boolean, object/String); @@ -420,7 +483,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 = { @@ -517,6 +580,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/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' }, diff --git a/testImports.js b/testImports.js index 90ed230..7546f06 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,23 @@ 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); + } + });*/ + + +/*dbFile.vote_solution("5792d8a970040378d4e4b389" , "down", function (bool, mssg) { + console.log(mssg); +});*/ |
