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

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