diff options
Diffstat (limited to 'routes')
| -rw-r--r-- | routes/index.js | 5 | ||||
| -rw-r--r-- | routes/user.js | 25 |
2 files changed, 28 insertions, 2 deletions
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 ********************/ |
