diff options
| author | nanalelfe <nargiza.nosirova@mail.utoronto.ca> | 2016-07-24 05:51:20 +0000 |
|---|---|---|
| committer | nanalelfe <nargiza.nosirova@mail.utoronto.ca> | 2016-07-24 05:51:20 +0000 |
| commit | 27dfe48c9d4f7cb224fe96082cfbe05be23c3a41 (patch) | |
| tree | 86c2ecd50e5e078be0f70044c1eb19d9b2e1f39a | |
| parent | 0c785ab730997972b573c48d25f958dea5ad62b9 (diff) | |
| parent | 513312a00975a3fa0848736829e00d46825e853f (diff) | |
Merged
| -rw-r--r-- | node_simple.js | 22 | ||||
| -rw-r--r-- | routes/index.js | 31 | ||||
| -rw-r--r-- | routes/user.js | 53 | ||||
| -rw-r--r-- | views/add_solutions.hbs | 18 | ||||
| -rw-r--r-- | views/user_solutions.hbs | 126 |
5 files changed, 169 insertions, 81 deletions
diff --git a/node_simple.js b/node_simple.js index cf5c372..f2787f7 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 + * * */ @@ -41,10 +45,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 +529,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); } }); @@ -565,11 +567,13 @@ exports.add_solution = function (fields, callback) { solutions.insert(Data, function(err) { if (err) callback(false , "Error: Failed to add the solution"); else { + // 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; }); + } }); }) 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}); 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 @@ +<main id="solutions-main"> + +<div class="row" id="solutions-main"> + +<div class="col-md-4 col-md-offset-4 col-sm-6 col-sm-offset-3"> + <h3><i class="fa fa-wpforms" aria-hidden="true"></i> Add Solution </h3> + <form action="/user/submit_solution/{{examID}}/{{qID}}" method="post"> + <div class="form-group"> + <input type="text" class="form-control" placeholder="Enter Solution" name="solution"> + </div> + <input type="hidden" name="_csrf" value="{{ csrfToken }}"> + <button class="btn btn-primary">Submit</button> + </form> +</div> + +</div> + +</main>
\ 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 @@ <main id="solutions-main"> - <div class='search solutions-container container'> - <div> - <!-- Add Solution button and path --> - <div class="row top-sol"> - <button class="col-xs-4 col-sm-2 col-md-2 col-lg-2 - col-xs-offset-4 col-sm-offset-5 col-md-offset-2 - col-lg-offset-2 rounded" id="add-solution"> - <h3>Add a solution</h3> - </button> +<div class='search solutions-container container'> + <div> + <!-- Add Solution button and path --> + <div class="row top-sol"> + <a href="/user/add_solution/{{examID}}/{{qID}}"> + <button class="col-xs-4 col-sm-2 col-md-2 col-lg-2 + col-xs-offset-4 col-sm-offset-5 col-md-offset-2 + col-lg-offset-2 rounded" id="add-solution"> + <h3>Add a solution</h3> + </button> + </a> - <!-- Replace p below with breadcrumbs later --> - <p class="col-xs-12 col-sm-12 col-md-6 show-path"> - CSC148 > Winter 2015 > Question 1 - </p> - </div> + <!-- Replace p below with breadcrumbs later --> + <p class="col-xs-12 col-sm-12 col-md-6 show-path"> + CSC148 > Winter 2015 > Question 1 + </p> + </div> - <!-- Solution 1 Example --> - <div class="row"> - <section id="sol-" class="col-md-10 col-md-offset-1 rounded"> - <div class="post-info"> - <div class=""> - <img class="img-responsive img-circle user-image" src="assets/images/misc/user1.jpeg"> - <p class="username"><a href="/user_profile">jSmith123</a></p> - </div> + <!-- Solution 1 Example --> - <div class="pull-right"> - <div class="upvoting"> - <button class="upvote"><img src="assets/images/up_arrow.png"></button> - <p class="upvote num-upvotes">3</p> - </div> - <div class="downvoting"> - <button class="downvote"><img src="assets/images/down_arrow.png"></button> - <p class=" downvote num-downvotes">-1</p> - </div> - </div> - </div> + {{#each query}} + <div class="row"> + <section id="sol-" class="col-md-10 col-md-offset-1 rounded"> + <div class="post-info"> + <div class=""> + <img class="img-responsive img-circle user-image" src="/assets/images/misc/user1.jpeg"> + <p class="username">{{this.author}}</p> + </div> - <div class="user-sol"> - <h3>Solution</h3> - <p><img src="assets/images/dummy-sol1.png"> </p> - </div> + <div class="pull-right"> + <div class="upvoting"> + <button class="upvote"><img src="/assets/images/up_arrow.png"></button> + <p class="upvote num-upvotes">{{this.votes}}</p> + </div> + <div class="downvoting"> + <button class="downvote"><img src="/assets/images/down_arrow.png"></button> + <p class=" downvote num-downvotes">-1</p> + </div> + </div> + </div> - <div class="comments"> - <h3><a data-toggle="collapse" href="#com1"><span>3</span> comments <span class="caret"></span></a></h3> + <div class="user-sol"> + <h3>Solution</h3> + <p>{{this.text}}</p> + </div> - <div id="com1" class="panel-collapse collapse"> - <ul class="list-group"> - <li class="list-group-item"> - <img src="assets/images/misc/user3.jpeg" class="img-responsive img-circle comment-user-img"> - <p class="comment-username"><a href="/user_profile">janeDoe456</a></p> - <p class="user-comment"> This is a good answer!</p> - </li> + <div class="comments"> + <h3><a data-toggle="collapse" href="#com1"><span>{{this.commentCount}}</span> comments <span class="caret"></span></a></h3> - <li class="list-group-item"> - <img src="assets/images/misc/user2.jpeg" class="img-responsive img-circle comment-user-img"> - <p class="comment-username"><a href="/user_profile">BenAfleckIsAnOkActor</a></p> - <p class="user-comment">Ben Affleck is indeed just an ok actor.</p> - </li> + <div id="com1" class="panel-collapse collapse"> + <ul class="list-group"> + {{#each this.comments}} + <li class="list-group-item"> + <img src="/assets/images/misc/user3.jpeg" class="img-responsive img-circle comment-user-img"> + <p class="comment-username"><a href="/user_profile">{{this.by}}</a>, {{this.date}}</p> + <p class="user-comment">{{this.text}}</p> + </li> + {{/each}} - <li class="list-group-item"> - <img src="assets/images/misc/user6.jpeg" class="img-responsive img-circle comment-user-img"> - <p class="comment-username"><a href="/user_profile">morgana254</a></p> - <p class="user-comment">I mean, he's no Casey Affleck.</p> - </li> + </ul> + </div> - </ul> - </div> + </div> + </section> + </div> + {{/each}} - </div> - - </section> - </div> - </div> - </div> - </main> + </div> +</div> +</main> + |
