Commit 7f822200 authored by Mark Stenglein's avatar Mark Stenglein
Browse files

Changes to allow the models to be imported anywhere with ease.

- I should really be doing something else right now....like homework.
parent fe09ad9f
...@@ -7,13 +7,13 @@ ...@@ -7,13 +7,13 @@
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
// Load in the different packages // Load in the different packages
require('app-module-path').addPath(__dirname);
var express = require('express'); var express = require('express');
var path = require('path'); var path = require('path');
var logger = require('morgan'); var logger = require('morgan');
var cookieParser = require('cookie-parser'); var cookieParser = require('cookie-parser');
var bodyParser = require('body-parser'); var bodyParser = require('body-parser');
var Sequelize = require('sequelize'); var Sequelize = require('sequelize');
//var config = require('config');
// Load in Routes // Load in Routes
// TODO: Make this a dynamic loading system that simply scans the directory // TODO: Make this a dynamic loading system that simply scans the directory
...@@ -42,7 +42,7 @@ app.use(require('node-sass-middleware')({ ...@@ -42,7 +42,7 @@ app.use(require('node-sass-middleware')({
app.use(express.static(path.join(__dirname, 'public'))); app.use(express.static(path.join(__dirname, 'public')));
// Initialize the Database and Connection // Initialize the Database and Connection
app.set('db', path.join(__dirname, 'models', 'index')); var db = require(path.join(__dirname, 'models'));
// Populate initial data // Populate initial data
// TODO: populate the information // TODO: populate the information
......
module.exports = function(sequelize, DataTypes) { module.exports = function(sequelize, DataTypes) {
var Section = sequelize.define("Section", { return sequelize.define("Section", {
// unique index/key // unique index/key
crn : { crn : {
type: DataTypes.STRING, type: DataTypes.STRING,
...@@ -10,7 +10,7 @@ module.exports = function(sequelize, DataTypes) { ...@@ -10,7 +10,7 @@ module.exports = function(sequelize, DataTypes) {
title : DataTypes.STRING, title : DataTypes.STRING,
section : DataTypes.STRING, section : DataTypes.STRING,
instructor : type: DataTypes.STRING, instructor : DataTypes.STRING,
campus : DataTypes.STRING, campus : DataTypes.STRING,
location : DataTypes.STRING, location : DataTypes.STRING,
...@@ -22,7 +22,7 @@ module.exports = function(sequelize, DataTypes) { ...@@ -22,7 +22,7 @@ module.exports = function(sequelize, DataTypes) {
endDate : DataTypes.DATE, endDate : DataTypes.DATE,
Msession : { Msession : {
type: DataTypes.BOOL, type: DataTypes.BOOLEAN,
allowNull: false, allowNull: false,
defaultValue: false defaultValue: false
}, },
...@@ -30,7 +30,7 @@ module.exports = function(sequelize, DataTypes) { ...@@ -30,7 +30,7 @@ module.exports = function(sequelize, DataTypes) {
MtimeEnd : DataTypes.TIME, MtimeEnd : DataTypes.TIME,
Tsession : { Tsession : {
type: DataTypes.BOOL, type: DataTypes.BOOLEAN,
allowNull: false, allowNull: false,
defaultValue: false defaultValue: false
}, },
...@@ -38,7 +38,7 @@ module.exports = function(sequelize, DataTypes) { ...@@ -38,7 +38,7 @@ module.exports = function(sequelize, DataTypes) {
TtimeEnd : DataTypes.TIME, TtimeEnd : DataTypes.TIME,
Wsession : { Wsession : {
type: DataTypes.BOOL, type: DataTypes.BOOLEAN,
allowNull: false, allowNull: false,
defaultValue: false defaultValue: false
}, },
...@@ -46,7 +46,7 @@ module.exports = function(sequelize, DataTypes) { ...@@ -46,7 +46,7 @@ module.exports = function(sequelize, DataTypes) {
WtimeEnd : DataTypes.TIME, WtimeEnd : DataTypes.TIME,
Rsession : { Rsession : {
type: DataTypes.BOOL, type: DataTypes.BOOLEAN,
allowNull: false, allowNull: false,
defaultValue: false defaultValue: false
}, },
...@@ -54,7 +54,7 @@ module.exports = function(sequelize, DataTypes) { ...@@ -54,7 +54,7 @@ module.exports = function(sequelize, DataTypes) {
RtimeEnd : DataTypes.TIME, RtimeEnd : DataTypes.TIME,
Fsession : { Fsession : {
type: DataTypes.BOOL, type: DataTypes.BOOLEAN,
allowNull: false, allowNull: false,
defaultValue: false defaultValue: false
}, },
...@@ -62,41 +62,41 @@ module.exports = function(sequelize, DataTypes) { ...@@ -62,41 +62,41 @@ module.exports = function(sequelize, DataTypes) {
FtimeEnd : DataTypes.TIME FtimeEnd : DataTypes.TIME
}, { }, {
validate: { validate: {
Msession: function(value) { Msession_validate: function(value) {
if (Msession === true) && if ((Msession === true) &&
(MtimeStart === null || MtimeEnd === null) { (MtimeStart === null || MtimeEnd === null)) {
throw new Error('Start and end times must be defined for' + throw new Error('Start and end times must be defined for' +
' Monday if class is set to true.'); ' Monday if class is set to true.');
} }
}, },
Tsession: function(value) { Tsession_validate: function(value) {
if (Tsession === true) && if ((Tsession === true) &&
(TtimeStart === null || TtimeEnd === null) { (TtimeStart === null || TtimeEnd === null)) {
throw new Error('Start and end times must be defined for' + throw new Error('Start and end times must be defined for' +
' Tuesday if class is set to true.'); ' Tuesday if class is set to true.');
} }
}, },
Wsession: function(value) { Wsession_validate: function(value) {
if (Wsession === true) && if ((Wsession === true) &&
(WtimeStart === null || WtimeEnd === null) { (WtimeStart === null || WtimeEnd === null)) {
throw new Error('Start and end times must be defined for' + throw new Error('Start and end times must be defined for' +
' Wednesday if class is set to true.'); ' Wednesday if class is set to true.');
} }
}, },
Rsession: function(value) { Rsession_validate: function(value) {
if (Rsession === true) && if ((Rsession === true) &&
(RtimeStart === null || RtimeEnd === null) { (RtimeStart === null || RtimeEnd === null)) {
throw new Error('Start and end times must be defined for' + throw new Error('Start and end times must be defined for' +
' Thursday if class is set to true.'); ' Thursday if class is set to true.');
} }
}, },
Fsession: function(value) { Fsession_validate: function(value) {
if (Fsession === true) && if ((Fsession === true) &&
(FtimeStart === null || FtimeEnd === null) { (FtimeStart === null || FtimeEnd === null)) {
throw new Error('Start and end times must be defined for' + throw new Error('Start and end times must be defined for' +
' Friday if class is set to true.'); ' Friday if class is set to true.');
} }
......
...@@ -8,12 +8,20 @@ module.exports = function(sequelize, DataTypes) { ...@@ -8,12 +8,20 @@ module.exports = function(sequelize, DataTypes) {
name: DataTypes.STRING, name: DataTypes.STRING,
universitySlug: { universitySlug: {
type: DataTypes.STRING, type: DataTypes.STRING
references: { //references: {
model: University, // model: University,
key: slug // key: slug
//}
}
}, {
classMethods: {
associate: function(models) {
Semester.belongsTo(models.University, { foreignKey: 'slug' } );
} }
} }
}) });
return Semester;
} }
...@@ -15,5 +15,13 @@ module.exports = function(sequelize, DataTypes) { ...@@ -15,5 +15,13 @@ module.exports = function(sequelize, DataTypes) {
type: DataTypes.STRING, type: DataTypes.STRING,
allowNull: false allowNull: false
} }
}) }, {
classMethods: {
associate: function(models) {
University.hasMany(models.Semester, { foreignKey: 'slug' } );
}
}
});
return University;
} }
...@@ -13,16 +13,16 @@ var sequelize = new Sequelize(sequelizeConf); ...@@ -13,16 +13,16 @@ var sequelize = new Sequelize(sequelizeConf);
// empty variable to load entire database into // empty variable to load entire database into
var db = {}; var db = {};
var modelNames = [
'Section',
'Semester',
'University'
]
// this section load every model file in the `models` dir into the database // this section load every model file in the `models` dir into the database
fs modelNames.forEach(function(modelName) {
.readdirSync(__dirname) var model = sequelize.import(path.join(__dirname, modelName));
.flter(function(file) { db[model.name] = model;
return (file.indexOf("." !== 0)) && (file !== "index.js"); });
})
.forEach(function(file) {
var model = sequelize.import(path.join(__dirname, file));
db[model.name] = model;
});
// TODO: figure out exactly what this is doing // TODO: figure out exactly what this is doing
Object.keys(db).forEach(function(modelName) { Object.keys(db).forEach(function(modelName) {
......
...@@ -6,6 +6,7 @@ ...@@ -6,6 +6,7 @@
"start": "node ./bin/www" "start": "node ./bin/www"
}, },
"dependencies": { "dependencies": {
"app-module-path": "^2.1.0",
"body-parser": "~1.15.1", "body-parser": "~1.15.1",
"config": "^1.24.0", "config": "^1.24.0",
"cookie-parser": "~1.4.3", "cookie-parser": "~1.4.3",
......
...@@ -6,9 +6,9 @@ ...@@ -6,9 +6,9 @@
var express = require('express'); var express = require('express');
var router = express.Router(); var router = express.Router();
var mongoose = require('mongoose');
var Semester = require('../../models/Semester'); var Semester = require('../../models/Semester');
var ical = require('ical-generator'); var ical = require('ical-generator');
var db = require('models');
var config = require('config'); var config = require('config');
// Load site wide configurations // Load site wide configurations
...@@ -26,23 +26,17 @@ router.get('/json/schools', function(req, res, next) { ...@@ -26,23 +26,17 @@ router.get('/json/schools', function(req, res, next) {
router.get('/json/classes/:SEMSLUG', function(req, res, next) { router.get('/json/classes/:SEMSLUG', function(req, res, next) {
var slug = req.params['SEMSLUG']; var slug = req.params['SEMSLUG'];
// find each person with matching slug // find each person with matching slug
var query = Semester.findOne({ 'slug': slug });
// remove unwanted fields from request // remove unwanted fields from request
query.select('-_id -classes._id -classes.session_templates._id');
// excecute the request (caches with redis) // excecute the request (caches with redis)
query.lean().exec(function (err, semester) {
// if there was an error or nothing was returned // if there was an error or nothing was returned
if (err || (! semester)) {
// send an error message // send an error message
res.json({
'results' : 'error, try something different'
})
}
// else send out the semester object as json // else send out the semester object as json
else {
res.json(semester);
}
})
}); });
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
......
...@@ -7,6 +7,7 @@ ...@@ -7,6 +7,7 @@
var express = require('express'); var express = require('express');
var router = express.Router(); var router = express.Router();
var config = require('config'); var config = require('config');
var db = require('models');
// Load site wide configurations // Load site wide configurations
var siteInfo = config.get('general'); var siteInfo = config.get('general');
......
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