diff options
Diffstat (limited to 'routes')
| -rw-r--r-- | routes/index.js | 31 | ||||
| -rw-r--r-- | routes/user.js | 53 |
2 files changed, 77 insertions, 7 deletions
diff --git a/routes/index.js b/routes/index.js index 359ba4f..6d5fdac 100644 --- a/routes/index.js +++ b/routes/index.js @@ -1,6 +1,9 @@ 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 // Remove later var passport_file = require('../config/passport.js'); @@ -158,6 +161,7 @@ router.get('/questions/:exam_id', function (req,res) { * text: "answer" * votes: 1 (int) * comments: [{}.{}] (just going to be string for now i think) + * author : "text" * } * { * _id: ... @@ -166,20 +170,33 @@ router.get('/questions/:exam_id', function (req,res) { * text: .. * votes: .. comments: .. + author : "text" * * } - * ]*/ + * ] + * "comments": [] + * + A comment: + { + "text": "this is asdfasdf", + "date": {"$date": "2016-07-23T03:55:34.906Z"}, + "by": "some_user name" + }, + * + */ + router.get('/solutions/:exam_id/:q_num', function (req, res) { - dbFile.get_all_solutions(req.params.exam_id,req.params.q_num,function (solutions) { - res.render('user_solutions', {query: solutions}); + var examID = req.params.exam_id; + var qID = req.params.q_num; + dbFile.get_all_solutions(examID, qID, function (solutions) { + solutions.forEach(function(soln){ + soln.commentCount = soln.comments.length; + }); + res.render('user_solutions', {query: solutions, examID: examID, qID: qID}); console.log(solutions); }); }); -router.get('/add_solution',function (req, res) { - redirect('/add_solutions_page'); -}); - router.post('/add_solutions/submit', function (req, res) { //TODO: get the form information for the solutions }); diff --git a/routes/user.js b/routes/user.js index 5fb7fb9..7d2c199 100644 --- a/routes/user.js +++ b/routes/user.js @@ -79,6 +79,59 @@ router.get('/user_profile', loggedIn, isUser, function(req, res, next) { res.render('user_profile_alt', {comments : comments, inbox: inbox}); }); +/* Adds a solution into the database, redirect to exam/question/solution page */ +router.post('/submit_solution/:examID/:qID', function(req,res){ + // Get required fields from body + // Populate this data + /* var Data = { + exam_id: fields[0], + q_id: fields[1], + text: fields[2], + votes: 0, + comments: [], + author: fields[3] + };*/ + + var examID = req.params.examID; + var qID = req.params.qID; + + var text = req.body.solution, + votes = 0, + comments = [], + author = req.user.user_name; + + var fields = [examID, qID, text, votes, comments, author]; + console.log(fields); + // Add to database + dbFile.add_solution(fields, function(addedSolution, statusMsg){ + if(addedSolution){ + console.log("Success!"); + }else{ + console.log("Failed to add solution!"); + } + console.log(statusMsg); + + //Redirect to solutions page again + res.redirect('/solutions/' + examID + '/' + qID); + + }); + +}); + +/* Routed here from Solutions page (add solution button) +* Renders add_solution page for response.*/ +router.get('/add_solution/:examID/:qID', function (req, res, next) { + var examID = req.params.examID; + var qID = req.params.qID; + + // Must be a logged in user to access + if(req.isAuthenticated()){ + res.render('add_solutions', {csrfToken: req.csrfToken(), examID: examID, qID: qID}); + } else { + res.redirect('/'); // Change this to go back to the solutions page + } + +}); router.get('/signup', loggedOut, function(req, res, next) { res.render('signup', {csrfToken: req.csrfToken(), success: req.session.success, errors: req.session.errors}); |
