diff options
| author | HumairAK <humair88@hotmail.com> | 2016-07-21 21:53:14 +0000 |
|---|---|---|
| committer | HumairAK <humair88@hotmail.com> | 2016-07-21 21:53:14 +0000 |
| commit | 797b4fe83ed4ebbed47930c7d0d0a34cd37703c6 (patch) | |
| tree | 59cd29eb763665d51902b2b402e4ab773e058a87 /routes/index.js | |
| parent | 377664b62639a59e582252122e2a29a49d9ecaae (diff) | |
Made appropriate changes to add admin panel for adding exams, still need to add validation and fix minor glitches. Added servercallback for get_all_exams and find_exams in node_simple.js
Diffstat (limited to 'routes/index.js')
| -rw-r--r-- | routes/index.js | 104 |
1 files changed, 71 insertions, 33 deletions
diff --git a/routes/index.js b/routes/index.js index bea28da..1e3f1e0 100644 --- a/routes/index.js +++ b/routes/index.js @@ -35,26 +35,61 @@ router.get('/questions', function(req, res, next) { }); router.get('/admin', function(req,res){ - res.render('admin'); + res.render('admin', {csrfToken: req.csrfToken()}); }); -/* Render/GET exam page */ -/* Render/GET exam page */ + +router.post('/admin/update', function(req,res){ + var course_code = req.body.course_code, + year = req.body.year, + type = req.body.type, + term = req.body.term, + instructors = parseStringArray(req.body.instructors), + page_count = req.body.page_count, + questions_count = req.body.questions_count, + questions_list = parseStringArray(req.body.questions_list), + upload_date = req.body.upload_date, + uploaded_by = req.body.uploaded_by; + + var fields = [ + course_code, // String + year, // Int + type, // String; Needs to be added to database code + term, // String + instructors, // Array of strings + page_count, // Int + questions_count, // Int + upload_date, // String + uploaded_by]; // String + + dbFile.add_exam(fields, questions_list, function(examAdded, statusMessage){ + if(examAdded){ + console.log("Success!"); + }else{ + console.log("Failed!"); + } + console.log(statusMessage); + }); + res.redirect('/admin'); +}); + + //EXAMPLE EXPECTED DATA GIVEN BELOW: -/*[ { courseCode: 'CSC240', - year: 2016, - term: 'Fall', - instructors: [ 'Faith Ellen', 'Tom F.' ], - type: 'Midterm Examination', - title: 'Thry of Computation' }, - { courseCode: 'CSC240', - year: 2014, - term: 'Fall', - instructors: [ 'Faith Ellen', 'Tom F.' ], - type: 'Midterm Examination', - title: 'Thry of Computation' } ] - */ +/*[ {courseCode: 'CSC240', + year: 2016, + term: 'Fall', + instructors: ['Faith Ellen', 'Tom F.'], + type: 'Midterm Examination', + title: 'Thry of Computation' }, + + {courseCode: 'CSC240', + year: 2014, + term: 'Fall', + instructors: ['Faith Ellen', 'Tom F.'], + type: 'Midterm Examination', + title: 'Thry of Computation' } ] + */ router.get('/exams/:id', function(req, res, next) { var minExamInfoArray = []; dbFile.get_all_exams(req.params.id, function (exams) { @@ -65,6 +100,7 @@ router.get('/exams/:id', function(req, res, next) { //console.log(exams); //only pass over the information that is necessary for the exams page for (var i = 0; i<exams.length;i++){ + var getInstructors = exams[i].instructors.join(", "); var minExamInfo = { courseCode:exams[i].course_code, year:exams[i].year, @@ -130,20 +166,7 @@ router.post('/signup', function(req, res, next) { failureFlash: true }));*/ - -/* -app.get('/exams',function (req,res) { - console.log(req.query.search); - var courseName = req.query.search; - res.redirect("http://localhost:3000/exams.html/?course_name="+courseName); -}); - - -app.get('/exams.html', function (req,res) { - /!*LOADS ALL THE STATIC FILES REALTIVE TO THE REDIRECTED URL*!/ - app.use('/exams.html', express.static(__dirname)); - res.sendFile(__dirname+'/exams.html'); -});*/ +/**** Helpers ****/ function getExamsForCourseCode(courseCode) { dbFile.get_all_exams(courseCode, function (exams) { @@ -156,8 +179,23 @@ function getExamsForCourseCode(courseCode) { }); } -module.exports = router; - function toProperCase(string) { return string.replace(/\w\S*/g, function(txt){return txt.charAt(0).toUpperCase() + txt.substr(1).toLowerCase();}); -}
\ No newline at end of file +} + + +/* Takes an input string delimited by commas, will split by comma and trim white + * spaces. Consider callback. + */ +function parseStringArray(input){ + var list = input.split(','); + var parsedList = []; + list.forEach(function(word){ + if (word != ""){ + parsedList.push(word.trim()); + } + }); + return parsedList; +} + +module.exports = router;
\ No newline at end of file |
