diff options
| author | kumar <kumar.damani@mail.utoronto.ca> | 2016-07-23 04:12:55 +0000 |
|---|---|---|
| committer | kumar <kumar.damani@mail.utoronto.ca> | 2016-07-23 04:12:55 +0000 |
| commit | 9ab4094797e82798eab40b32e7c71b52bb847b8f (patch) | |
| tree | d1fa9e85cd0278f73689e298a72dca2784fd2238 /node_simple.js | |
| parent | e4a06d8a5f6af3be38333f5c1bb551fcbd6255cc (diff) | |
added comment and solutions history fetch for a given username -- for profile
Diffstat (limited to 'node_simple.js')
| -rw-r--r-- | node_simple.js | 96 |
1 files changed, 91 insertions, 5 deletions
diff --git a/node_simple.js b/node_simple.js index cc09be4..99079d5 100644 --- a/node_simple.js +++ b/node_simple.js @@ -82,6 +82,90 @@ var uri = exports.uri = 'mongodb://general:assignment4@ds057862.mlab.com:57862/ //****************************FUNCTIONS************************************************| + +// will add comments ASAP +exports.retrieve_userComments_history = function (username, callback) { + + // get a connection + mongoFactory.getConnection(uri).then(function (db) { + + var solutions = db.collection('solutions'); + solutions.aggregate([ + + { $match : { + "comments.by": username + }}, + { $unwind : "$comments" }, + { $match : { + "comments.by": username + }}, + {$project: { + comment: "$comments.text", + date: "$comments.date", + exam_id: "$exam_id", + _id: 0 + }} + ]).toArray(function (err, results) { + if (err) callback(false, "Error: some wierd error occured while query"); + else { + callback(true, results); + } + + }); + + db.close(); + + }).catch(function (err) { + // console.err(err); + callback(false, "Error: failed to connect to db"); + }) +}; + +exports.retrieve_userComments_count = function (username, callback) { + + exports.retrieve_userComments_history(username, function (bool, results) { + if (!bool) callback(false, "Error: error occured"); + else { + var length = results.length; + callback(true, length); + } + }); + +}; + +exports.retrieve_userSolutions_history = function (username, callback) { + + // get a connection + mongoFactory.getConnection(uri).then(function (db) { + + var solutions = db.collection('solutions'); + solutions.find( { author: username } ).toArray(function (err, result) { + if (err) callback(false, "Error: problem while looking for stuff"); + else { + callback(true, result); + } + db.close(); + }); + + }).catch(function (err) { + // console.err(err); + callback(false, "Error: failed to connect to db"); + }) +}; + +exports.retrieve_userSolutions_count = function (username, callback) { + + exports.retrieve_userSolutions_history(username, function (bool, results) { + if (!bool) callback(false, "Error: error occured"); + else { + var length = results.length; + callback(true, length); + } + }); + +}; + + /* * This function creates and adds a user to users table. * IFF both the email and the user_name are not in the database already. @@ -374,18 +458,19 @@ exports.get_all_solutions = function (exam_id, q_num, callback) { /* * This function will add a solution to the solutions table in the database . - * Params: fields - [exam_id , question_id, solution text] + * Params: fields - [exam_id , question_id, solution text, user_name] * Note: exam_id - is a unique identifier for each exam in the database. to see an example, ... * ... call get_all_exams and look at the output. Looks like: 578a44ff71ed097fc3079d6e * question_id - is unique relevant to 1 exam. * */ -exports.add_solution = function (fields) { +exports.add_solution = function (fields, callback) { var Data = { exam_id: fields[0], q_id: fields[1], text: fields[2], votes: 0, - comments: [] + comments: [], + author: fields[3] }; // establish a connection @@ -396,9 +481,10 @@ exports.add_solution = function (fields) { var solutions = db.collection('solutions'); // insert data into table solutions.insert(Data, function(err) { - if (err) throw err; + if (err) callback(false , "Error: Failed to add the solution"); else { - console.log("solution added"); + // console.log("solution added"); + callback(true, "Success: added exam successfully!"); db.close(function (err) { // close the connection when done if (err) throw err; }); |
