Unverified Commit f7518241 authored by Mark Stenglein's avatar Mark Stenglein
Browse files

Added first API call

- localhost:3000/api/json/GMU2016F (or any other slug)
parent 6906b59e
......@@ -36,7 +36,7 @@ var populateDB = require('./setup/populateDB');
populateDB();
app.use('/', routes);
app.use('/api', api);
//app.use('/api/', api);
// catch 404 and forward to error handler
app.use(function(req, res, next) {
......
......@@ -7,6 +7,7 @@ config.siteDescription = "A simple application to add your class schedule to cal
// MongoDB config
config.mongoDBURL = 'mongodb://localhost/myappdatabase';
config.ReloadDB = true;
config.schoolSlugs = {
// Slug: Long Name
......@@ -16,16 +17,9 @@ config.schoolSlugs = {
// Data files to load:
config.dataFiles = [
{
// URL Slug for the API
slug: 'GMU',
// List of .json files to import (place in setup folder under SLUG folder)
dataSource: [
'fall2016GMU' // IE: get's put in 'setup/GMU/fall2016GMU.json'
],
// Whether to actually import the data
enabled: false
}
'GMU2016F.min.json',
'GMU2016S.min.json',
'GMU2016SP.min.json'
]
module.exports = config;
var express = require('express');
var router = express.Router();
var mongoose = require('mongoose');
var Semester = require('../models/Semester');
var api = express.Router();
// Load site wide configurations
var config = require('../config');
/* GET Schools listing. */
router.get('/api/lists/schools', function(req, res, next) {
api.get('/api/lists/schools', function(req, res, next) {
res.send(JSON.stringify(schools));
});
// GET ical file
router.get('/api/:format/:school/:semester/:crns', function(req, res) {
var crns = req.params['crns'].split(',');
var format = req.params['format'];
var school = req.params['school'];
var semester = req.params['semester'];
var crnNums = [{
format,
year,
season,
crns
}];
api.get('/api/json/:SEMSLUG', function(req, res) {
var slug = req.params['SEMSLUG'];
// find each person with matching slug
var query = Semester.findOne({ 'slug': slug });
query.exec(function (err, semester) {
if (err || (! semester)) { res.json({ 'results' : 'error, try something different' }) }
else {res.json(semester);}
})
res.send(crnNums);
});
module.exports = router;
module.exports = api;
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');
......@@ -8,6 +11,18 @@ router.get('/docs', function(req, res, next) {
res.render('docs', { config });
})
// GET ical file
router.get('/api/json/:SEMSLUG', function(req, res) {
var slug = req.params['SEMSLUG'];
// find each person with matching slug
var query = Semester.findOne({ 'slug': slug });
query.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. */
......
......@@ -3,36 +3,32 @@ var mongoose = require('mongoose');
var Semester = require('../models/Semester');
var fs = require("fs");
//////
// DATA FILES HERE - TODO: Make this programmatic instead of hard coded
//////
var datafiles = [
'GMU2016F.min.json',
'GMU2016S.min.json',
'GMU2016SP.min.json'
]
// Load site wide configurations
var config = require('../config');
var populateDB = function() {
emptySemesters(); // NOTE: this is an asynchronous call
var semesters = [];
for (var i = 0; i < datafiles.length; i++) {
var datafile = datafiles[i];
var semester = new Semester(JSON.parse(fs.readFileSync("./setup/dataFiles/" + datafile)));
semesters.push(semester.toObject());
}
Semester.collection.insert(semesters, function(err) {
if (err) { console.error('Database Error!', err) }
else {
var findItems = Semester.find();
findItems.select('-_id');
findItems.select('-classes');
findItems.exec(function(err, users) {
if (err) {console.error(err); }
else { console.log(users); }
});
if (config.ReloadDB) {
emptySemesters(); // NOTE: this is an asynchronous call
var semesters = [];
for (var i = 0; i < config.dataFiles.length; i++) {
var datafile = config.dataFiles[i];
var semester = new Semester(JSON.parse(fs.readFileSync("./setup/dataFiles/" + datafile)));
semesters.push(semester.toObject());
}
});
Semester.collection.insert(semesters, function(err) {
if (err) { console.error('Database Error!', err) }
else {
var findItems = Semester.find();
findItems.select('-_id');
findItems.select('-classes');
findItems.exec(function(err, users) {
if (err) {console.error(err); }
else { console.log(users); }
});
}
});
}
}
// Empty collections
......
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