From d7b7979ee012dde10feae7d64e53fde2847aaf89 Mon Sep 17 00:00:00 2001 From: HumairAK Date: Sun, 24 Jul 2016 16:45:03 -0400 Subject: added comments to frient-end --- node_simple.js | 9 +++- routes/index.js | 5 +- routes/user.js | 25 +++++++++ views/user_solutions.hbs | 138 ++++++++++++++++++++++++++++------------------- 4 files changed, 118 insertions(+), 59 deletions(-) diff --git a/node_simple.js b/node_simple.js index 8e7620f..003d4a8 100644 --- a/node_simple.js +++ b/node_simple.js @@ -684,7 +684,7 @@ exports.get_exam_info_by_ID = function (exam_id, callback) { * Params: sol_id - id of the solution to which to add the comment * fields - <[text, by_username]> * */ -exports.add_comment = function (sol_id, fields) { +exports.add_comment = function (sol_id, fields, serverCallback) { var Data = { text: fields[0], date: new Date(), @@ -698,14 +698,19 @@ exports.add_comment = function (sol_id, fields) { var solutions = db.collection('solutions'); // insert data into table solutions.updateOne( {_id: ObjectId(sol_id)}, {$push: {comments: Data}} , function (err, result) { - if (err) throw err; + if (err) { + ServerCallback(false, "Error: Solution found, but could not update comments."); + throw err; + } else { + serverCallback(true, "Comment added successfully"); console.log("comment added"); } }); }) .catch(function (err) { + serverCallback(false, "Error: Could not establish connection with database."); console.error(err); }) }; diff --git a/routes/index.js b/routes/index.js index ae7b194..d8cfd36 100644 --- a/routes/index.js +++ b/routes/index.js @@ -2,6 +2,7 @@ var dbFile = require("../node_simple.js"); var express = require('express'); var router = express.Router(); var csrf = require('csurf'); // Cross-Site Request Forgery prevention + var csrfProtection = csrf(); router.use(csrfProtection); // router is protected @@ -114,7 +115,7 @@ router.get('/questions/:exam_id', function (req,res) { // Find q_id in questionsInfo, update comment/solutions count questionsInfo.forEach(function(q){ - if (q.id == question.id){ + if (question.q_id == q._id){ question.count += q.count; question.comments += q.comments; } @@ -187,7 +188,7 @@ router.get('/solutions/:exam_id/:q_num', function (req, res) { solutions.forEach(function(soln){ soln.commentCount = soln.comments.length; }); - res.render('user_solutions', {query: solutions, examID: examID, qID: qID}); + res.render('user_solutions', {query: solutions, examID: examID, qID: qID, csrfToken: req.csrfToken()}); }); }); diff --git a/routes/user.js b/routes/user.js index f88be68..c466e3c 100644 --- a/routes/user.js +++ b/routes/user.js @@ -232,6 +232,31 @@ router.post('/user_profile/send_message', loggedIn, function(req, res, next) { res.redirect('/user/user_profile'); }); +router.post('/comment/submit/:examID/:qID/:solID', function(req, res, next){ + var examID = req.params.examID; + var qID = req.params.qID; + var comment = req.body.comment; + var username = req.user.user_name; + var solutionID = req.params.solID; + // Must be a logged in user to access + console.log(examID + "," + qID + "," + comment + "," + username + "," + solutionID); + var fields = [comment, username]; + if(req.isAuthenticated()){ + dbFile.add_comment(solutionID, fields, function(commentAdded, statusMessage){ + if(commentAdded){ + console.log("Success!"); + } else{ + console.log("Failed to add comment!"); + } + console.log(statusMessage); + res.redirect('/solutions/' + examID + '/' + qID); + }); + + } else { //User not logged in + res.redirect('/'); // Change this to go back to the solutions page with "need to be logged in msg" + } +}); + module.exports = router; /************** Route protection ********************/ diff --git a/views/user_solutions.hbs b/views/user_solutions.hbs index abf1117..598e63d 100644 --- a/views/user_solutions.hbs +++ b/views/user_solutions.hbs @@ -1,73 +1,101 @@
-- cgit v1.2.3