From 807eb06e57ba4454bdba32493ae603ef9c2c45a1 Mon Sep 17 00:00:00 2001 From: HumairAK Date: Sun, 24 Jul 2016 00:09:06 -0400 Subject: Added some add_solutions functionality --- routes/index.js | 31 ++++++++++++++++++++++++------- routes/user.js | 55 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 79 insertions(+), 7 deletions(-) (limited to 'routes') 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 96cbd67..ad25ef8 100644 --- a/routes/user.js +++ b/routes/user.js @@ -57,6 +57,61 @@ router.get('/user_profile', loggedIn, isUser, function(req, res, next) { res.render('user_profile_alt', {comments : comments}); }); +/* 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); + console.log("CHECK 7"); + }); + +}); + +/* 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}); req.session.errors = null; -- cgit v1.2.3 From 513312a00975a3fa0848736829e00d46825e853f Mon Sep 17 00:00:00 2001 From: HumairAK Date: Sun, 24 Jul 2016 00:19:27 -0400 Subject: minor fixes --- routes/user.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'routes') diff --git a/routes/user.js b/routes/user.js index ad25ef8..6ae0d80 100644 --- a/routes/user.js +++ b/routes/user.js @@ -91,7 +91,7 @@ router.post('/submit_solution/:examID/:qID', function(req,res){ //Redirect to solutions page again res.redirect('/solutions/' + examID + '/' + qID); - console.log("CHECK 7"); + }); }); -- cgit v1.2.3