Unverified Commit 0349e198 authored by Mark Stenglein's avatar Mark Stenglein
Browse files

various edits

- Fleshed out some of the API
- added some comments for documentation
- performed some linting
parent fc1fba56
......@@ -34,7 +34,7 @@ app.use(express.static(path.join(__dirname, 'public')));
// Connect to mongo
mongoose.connect(config.mongoDBURL);
mongooseRedisCache(mongoose)
mongooseRedisCache(mongoose);
// Populate initial data
var populateDB = require('./setup/populateDB');
......@@ -75,5 +75,4 @@ app.use(function(err, req, res, next) {
});
});
module.exports = app;
......@@ -29,7 +29,7 @@ var semesterSchema = new Schema({
]
});
semesterSchema.set('redisCache', true)
semesterSchema.set('redisCache', true);
// the schema is useless so far
// we need to create a model using it
......
......@@ -6,29 +6,48 @@ var Semester = require('../../models/Semester');
// Load site wide configurations
var config = require('../../config');
// GET ical file
// Setup Slug Cache for redis cache
var jsonSlugCache = { slugs: [] };
////////////////////////////////////////////////////////////////////////////////
// JSON API Section
// Get school and school slug listing
router.get('/json/schools', function(req, res, next) {
res.json(config.schoolSlugs);
});
// TODO: FINISH THIS
// Get semester and semester slug listing for a given school
router.get('/json/semesters/:SCHOOL', function(req, res, next) {
var schoolSlug = req.params['SCHOOL'];
res.json({'TODO': schoolSlug});
});
// GET classes for a semester
router.get('/json/classes/:SEMSLUG', function(req, res, next) {
var slug = req.params['SEMSLUG'];
if (jsonSlugCache[slug])
{
jsonSlugCache[slug].timesUsed++;
console.log(jsonSlugCache[slug].timesUsed)
res.send(jsonSlugCache[slug]['value']);
}
else {
// find each person with matching slug
var query = Semester.findOne({ 'slug': slug });
query.select('-_id -classes._id -classes.session_templates._id');
query.lean().exec(function (err, semester) {
if (err || (! semester)) {
res.json({
'results' : 'error, try something different'
})
}
else {res.json(semester);}
})
}
// find each person with matching slug
var query = Semester.findOne({ 'slug': slug });
// remove unwanted fields from request
query.select('-_id -classes._id -classes.session_templates._id');
// excecute the request (caches with redis)
query.lean().exec(function (err, semester) {
// if there was an error or nothing was returned
if (err || (! semester)) {
// send an error message
res.json({
'results' : 'error, try something different'
})
}
// else send out the semester object as json
else {
res.json(semester);
}
})
});
////////////////////////////////////////////////////////////////////////////////
// ICAL API Section
module.exports = router;
var express = require('express');
var router = express.Router();
var mongoose = require('mongoose');
var Semester = require('../models/Semester');
// Load site wide configurations
var config = require('../config');
......
var express = require('express');
var router = express.Router();
var mongoose = require('mongoose');
var Semester = require('../models/Semester');
// Load site wide configurations
var config = require('../config');
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment