diff options
| author | HumairAK <humair88@hotmail.com> | 2016-07-24 20:45:03 +0000 |
|---|---|---|
| committer | HumairAK <humair88@hotmail.com> | 2016-07-24 20:45:03 +0000 |
| commit | d7b7979ee012dde10feae7d64e53fde2847aaf89 (patch) | |
| tree | ba2afca72170274c2ee6a24d0bf2fb7d1771bc25 | |
| parent | bb66f6a9f7d6a2f9d75ab7d42483339618615e03 (diff) | |
added comments to frient-end
| -rw-r--r-- | node_simple.js | 9 | ||||
| -rw-r--r-- | routes/index.js | 5 | ||||
| -rw-r--r-- | routes/user.js | 25 | ||||
| -rw-r--r-- | views/user_solutions.hbs | 138 |
4 files changed, 118 insertions, 59 deletions
diff --git a/node_simple.js b/node_simple.js index 8e7620f..003d4a8 100644 --- a/node_simple.js +++ b/node_simple.js @@ -684,7 +684,7 @@ exports.get_exam_info_by_ID = function (exam_id, callback) { * Params: sol_id - <string> id of the solution to which to add the comment * fields - <[text, by_username]> * */ -exports.add_comment = function (sol_id, fields) { +exports.add_comment = function (sol_id, fields, serverCallback) { var Data = { text: fields[0], date: new Date(), @@ -698,14 +698,19 @@ exports.add_comment = function (sol_id, fields) { var solutions = db.collection('solutions'); // insert data into table solutions.updateOne( {_id: ObjectId(sol_id)}, {$push: {comments: Data}} , function (err, result) { - if (err) throw err; + if (err) { + ServerCallback(false, "Error: Solution found, but could not update comments."); + throw err; + } else { + serverCallback(true, "Comment added successfully"); console.log("comment added"); } }); }) .catch(function (err) { + serverCallback(false, "Error: Could not establish connection with database."); console.error(err); }) }; 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 ********************/ diff --git a/views/user_solutions.hbs b/views/user_solutions.hbs index abf1117..598e63d 100644 --- a/views/user_solutions.hbs +++ b/views/user_solutions.hbs @@ -1,73 +1,101 @@ <main id="solutions-main"> <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> +<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> +<!-- 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> - <!-- Solution 1 Example --> + <!-- 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> - {{#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> +<!-- 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">{{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> +{{#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>{{this.text}}</p> + <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>{{this.commentCount}}</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"> - {{#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}} + <div class="comments"> + <h3> + <a data-toggle="collapse" href="#com{{@index}}"> + <span> + {{this.commentCount}} + </span> + comments + <span class="caret"></span> + </a> + </h3> - </ul> - </div> + <div id="com{{@index}}" 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}} - </div> - </section> - </div> - {{/each}} + <form action="/user/comment/submit/{{../examID}}/{{../qID}}/{{this._id}}" method="post"> + <div class="form-group"> + <textarea type="text" + class="form-control" + placeholder="Enter comment" + name="comment"> + + </textarea> + </div> + <input type="hidden" name="_csrf" value="{{../csrfToken }}"> + <button type="submit" class="btn btn-primary">Submit</button> + </form> + </ul> + </div> + </div> + </section> </div> +{{/each}} + + +</div> </div> </main> |
