diff options
| -rw-r--r-- | node_simple.js | 24 | ||||
| -rw-r--r-- | routes/admin.js | 2 | ||||
| -rw-r--r-- | routes/user.js | 42 | ||||
| -rw-r--r-- | views/user_profile_alt.hbs | 39 |
4 files changed, 76 insertions, 31 deletions
diff --git a/node_simple.js b/node_simple.js index 2c57d69..c3431ed 100644 --- a/node_simple.js +++ b/node_simple.js @@ -95,6 +95,10 @@ var uri = exports.uri = 'mongodb://general:assignment4@ds057862.mlab.com:57862/ // will add comments ASAP +/* callback(success, data/message) => callback(boolean, object/String); + * + * + */ exports.retrieve_userComments_history = function (username, callback) { // get a connection @@ -117,7 +121,7 @@ exports.retrieve_userComments_history = function (username, callback) { _id: 0 }} ]).toArray(function (err, results) { - if (err) callback(false, "Error: some wierd error occured while query"); + if (err) callback(false, "Error: some weird error occurred while query"); else { callback(true, results); } @@ -135,7 +139,7 @@ exports.retrieve_userComments_history = function (username, callback) { exports.retrieve_userComments_count = function (username, callback) { exports.retrieve_userComments_history(username, function (bool, results) { - if (!bool) callback(false, "Error: error occured"); + if (!bool) callback(false, "Error: error occurred"); else { var length = results.length; callback(true, length); @@ -828,9 +832,10 @@ exports.remove_exam = function (fields, serverCallback) { }); }; -//get_exam_byID("578a44ff71ed097fc3079d6e"); -exports.get_exam_byID = function (id) { + +// callback(success, error, data) +exports.get_exam_byID = function (id, callback) { // establish a connection mongoFactory.getConnection(uri) @@ -840,15 +845,20 @@ exports.get_exam_byID = function (id) { var exams = db.collection('exams'); // query exams.find( {_id: ObjectId(id)} ).toArray(function (err, docs) { - if (err) throw err; + if (err) { + callback(false, true, null); + } else if (!docs) { + callback(false, false, null); + } else { // console.log(JSON.stringify(docs, null, 2)); - console.log(docs); + //console.log(docs); + callback(true, false, docs[0]); } }); }) .catch(function(err) { - console.error(err); + callback(false, true, null); }); }; diff --git a/routes/admin.js b/routes/admin.js index e038fa3..5aa22fe 100644 --- a/routes/admin.js +++ b/routes/admin.js @@ -130,7 +130,7 @@ module.exports = router; /************** Route protection ********************/ function isAdmin(req, res, next) { - if (!req.user.email){ + if (req.user && !req.user.email){ return next(); } res.redirect('/'); diff --git a/routes/user.js b/routes/user.js index 325b8ff..96cbd67 100644 --- a/routes/user.js +++ b/routes/user.js @@ -3,6 +3,8 @@ var router = express.Router(); var csrf = require('csurf'); // Cross-Site Request Forgery prevention var passport = require('passport'); +var dbFile = require("../node_simple.js"); + var csrfProtection = csrf(); router.use(csrfProtection); // router is protected @@ -14,7 +16,45 @@ router.get('/logout', loggedIn, function (req, res, next) { /* Render/GET user_profile page */ router.get('/user_profile', loggedIn, isUser, function(req, res, next) { - res.render('user_profile_alt'); + // GET COMMENTS + var comments = []; + dbFile.retrieve_userComments_history(req.user.user_name, function (success, object) { + if (!success) { + // Need to redirect to an error page instead. + console.log("Could not retrieve comments."); + } else if (object){ + comments = object; + for (var comment in comments) { + dbFile.get_exam_byID(comment.exam_id, function(success, error, data) { + if (success) { + comment.exam_info = { + course_code : data.course_code, + term: data.term, + year : data.year + }; + } + }); + } + + } + }); + // For testing purposes, remove later: + if (!comments.length) { + var obj = { + comment: 'Use a heap!', + date: '2016-04-05', + exam_id: 'some id', + exam_info : { + course_code : 'CSC263', + term: 'Fall', + year : 2016 + } + }; + comments.push(obj); + comments.push(obj); + } + + res.render('user_profile_alt', {comments : comments}); }); router.get('/signup', loggedOut, function(req, res, next) { diff --git a/views/user_profile_alt.hbs b/views/user_profile_alt.hbs index 413fd0f..6455cc6 100644 --- a/views/user_profile_alt.hbs +++ b/views/user_profile_alt.hbs @@ -187,7 +187,9 @@ </div> </div> <div class="row" id="profile-comments"> - <h3>Comment History</h3> + + {{# if comments }} + <h3>Comment History</h3> <div class="col-sm-10 col-sm-offset-1 prf-tables"> <table class="table"> <thead> @@ -198,30 +200,23 @@ </tr> </thead> <tbody> - <tr> - <td>2016-05-01</td> - <td>Use BFS! Not DFS!</td> - <td>CSC148 > Winter 2014</td> - </tr> - <tr> - <td>2016-04-05</td> - <td>Use a heap!</td> - <td>CSC263 > Fall 2015</td> - </tr> - <tr> - <td>2016-02-30</td> - <td>I love python!</td> - <td>CSC108 > Winter 2015</td> - </tr> - <tr> - <td>2016-01-15</td> - <td>I hate verilog!</td> - <td>CSC258 > Fall 2015</td> - </tr> - </tbody> + {{# each comments }} + <tr> + <td>{{ this.date }}</td> + <td>{{ this.comment }}</td> + <td>{{ this.exam_info.course_code }} > {{ this.exam_info.term }} + {{ this.exam_info.year }}</td> + </tr> + {{/each}} </table> + + </div> + {{else}} + <h3>No Comments.</h3> + {{/if}} + </div> <!-- Date, Edit, Logout (new row) --> |
