Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Agrha #25

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
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
28 changes: 28 additions & 0 deletions app.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
const express = require('express')
const body = require('body-parser')
const app = express()

let index = require('./routers/index')
let teacher = require ('./routers/teachers')
let student = require ('./routers/students')
let subject = require ('./routers/subject')

app.set('views', './views')
app.set('view engine', 'ejs')
app.use(body.json())
app.use(body.urlencoded({ extended: false }))

app.use('/teachers',teacher)
app.use('/students',student)
app.use('/subjects',subject)
app.use('/',index)







app.listen(3000, function(){
console.log('AYE AYE CAPT:3000');
})
23 changes: 23 additions & 0 deletions config/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
"development": {
"username": "postgres",
"password": "postgres",
"database": "express_from_scratch",
"host": "127.0.0.1",
"dialect": "postgres"
},
"test": {
"username": "root",
"password": null,
"database": "database_test",
"host": "127.0.0.1",
"dialect": "mysql"
},
"production": {
"username": "root",
"password": null,
"database": "database_production",
"host": "127.0.0.1",
"dialect": "mysql"
}
}
33 changes: 33 additions & 0 deletions migrations/20180223105542-create-teachers.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
'use strict';
module.exports = {
up: (queryInterface, Sequelize) => {
return queryInterface.createTable('Teachers', {
id: {
allowNull: false,
autoIncrement: true,
primaryKey: true,
type: Sequelize.INTEGER
},
first_name: {
type: Sequelize.STRING
},
last_name: {
type: Sequelize.STRING
},
email: {
type: Sequelize.STRING
},
createdAt: {
allowNull: false,
type: Sequelize.DATE
},
updatedAt: {
allowNull: false,
type: Sequelize.DATE
}
});
},
down: (queryInterface, Sequelize) => {
return queryInterface.dropTable('Teachers');
}
};
27 changes: 27 additions & 0 deletions migrations/20180225222846-create-subject.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
'use strict';
module.exports = {
up: (queryInterface, Sequelize) => {
return queryInterface.createTable('Subjects', {
id: {
allowNull: false,
autoIncrement: true,
primaryKey: true,
type: Sequelize.INTEGER
},
name: {
type: Sequelize.STRING
},
createdAt: {
allowNull: false,
type: Sequelize.DATE
},
updatedAt: {
allowNull: false,
type: Sequelize.DATE
}
});
},
down: (queryInterface, Sequelize) => {
return queryInterface.dropTable('Subjects');
}
};
33 changes: 33 additions & 0 deletions migrations/20180225223023-create-score.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
'use strict';
module.exports = {
up: (queryInterface, Sequelize) => {
return queryInterface.createTable('Scores', {
id: {
allowNull: false,
autoIncrement: true,
primaryKey: true,
type: Sequelize.INTEGER
},
score: {
type: Sequelize.INTEGER
},
id_student: {
type: Sequelize.INTEGER
},
id_subject: {
type: Sequelize.INTEGER
},
createdAt: {
allowNull: false,
type: Sequelize.DATE
},
updatedAt: {
allowNull: false,
type: Sequelize.DATE
}
});
},
down: (queryInterface, Sequelize) => {
return queryInterface.dropTable('Scores');
}
};
30 changes: 30 additions & 0 deletions migrations/20180225223223-create-students.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
'use strict';
module.exports = {
up: (queryInterface, Sequelize) => {
return queryInterface.createTable('Students', {
id: {
allowNull: false,
autoIncrement: true,
primaryKey: true,
type: Sequelize.INTEGER
},
name: {
type: Sequelize.STRING
},
email: {
type: Sequelize.STRING
},
createdAt: {
allowNull: false,
type: Sequelize.DATE
},
updatedAt: {
allowNull: false,
type: Sequelize.DATE
}
});
},
down: (queryInterface, Sequelize) => {
return queryInterface.dropTable('Students');
}
};
25 changes: 25 additions & 0 deletions migrations/20180225223310-addColumn.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
'use strict';

module.exports = {
up: (queryInterface, Sequelize) => {
/*
Add altering commands here.
Return a promise to correctly handle asynchronicity.

Example:
return queryInterface.createTable('users', { id: Sequelize.INTEGER });
*/
return queryInterface.addColumn('Teachers', 'id_subject', Sequelize.INTEGER)
},

down: (queryInterface, Sequelize) => {
/*
Add reverting commands here.
Return a promise to correctly handle asynchronicity.

Example:
return queryInterface.dropTable('users');
*/
return queryInterface.removeColumn('Teachers', 'id_subject');
}
};
36 changes: 36 additions & 0 deletions models/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
'use strict';

var fs = require('fs');
var path = require('path');
var Sequelize = require('sequelize');
var basename = path.basename(__filename);
var env = process.env.NODE_ENV || 'development';
var config = require(__dirname + '/../config/config.json')[env];
var db = {};

if (config.use_env_variable) {
var sequelize = new Sequelize(process.env[config.use_env_variable], config);
} else {
var sequelize = new Sequelize(config.database, config.username, config.password, config);
}

fs
.readdirSync(__dirname)
.filter(file => {
return (file.indexOf('.') !== 0) && (file !== basename) && (file.slice(-3) === '.js');
})
.forEach(file => {
var model = sequelize['import'](path.join(__dirname, file));
db[model.name] = model;
});

Object.keys(db).forEach(modelName => {
if (db[modelName].associate) {
db[modelName].associate(db);
}
});

db.sequelize = sequelize;
db.Sequelize = Sequelize;

module.exports = db;
17 changes: 17 additions & 0 deletions models/score.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
'use strict';
module.exports = (sequelize, DataTypes) => {
var Score = sequelize.define('Score', {
score: DataTypes.INTEGER,
id_student: DataTypes.INTEGER,
id_subject: DataTypes.INTEGER
}, {});
Score.associate = models=>{
Score.belongsTo(models.Subject,{
foreignKey:'id_subject'
})
Score.belongsTo(models.Students,{
foreignKey:'id_student'
})
}
return Score;
};
14 changes: 14 additions & 0 deletions models/students.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
'use strict';
module.exports = (sequelize, DataTypes) => {
var Students = sequelize.define('Students', {
name: DataTypes.STRING,
email: DataTypes.STRING
}, {});
Students.associate = models=>{
Students.belongsToMany(models.Subject,{
through:'Score',
foreignKey:'id_student'
})
}
return Students;
};
16 changes: 16 additions & 0 deletions models/subject.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
'use strict';
module.exports = (sequelize, DataTypes) => {
var Subject = sequelize.define('Subject', {
name: DataTypes.STRING
}, {});
Subject.associate = function (models) {
Subject.hasMany(models.Teachers,{
foreignKey:'id_subject'
})
Subject.belongsToMany(models.Students,{
through:'Score',
foreignKey:'id_subject'
})
};
return Subject;
};
17 changes: 17 additions & 0 deletions models/teachers.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
'use strict';
module.exports = (sequelize, DataTypes) => {
var Teachers = sequelize.define('Teachers', {
first_name: DataTypes.STRING,
last_name: DataTypes.STRING,
email: DataTypes.STRING
}, {});
Teachers.associate = function (models) {
Teachers.belongsTo(models.Subject,{
foreignKey:'id_subject'
})
};
Teachers.prototype.fullName = function(){
return `${this.first_name} ${this.last_name}`
}
return Teachers;
};
Loading