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

RizkyAgung #19

Open
wants to merge 8 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
25 changes: 25 additions & 0 deletions app.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
const express = require('express');
const bodyParser = require('body-parser')
const Teachers = require('./routers/teachersRouter');
const Subjects = require('./routers/subjectsRouter');
const Students = require('./routers/studentsRouter');
const app = express();

app.set('view engine', 'ejs');
app.set('views', './views');

app.use(bodyParser.urlencoded({ extended: false }))
app.use(bodyParser.json())

app.use('/teachers', Teachers);
app.use('/subjects', Subjects);
app.use('/students', Students);


app.get('/', (req, res) => {
res.render('home', {pageTitle:"Home"});
})

app.listen(3000, function() {
console.log("IT WORKS!!");
});
9 changes: 9 additions & 0 deletions config/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"development": {
"username": "postgres",
"password": "postgres",
"database": "school-app",
"host": "127.0.0.1",
"dialect": "postgres"
}
}
30 changes: 30 additions & 0 deletions migrations/20171102071826-create-teacher.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('Teachers', {
id: {
allowNull: false,
autoIncrement: true,
primaryKey: true,
type: Sequelize.INTEGER
},
first_name: {
type: Sequelize.STRING
},
last_name: {
type: Sequelize.STRING
},
createdAt: {
allowNull: false,
type: Sequelize.DATE
},
updatedAt: {
allowNull: false,
type: Sequelize.DATE
}
});
},
down: (queryInterface, Sequelize) => {
return queryInterface.dropTable('Teachers');
}
};
11 changes: 11 additions & 0 deletions migrations/20171102075024-add-email-Teacher.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
'use strict';

module.exports = {
up: (queryInterface, Sequelize) => {
return queryInterface.addColumn('Teachers','email',{type:Sequelize.STRING});
},

down: (queryInterface, Sequelize) => {
return queryInterface.removeColumn('Teachers','email');
}
};
27 changes: 27 additions & 0 deletions migrations/20171102085948-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
},
subject_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/20171102141146-create-student.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('Students', {
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('Students');
}
};
11 changes: 11 additions & 0 deletions migrations/20171104071349-add_column-subjectID-teachers.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
'use strict';

module.exports = {
up: (queryInterface, Sequelize) => {
return queryInterface.addColumn('Teachers','SubjectId',{type:Sequelize.INTEGER});
},

down: (queryInterface, Sequelize) => {
return queryInterface.removeColumn('Teachers','SubjectId');;
}
};
30 changes: 30 additions & 0 deletions migrations/20171105033042-create-students-subjects.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-Subjects', {
id: {
allowNull: false,
autoIncrement: true,
primaryKey: true,
type: Sequelize.INTEGER
},
StudentId: {
type: Sequelize.INTEGER
},
SubjectId: {
type: Sequelize.INTEGER
},
createdAt: {
allowNull: false,
type: Sequelize.DATE
},
updatedAt: {
allowNull: false,
type: Sequelize.DATE
}
});
},
down: (queryInterface, Sequelize) => {
return queryInterface.dropTable('Students-Subjects');
}
};
11 changes: 11 additions & 0 deletions migrations/20171105125232-rename-students-subjects.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
'use strict';

module.exports = {
up: (queryInterface, Sequelize) => {
return queryInterface.renameTable('Students-Subjects', 'Students_Subjects');
},

down: (queryInterface, Sequelize) => {
return queryInterface.renameTable('Students_Subjects', 'Students-Subjects');
}
};
11 changes: 11 additions & 0 deletions migrations/20171106044240-addColumn-score-Students_Subjects.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
'use strict';

module.exports = {
up: (queryInterface, Sequelize) => {
return queryInterface.addColumn('Students_Subjects', 'score', {type : Sequelize.INTEGER})
},

down: (queryInterface, Sequelize) => {
return queryInterface.removeColumn('Students_Subjects', 'score')
}
};
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]);
} 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;
21 changes: 21 additions & 0 deletions models/student.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
'use strict';
module.exports = (sequelize, DataTypes) => {
var Student = sequelize.define('Student', {
first_name: DataTypes.STRING,
last_name: DataTypes.STRING,
email: {
type: DataTypes.STRING,
validate: {
isEmail: true
}
}
});
Student.prototype.getFullName = function () {
return `${this.first_name} ${this.last_name}`;
};
Student.associate = models => {
Student.belongsToMany(models.Subject, { through: 'Students_Subject' });
Student.hasMany(models.Students_Subject)
}
return Student;
};
21 changes: 21 additions & 0 deletions models/students-subjects.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
'use strict';
module.exports = (sequelize, DataTypes) => {
var Students_Subject = sequelize.define('Students_Subject', {
id: {
primaryKey: true,
autoIncrement: true,
type: DataTypes.INTEGER
},
StudentId: DataTypes.INTEGER,
SubjectId: DataTypes.INTEGER,
score: DataTypes.INTEGER
});
Students_Subject.associate = models => {
Students_Subject.belongsTo(models.Student, {foreignKey : 'StudentId'})
Students_Subject.belongsTo(models.Subject, {foreignKey : 'SubjectId'})
}
Students_Subject.prototype.getId = function () {
return this.id
}
return Students_Subject;
};
12 changes: 12 additions & 0 deletions models/subject.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
'use strict';
module.exports = (sequelize, DataTypes) => {
var Subject = sequelize.define('Subject', {
subject_name: DataTypes.STRING
});
Subject.associate = (models) => {
Subject.hasMany(models.Teacher);
Subject.belongsToMany(models.Student, { through: 'Students_Subject' })
Subject.hasMany(models.Students_Subject)
}
return Subject;
};
17 changes: 17 additions & 0 deletions models/teacher.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
'use strict';
module.exports = (sequelize, DataTypes) => {
var Teacher = sequelize.define('Teacher', {
first_name: DataTypes.STRING,
last_name: DataTypes.STRING,
email: {
type: DataTypes.STRING,
validate: {
isEmail: true
}
}
});
Teacher.associate = (models) => {
Teacher.belongsTo(models.Subject);
};
return Teacher;
};
Loading