aboutsummaryrefslogtreecommitdiff
path: root/app.js
blob: e4d0843f20190ca49967f382d76d6d41eeabfdc5 (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
var fs = require("fs");
var express = require('express');
var path = require('path');
var cookieParser = require('cookie-parser');
var bodyParser = require('body-parser');
var hbs = require('express-handlebars');
var dbFile = require("./node_simple.js");
var sanitizer  = require ("sanitizer");

var routes = require('./routes/index');
var app = express();

// Templating engine, we are using handlebars
// This will allow us to create html pages dynamically before serving them.
app.engine('.hbs', hbs({extname: '.hbs', defaultLayout: 'layout',
    layoutsDir: __dirname + '/views/layouts/', // Set directory for base layout
    partialsDir: __dirname + '/views/partials'})); // Set directory for partials

app.set('views', path.join(__dirname, 'views')); // Our view path
app.set('view engine', '.hbs');

// Middleware initialization, make sure everything is initialized in proper order
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({extended: true}));
app.use(cookieParser());

/*LOADS ALL STATIC FILES FROM THE DIRECTORY __dirname*/
app.use(express.static(__dirname));

// Allows us to customize express routing
// in a separate file.
app.use('/', routes);

module.exports = app;

app.listen(3000, function() {
    console.log('listening on http://localhost:3000/');
});

//to fetch exams given a course code and populate the exams page
app.get('/exams',function (req,res) {
    console.log(req.query.search);
    var courseName = req.query.search;
    courseName = sanitizer.escape(courseName);
    courseName = courseName.toUpperCase();
    console.log(courseName);
    var result = getExamsForCourseCode(courseName);
    console.log(result);
    res.write(JSON.stringify(result));
});

function getExamsForCourseCode(courseCode) {
    var minExamInfoArray = [];
    dbFile.get_all_exams(courseCode, 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"
                };
                minExamInfoArray.push(minExamInfo);
                console.log(minExamInfo);
            }
        }
    });

    return minExamInfoArray;
}


/*Helper function to make sure the proper capital case is presented*/

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