aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--routes/index.js83
-rw-r--r--views/index.hbs28
2 files changed, 81 insertions, 30 deletions
diff --git a/routes/index.js b/routes/index.js
index 347c91a..f71bc74 100644
--- a/routes/index.js
+++ b/routes/index.js
@@ -2,10 +2,10 @@ var dbFile = require("../node_simple.js");
var express = require('express');
var router = express.Router();
var csrf = require('csurf'); // Cross-Site Request Forgery prevention
-
+var express_validator = require("express-validator");
var csrfProtection = csrf();
router.use(csrfProtection); // router is protected
-
+router.use(express_validator);
// Remove later
var passport_file = require('../config/passport.js');
var bcrypt = require('bcrypt-nodejs');
@@ -13,7 +13,7 @@ var bcrypt = require('bcrypt-nodejs');
/* Render/GET homepage. */
router.get('/', function(req, res, next) {
//addFirstAdmin();
- res.render('index');
+ res.render('index', {csrfToken: req.csrfToken(), success: req.session.success, errors: req.session.errors});
req.session.errors = null;
req.session.success = null;
@@ -59,33 +59,59 @@ router.get('/user_solutions', function(req, res, next) {
title: 'Thry of Computation' } ]
*/
+
+router.get('/exams/', function(req,res,next){
+ req.check('search','Course code should be between 6 and 20 characters').notEmpty().withMessage('Course code required').isLength({min: 6, max: 20});
+
+ var errors = req.validationErrors();
+ if (errors){
+ console.log(errors);
+ req.session.errors = errors;
+ req.session.success = false;
+ res.redirect('/');
+ }
+});
+
router.get('/exams/:id', function(req, res, next) {
- var minExamInfoArray = [];
- dbFile.get_all_exams(req.params.id, function (exams) {
- if (exams.length == 0){
- console.log("Nothing was found");
- }
- else {
- //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,
- term:toProperCase(exams[i].term),
- instructors: getInstructors,
- type:toProperCase(exams[i].type) + " Examination",
- title:exams[i].title,
- id:exams[i]._id,
- questionCount : exams[i].questions_count
- };
- //console.log(minExamInfo);
- minExamInfoArray.push(minExamInfo);
+
+ req.check('search','Course code should be between 6 and 20 characters').notEmpty().withMessage('Course code required').isLength({min: 6, max: 20});
+
+ var errors = req.validationErrors();
+ if (errors){
+ console.log("id " + errors);
+ req.session.errors = errors;
+ req.session.success = false;
+ res.redirect('/');
+ }else{
+
+ var minExamInfoArray = [];
+ dbFile.get_all_exams(req.params.id, function (exams) {
+ if (exams.length == 0){
+ console.log("Nothing was found");
}
- }
- res.render('exams', {query: req.params.id, result: minExamInfoArray});
- });
+ else {
+ //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,
+ term:toProperCase(exams[i].term),
+ instructors: getInstructors,
+ type:toProperCase(exams[i].type) + " Examination",
+ title:exams[i].title,
+ id:exams[i]._id,
+ questionCount : exams[i].questions_count
+ };
+ //console.log(minExamInfo);
+ minExamInfoArray.push(minExamInfo);
+ }
+ }
+ res.render('exams', {query: req.params.id, result: minExamInfoArray});
+ });
+
+ }
});
/* Render user search page based on query */
@@ -93,7 +119,6 @@ router.get('/user/:query', function(req,res,next){
var query = req.params.query;
// Need to validate query
-
dbFile.search_users(query, function(success, result){
if(success){
console.log(result);
diff --git a/views/index.hbs b/views/index.hbs
index c3cbeab..6dffef9 100644
--- a/views/index.hbs
+++ b/views/index.hbs
@@ -1,4 +1,6 @@
<header>
+
+
<div class="header-content">
<form action="/search/courses" method="get" id="search-field">
@@ -7,7 +9,31 @@
<hr> <!-- Divider -->
<div class="search col-md-8 col-lg-8 col-md-offset-2">
<!-- Search-bar, Drop-down and Prompt -->
- <div class="input-group">
+
+ {{# if success }}
+ <section class="alert alert-success">
+ <!-- Change those to banners later -->
+ <h2>Signup Successful!</h2>
+ </section>
+ {{ else }}
+ {{# if errors }}
+ <section class="alert alert-danger">
+ <ul>
+ {{# each errors }}
+ <li>{{ this.msg }}</li>
+ {{/each}}
+ </ul>
+ </section>
+ {{/if}}
+ {{/if}}
+ {{#if flashMsg}}
+ <section class="alert alert-danger">
+ {{flashMsg}}
+ </section>
+ {{/if}}
+
+
+ <div class="input-group">
<!-- Search Category Drop-Down -->
<div class="input-group-btn">
<button type="button"