aboutsummaryrefslogtreecommitdiff
path: root/routes/index.js
blob: 84087e729cdd7c8afe2141e219851ce3c79e0775 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
var dbFile  = require("../node_simple.js");
var express = require('express');
var router = express.Router();
var csrf = require('csurf'); // Cross-Site Request Forgery prevention
var passport = require('passport');

var csrfProtection = csrf();
router.use(csrfProtection); // router is protected

/* Render/GET homepage. */
router.get('/', function(req, res, next) {
    res.render('index');
});

/* Render/GET about page */
router.get('/about', function(req, res, next) {
    res.render('about');
});

/* Render/GET user_solutions page */
router.get('/user_solutions', function(req, res, next) {
    res.render('user_solutions');
});

/* Render/GET user_profile page */
router.get('/user_profile', function(req, res, next) {
    res.render('user_profile_alt');
});

/* Render/GET questions page */
router.get('/questions', function(req, res, next) {
    res.render('questions');
});

/* Render/GET exam page */
/* Render/GET exam page */
//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' } ]
 */

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 minExamInfo = {     courseCode:exams[i].course_code,
                    year:exams[i].year,
                    term:toProperCase(exams[i].term),
                    instructors:exams[i].instructors,
                    type:toProperCase(exams[i].type) + " Examination",
                    title:exams[i].title,
                    id:exams[i]._id
                };
                //console.log(minExamInfo);
                minExamInfoArray.push(minExamInfo);
            }
        }
        console.log(minExamInfoArray);
        res.render('exams', {query: minExamInfoArray});
    });
});

/* Render/GET search (exam) page */
router.get('/search', function(req, res, next) {
    console.log(req.query.search);
    var courseName = req.query.search;
    res.redirect('/exams/' + courseName);
});

router.get('/signup', function(req, res, next) {
    res.render('signup', {csrfToken: req.csrfToken()});
});

router.post('/signup', function(req, res, next) {
    res.redirect('/');
});

/*router.post('/signup', passport.authenticate('local.signup', {
    sucessRedirect: '/profile',
    failureRedirect: '/signup',
    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');
});*/


function getExamsForCourseCode(courseCode) {
    dbFile.get_all_exams(courseCode, function (exams) {
        if (exams.length == 0){
            console.log("Nothing was found");
        }
        else {
            console.log(exams);
        }
    });
}


module.exports = router;

function toProperCase(string) {
    return string.replace(/\w\S*/g, function(txt){return txt.charAt(0).toUpperCase() + txt.substr(1).toLowerCase();});
}