aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--node_simple.js26
-rw-r--r--routes/index.js104
-rw-r--r--views/admin.hbs2
-rw-r--r--views/index.hbs20
-rw-r--r--views/partials/header.hbs1
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<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
diff --git a/views/admin.hbs b/views/admin.hbs
index 89e78c7..ecbd46e 100644
--- a/views/admin.hbs
+++ b/views/admin.hbs
@@ -1,7 +1,7 @@
<main id="solutions-main">
<div class="col-md-4 col-md-offset-4 col-sm-6 col-sm-offset-3">
<h4>Add Exam</h4>
-<form action="/add_exam" method="post">
+<form action="/admin/update" method="post">
<div class="form-group">
<input type="text"
class="form-control"
diff --git a/views/index.hbs b/views/index.hbs
index 9826209..59c589f 100644
--- a/views/index.hbs
+++ b/views/index.hbs
@@ -10,7 +10,14 @@
<div class="input-group">
<!-- Search Category Drop-Down -->
<div class="input-group-btn">
- <button type="button" class="btn btn-default dropdown-toggle search-button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">Courses <span class="caret"></span></button>
+ <button type="button"
+ class="btn btn-default dropdown-toggle search-button"
+ data-toggle="dropdown"
+ aria-haspopup="true"
+ aria-expanded="false">
+ Courses
+ <span class="caret"></span>
+ </button>
<ul class="dropdown-menu">
<li><a href="#">Courses</a></li>
<li><a href="#">Users</a> </li>
@@ -18,10 +25,17 @@
</div>
<!-- Search bar -->
- <input type="text" id = "user-input" class="form-control" aria-label="..." name="search" placeholder="Enter course name">
+ <input type="text"
+ id = "user-input"
+ class="form-control"
+ aria-label="..."
+ name="search"
+ placeholder="Enter course name">
<!-- Prompt Search -->
<span class="input-group-btn">
- <button id = 'go-button' class="btn btn-default search-button go-button" type="submit">Go!</button>
+ <button id = 'go-button'
+ class="btn btn-default search-button go-button"
+ type="submit">Go!</button>
</span>
</div>
</div>
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 @@
</ul>
</li>
<li><a href="#" data-toggle="modal" data-target=".login-window">Log In</a></li>
+ <li><a href="/admin">Admin Panel</a></li>
<!--<li><a href="#" data-toggle="modal" data-target=".registration-window">Sign up</a></li>-->
<li><a href="/signup" >Sign up</a></li>
<li class="dropdown">