aboutsummaryrefslogtreecommitdiff
path: root/node_simple.js
diff options
context:
space:
mode:
authorHumairAK <humair88@hotmail.com>2016-07-23 04:15:44 +0000
committerHumairAK <humair88@hotmail.com>2016-07-23 04:15:44 +0000
commit29e3a33a09be459a16248cbe6eabfccd8f9f4406 (patch)
treee0068efb1955e9cd44e9507b75190f49e25a53be /node_simple.js
parent08553e26f159f725c6a750f7503c1a5e7f89d608 (diff)
parentf658b2367b9ea33703f3b4174688a96e682d86c8 (diff)
Merge branch 'master' of https://github.com/HumairAK/solutions_repo
Diffstat (limited to 'node_simple.js')
-rw-r--r--node_simple.js98
1 files changed, 92 insertions, 6 deletions
diff --git a/node_simple.js b/node_simple.js
index e7d619a..16c84ed 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.
* */
@@ -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;
});