Unverified Commit 79ba1fbc authored by Mark Stenglein's avatar Mark Stenglein
Browse files

Separate out the API

- Move all api elements to it's own route file
- Change url to include /api/v1/... for future versioning
- Prep add in new route files to app.js
    - This should be done automatically with a recursive script in the future if possible
parent 1972326f
......@@ -9,8 +9,9 @@ var mongooseRedisCache = require("mongoose-redis-cache");
// Load site wide configurations
var config = require('./config');
// Load in Routes
var routes = require('./routes/index');
//var api = require('./routes/api');
var apiV1 = require('./routes/api/v1');
var app = express();
......@@ -39,7 +40,7 @@ var populateDB = require('./setup/populateDB');
populateDB();
app.use('/', routes);
//app.use('/api/', api);
app.use('/api/v1', apiV1);
// catch 404 and forward to error handler
app.use(function(req, res, next) {
......
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');
// GET ical file
var jsonSlugCache = { slugs: [] };
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);}
})
}
});
module.exports = router;
......@@ -10,31 +10,6 @@ router.get('/docs', function(req, res, next) {
res.render('docs', { config });
})
// GET ical file
var jsonSlugCache = { slugs: [] };
router.get('/api/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);}
})
}
});
// TODO: Actually learn how to make this function set be passed around properly
/* GET home page. */
......
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