From e9b31c0e1d442421b9eb811d31259731211c9386 Mon Sep 17 00:00:00 2001 From: Bram van der Veen <96aa48@gmail.com> Date: Sat, 8 Aug 2015 22:23:55 +0200 Subject: [PATCH] Fixed the issue with 'RIP' eastereggs and added a lot of comments. --- lookup.js | 25 ++++++++++++++++--------- 1 file changed, 16 insertions(+), 9 deletions(-) diff --git a/lookup.js b/lookup.js index 7553de8..e24bd1a 100644 --- a/lookup.js +++ b/lookup.js @@ -1,34 +1,40 @@ //lookup.js -var config = require('./configuration'); var fs = require('fs'); +var database = require('mongoskin').db('mongodb://' + config().database); //Initialize the database connection. -var database = require('mongoskin').db('mongodb://' + config().database); +//Getting local variables stored in the configuration file. +var config = require('./configuration'); var school_id = config().school_id; //Function for looking through the database and finding entries related to the searchterm. function get(req, res, next, search) { - var index = database.collection('index'); - easter(search) ? req.easter = easter(search) : null; + var index = database.collection('index'); //Initialize the database collection. + easter(search) ? req.easter = easter(search) : null; //Bind the easter object to the request object. easter(search) ? search = easter(search).name : null; //Check if there are any eastereggs matching the search query. search = new RegExp(search, 'i'); //Make regular exeption for ignoring the case (Bram vs BRAM) should return the same. index.find({$or : [{id : search}, {name : search}, {first_name : search}, {last_name : search}, {username: search}]}).toArray(function (err, database_entry) { if (err) console.warn(err); - if (database_entry.length == 1) { + if (req.easter.type == 'RIP') { //There is an easteregg type that is a grave stone for old students and teachers, here's the logical exception for it. + require('./auth').is(req, res, function () { //Ask the authentication system if the user is authenticated (will happen several times in the module). + res.render('schedule', req); + }); + } + else if (database_entry.length == 1) { //If there was a match in the system with the supplied database_entry[0].url = make_url(req, database_entry[0]); req.match = database_entry[0]; next(); } - else if (database_entry.length == 0) { + else if (database_entry.length == 0) { //If there were no matches found. require('./auth').is(req, res, function () { - res.render('not_found', req); + res.render('not_found', req); //Render the not_found page. }); } - else { + else { //If there are multiple matches found in the system req.match = database_entry; require('./auth').is(req, res, function () { - res.render('list', req); + res.render('list', req); //Render the list view }); } }); @@ -93,6 +99,7 @@ function make_url(req, database_entry) { //Function for checking the given search query for eatereggs. //TODO: add a way to supply a template file for eastereggs. function easter(search) { + console.log('search', search); var list = JSON.parse(fs.readFileSync(__dirname + '/eastereggs.json')); for (entry of list) {