diff options
| author | Waref Haque <warefhaque@inside-239-60.wireless.utoronto.ca> | 2016-07-18 17:18:41 +0000 |
|---|---|---|
| committer | Waref Haque <warefhaque@inside-239-60.wireless.utoronto.ca> | 2016-07-18 17:18:41 +0000 |
| commit | 39522c0ae3727607164e39a0334201c77b53fdb6 (patch) | |
| tree | 0c57f57e0d3fe291a6a042d89ac602c5b672605f /app.js | |
| parent | a35da9f9ccc1124d9b6f4461c7216ffbb0285e2f (diff) | |
fetch exams with course code p 2
Diffstat (limited to 'app.js')
| -rw-r--r-- | app.js | 52 |
1 files changed, 50 insertions, 2 deletions
@@ -4,6 +4,8 @@ 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(); @@ -14,7 +16,6 @@ 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'); @@ -30,5 +31,52 @@ app.use(express.static(__dirname)); // 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*/ -module.exports = app;
\ No newline at end of file +function toProperCase(string) { + return string.replace(/\w\S*/g, function(txt){return txt.charAt(0).toUpperCase() + txt.substr(1).toLowerCase();}); +} |
