Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 12 additions & 3 deletions app.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,26 @@ app.use(bodyParser.urlencoded({ extended: true }));

app.use(morgan('dev'));

app.use('/students', students);
app.use('/tests', tests);
app.use('/students', students); // this already includes the /students, so we should not
app.use('/tests', tests); // specify /students in the actual students.js file. Instead, we should just write /.

app.use((err, req, res, next) => {
console.error(err.stack);
res.status(500).send('Something broke!');
});

// app.get('/', function (req, res, next) {
// try {
// res.redirect('/students/');
// } catch (error) {
// next(error);
// }
// });


const init = async () => {

if (require.main === module){
if (require.main === module) {
//will only run when run with npm start and not with npm test to avoid db syncing in multiple threads when running tests
try {
await db.sync();
Expand Down
27 changes: 26 additions & 1 deletion db/models/student.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,31 @@
const Sequelize = require('sequelize');
const db = require('../db');

const Student = db.define();
const Student = db.define('Student', {
firstName: {
type: Sequelize.STRING,
allowNull: false,
},
lastName: {
type: Sequelize.STRING,
allowNull: false,
},
email: {
type: Sequelize.STRING,
validate: {
isEmail: true
},
allowNull: false,
},
}, {
hooks: {
beforeCreate: (student, options) => {
student.firstName = student.firstName.charAt(0).toUpperCase() + student.firstName.slice(1);
student.lastName = student.lastName.charAt(0).toUpperCase() + student.lastName.slice(1);
}
}
});



module.exports = Student;
13 changes: 12 additions & 1 deletion db/models/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,17 @@ const Sequelize = require('sequelize');
const db = require('../db');
const Student = require('./student');

const Test = db.define();
const Test = db.define('Test', {
subject: {
type: Sequelize.STRING,
allowNull: false,
},
grade: {
type: Sequelize.INTEGER,
allowNull: false,
},
});

Test.belongsTo(Student, { as: "student" }) // it has to match up with student.js's alias

module.exports = Test;
Loading