From 797b4fe83ed4ebbed47930c7d0d0a34cd37703c6 Mon Sep 17 00:00:00 2001 From: HumairAK Date: Thu, 21 Jul 2016 17:53:14 -0400 Subject: 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 --- node_simple.js | 26 ++++++++---- routes/index.js | 104 +++++++++++++++++++++++++++++++--------------- views/admin.hbs | 2 +- views/index.hbs | 20 +++++++-- views/partials/header.hbs | 1 + 5 files changed, 107 insertions(+), 46 deletions(-) diff --git a/node_simple.js b/node_simple.js index 767ec29..7e9c736 100644 --- a/node_simple.js +++ b/node_simple.js @@ -71,6 +71,8 @@ var ObjectId = require('mongodb').ObjectID; // Standard URI format: mongodb://[dbuser:dbpassword@]host:port/dbname var uri = 'mongodb://general:assignment4@ds057862.mlab.com:57862/solutions_repo'; +// Keep this for testing on local machine, do not remove. - Humair +//var uri = 'mongodb://localhost:27017/db'; //***********************PRELIMINARY TESTING******************************************| @@ -412,8 +414,6 @@ exports.get_all_exams = function (course_code, callback) { }; - - /* * This function will add an exam to the database UNLESS the exams already exists. * If the exams table is empty, this will create one and then add the data. @@ -423,8 +423,10 @@ exports.get_all_exams = function (course_code, callback) { * ["instructor1",...,"instructor n"], page_count, question_count * "upload_date", "user_name"] * questions_array - a array by format ["q_1", "q_2", ... , "q_question_count"] - * */ -exports.add_exam = function (fields, questions_array) { + * + * callback parameter takes a boolean to indicate whether insert was successful + * and an output status message.*/ +exports.add_exam = function (fields, questions_array, serverCallback) { // construct an exam object var Data = @@ -453,9 +455,10 @@ exports.add_exam = function (fields, questions_array) { // first see if the exam already exists // pass in course_code, year, term and type... - exports.find_exam([fields[0], fields[1], fields[2], fields[3]], function(result) { + exports.find_exam([fields[0], fields[1], fields[2], fields[3]], serverCallback, function(result, serverCallback) { if (result == true) { // meaning that the exam was found + serverCallback(false, "This exam already exists in the database."); console.log("This exam already exists in the database"); } @@ -468,9 +471,13 @@ exports.add_exam = function (fields, questions_array) { var exam_collection = db.collection('exams'); // insert data into table exam_collection.insert(Data, function(err) { - if (err) throw err; + if (err) { + serverCallback(false, "Error: Could not add exam into database."); + throw err; + } else { console.log("exam added"); + serverCallback(true, "Exam Successfully added."); db.close(function (err) { // close the connection when done if (err) throw err; }); @@ -478,6 +485,7 @@ exports.add_exam = function (fields, questions_array) { }); }) .catch(function(err) { + serverCallback(false, "Error: Could not establish connection with database."); console.error(err); }); } @@ -491,7 +499,7 @@ exports.add_exam = function (fields, questions_array) { * Params: fields - an array of format ["course_code", year, "term", "type"] * * */ -exports.find_exam = function (fields, callback) { +exports.find_exam = function (fields, serverCallback, callback) { var course_code = fields[0]; var year = fields[1]; @@ -521,10 +529,10 @@ exports.find_exam = function (fields, callback) { if (err) throw err; if (docs.length == 0) { // if this exam doesnt exist.... add it - callback(false); + callback(false, serverCallback); } else { // exam was found - callback(true); + callback(true, serverCallback); } }); 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

Add Exam

-
+
- +
- + - +
diff --git a/views/partials/header.hbs b/views/partials/header.hbs index 98ccffb..8298ce2 100644 --- a/views/partials/header.hbs +++ b/views/partials/header.hbs @@ -37,6 +37,7 @@
  • Log In
  • +
  • Admin Panel
  • Sign up