From cbcf2f83d7a11895deeb1234504dd767d8826d4d Mon Sep 17 00:00:00 2001 From: kumar Date: Sat, 23 Jul 2016 23:32:57 -0400 Subject: updated todo lst --- node_simple.js | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/node_simple.js b/node_simple.js index f57b3aa..e18839b 100644 --- a/node_simple.js +++ b/node_simple.js @@ -6,12 +6,16 @@ * 2. add upload date and user name ? DONE * 3. remove exam ? DONE * 4a. get all questions for a given exam_id ? DONE - * 4b. add exam id to each question returned. ? PENDING + * 4b. add exam id to each question returned. ? NO NEED * 6. get all solutions provided question_id and exam_id ? DONE - * 5. solutions ? CURRENTLY WORKING ON -- need to add field for solutions provider, and updating. + * 5. solutions ? DONE -- need to add field for solutions provider, and updating. DONE * 7. add university field to courses, exams ? PENDING * 8. make a user ? DONE * 9. update user info when they comment or post a solution ? PENDING + * 10. comment_history ? DONE + * 11. solutions_history ? DONE + * 12. voting for solutions ? DONE + * * */ -- cgit v1.2.3 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 --- node_simple.js | 20 ++++---- routes/index.js | 31 +++++++++--- routes/user.js | 55 +++++++++++++++++++++ views/add_solutions.hbs | 18 +++++++ views/user_solutions.hbs | 126 +++++++++++++++++++++++------------------------ 5 files changed, 168 insertions(+), 82 deletions(-) create mode 100644 views/add_solutions.hbs diff --git a/node_simple.js b/node_simple.js index 58a114b..bb291cc 100644 --- a/node_simple.js +++ b/node_simple.js @@ -41,10 +41,10 @@ // |..........| -// |================================solutions============================================| -// |_________ _id_____________|exam_id_____________________|q_id_|text____|votes|comments| -// |==========================|============================|=====|========|=====|========| -// |"354ff71ed078933079d6467e"|"578a44ff71ed097fc3079d6e" |1 |"answer"| 1 |[{},{}] | +// |================================solutions========================================================| +// |_________ _id_____________|exam_id_____________________|q_id_|text____|votes|comments | author | +// |==========================|============================|=====|========|=====|===========|========| +// |"354ff71ed078933079d6467e"|"578a44ff71ed097fc3079d6e" |1 |"answer"| 1 |[{},{}] | joe | // |..........| // |========================================users===============================================================================| @@ -525,8 +525,6 @@ exports.get_all_solutions = function (exam_id, q_num, callback) { ).toArray( function (err, docs) { if (err) throw err; else { - console.log(docs); - callback(docs); } }); @@ -556,29 +554,31 @@ exports.add_solution = function (fields, callback) { comments: [], author: fields[3] }; - + console.log("CHECK 1"); // establish a connection mongoFactory.getConnection(uri) .then(function(db) { - + console.log("CHECK 2"); // find the solutions table var solutions = db.collection('solutions'); // insert data into table solutions.insert(Data, function(err) { if (err) callback(false , "Error: Failed to add the solution"); else { + console.log("CHECK 3"); // console.log("solution added"); - callback(true, "Success: added exam successfully!"); + callback(true, "Success: added solution successfully!"); db.close(function (err) { // close the connection when done if (err) throw err; }); + console.log("CHECK 4"); } }); }) .catch(function(err) { console.error(err); }); - + console.log("CHECK 5"); }; /* 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; diff --git a/views/add_solutions.hbs b/views/add_solutions.hbs new file mode 100644 index 0000000..2865a88 --- /dev/null +++ b/views/add_solutions.hbs @@ -0,0 +1,18 @@ +
+ +
+ +
+

Add Solution

+
+
+ +
+ + +
+
+ +
+ +
\ No newline at end of file diff --git a/views/user_solutions.hbs b/views/user_solutions.hbs index 847dfff..abf1117 100644 --- a/views/user_solutions.hbs +++ b/views/user_solutions.hbs @@ -1,77 +1,73 @@
-
+ + + + -- 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 --- node_simple.js | 10 +++++----- routes/user.js | 2 +- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/node_simple.js b/node_simple.js index d9835a8..74a28db 100644 --- a/node_simple.js +++ b/node_simple.js @@ -557,31 +557,31 @@ exports.add_solution = function (fields, callback) { comments: [], author: fields[3] }; - console.log("CHECK 1"); + // establish a connection mongoFactory.getConnection(uri) .then(function(db) { - console.log("CHECK 2"); + // find the solutions table var solutions = db.collection('solutions'); // insert data into table solutions.insert(Data, function(err) { if (err) callback(false , "Error: Failed to add the solution"); else { - console.log("CHECK 3"); + // console.log("solution added"); callback(true, "Success: added solution successfully!"); db.close(function (err) { // close the connection when done if (err) throw err; }); - console.log("CHECK 4"); + } }); }) .catch(function(err) { console.error(err); }); - console.log("CHECK 5"); + }; 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