diff options
| author | HumairAK <humair88@hotmail.com> | 2016-07-24 00:46:35 +0000 |
|---|---|---|
| committer | HumairAK <humair88@hotmail.com> | 2016-07-24 00:46:35 +0000 |
| commit | 18b6e474fb64f7e9b4699d3bfbf3f5276e6996ad (patch) | |
| tree | df3a1f71250366ced02579f4f381ec74598861f3 /routes | |
| parent | 17d743822746b047b94cd24e5407f1342ba01a7e (diff) | |
Finished questions, added servercallback for find_exam_byID
Diffstat (limited to 'routes')
| -rw-r--r-- | routes/admin.js | 7 | ||||
| -rw-r--r-- | routes/index.js | 75 |
2 files changed, 64 insertions, 18 deletions
diff --git a/routes/admin.js b/routes/admin.js index e038fa3..7e5c7fe 100644 --- a/routes/admin.js +++ b/routes/admin.js @@ -8,8 +8,6 @@ var passport_file = require('../config/passport.js'); var csrfProtection = csrf(); router.use(csrfProtection); // router is protected - - router.get('/', isAdmin, function(req,res){ res.render('admin', {csrfToken: req.csrfToken()}); }); @@ -65,7 +63,7 @@ router.post('/add/course', function(req,res){ res.redirect('/admin'); }); -// Add a new admin, redirect to admin panel +/* Add a new admin, redirect to admin panel */ router.post('/add/admin', function(req,res){ var admin_data = { fname: req.body.fname, @@ -128,9 +126,8 @@ 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/index.js b/routes/index.js index 4a0e54c..d3c55e6 100644 --- a/routes/index.js +++ b/routes/index.js @@ -84,24 +84,73 @@ router.get('/search', function(req, res, next) { res.redirect('/exams/' + courseName); }); -/*EXAMPLE DATA GIVEN BELOW: + +/* This is a redirect from the exams page, route is generated in exams.hbs + * + * EXAMPLE DATA GIVEN BELOW: * questions = [{id,count,comments},{id,count,comments}] --> array of "question" objects * * id = questions number * count= number of solutions * comments = number of comments*/ router.get('/questions/:exam_id', function (req,res) { - console.log(req.params.exam_id); - dbFile.get_exam_info_by_ID(req.params.exam_id, function (questions) { + var examID = req.params.exam_id; + console.log(examID); + dbFile.get_exam_byID(examID, function(exam){ + + /* [ + { q_id: 1, question: 'this is q1' }, + { q_id: 2, question: 'this is q2' } + ] + */ + var qList = exam.questions_list; + + // Add comments/solutions + dbFile.get_exam_info_by_ID(examID, function (questionsInfo) { + qList.forEach(function(question){ + question.count = 0; + question.comments = 0; + + // Find q_id in questionsInfo, update comment/solutions count + questionsInfo.forEach(function(q){ + if (q.id == question.id){ + question.count += q.count; + question.comments += q.comments; + } + }); + }); + + var examInfo = { + id : exam._id, + courseCode : exam.course_code, + term : toProperCase(exam.term), + type : toProperCase(exam.type), + year : exam.year, + instructors : exam.instructors.join(), + uploadDate : exam.upload_date, + uploader : exam.uploaded_by, + pageCount : exam.page_count, + questionCount : exam.questions_count + }; + res.render('questions', {query: qList, examInfo: examInfo}); + }); + + + console.log(exam); + }); + + /*dbFile.get_exam_info_by_ID(examID, function (questions) { console.log(questions); res.render('questions', {query: questions}); - }); + });*/ + }); -/*GET the solutions for a given exam given the question number and the exam_id*/ -/*EXPECTED DATA GIVEN BELOW: -array of solutions: +/*GET the solutions for a given exam given the question number and the exam_id + +* EXPECTED DATA GIVEN BELOW: +* array of solutions: * solutions = [ { * _id: "354ff71ed078933079d6467e" * exam_id: "578a44ff71ed097fc3079d6e" @@ -112,17 +161,17 @@ array of solutions: * } * { * _id: ... - * exam_id: .. - * q_id: ... - * text: .. - * votes: .. - * comments: .. + * exam_id: .. + * q_id: ... + * text: .. + * votes: .. + comments: .. * * } * ]*/ 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('solutions', {query: solutions}); + res.render('user_solutions', {query: solutions}); console.log(solutions); }); }); |
