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

DONE CRUD #10

Open
wants to merge 4 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
13 changes: 13 additions & 0 deletions app.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
"use strict"
const app = require('express')();
const bodyParser = require('body-parser');
const routes = require('./routes');
const model = require('./models');

app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());
app.use('/',routes);
app.set('view engine', 'ejs');
app.listen(3000, () => {
console.log('App Listening on port 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": "admin",
"password": 12345,
"database": "sekolah",
"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"
}
}
Binary file added express-from-scratch.tar.gz
Binary file not shown.
30 changes: 30 additions & 0 deletions migrations/20180225025926-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');
}
};
27 changes: 27 additions & 0 deletions migrations/20180225033851-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');
}
};
29 changes: 29 additions & 0 deletions migrations/20180225113114-addForeingKey.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
'use strict';

module.exports = {
up: (queryInterface, Sequelize) => {
return queryInterface.addColumn('Teachers',
'id_subject',
{
type: Sequelize.INTEGER,
},
);
/*
Add altering commands here.
Return a promise to correctly handle asynchronicity.

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

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

Example:
return queryInterface.dropTable('users');
*/
}
};
30 changes: 30 additions & 0 deletions migrations/20180226085916-create-student-subject.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('StudentSubjects', {
id: {
allowNull: false,
autoIncrement: true,
primaryKey: true,
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('StudentSubjects');
}
};
24 changes: 24 additions & 0 deletions migrations/20180226131904-addScore.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
'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 });
*/
},

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

Example:
return queryInterface.dropTable('users');
*/
}
};
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;
16 changes: 16 additions & 0 deletions models/student.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
'use strict';
module.exports = (sequelize, DataTypes) => {
var Student = sequelize.define('Student', {
first_name: DataTypes.STRING,
last_name: DataTypes.STRING,
email: DataTypes.STRING,
}, {});
Student.associate = function(models) {
// associations can be defined here
Student.belongsToMany(models.Subject, { through: models.StudentSubject ,foreignKey: 'id_student'});
Student.hasMany(models.StudentSubject,{
foreignKey: 'id_student'
})
};
return Student;
};
14 changes: 14 additions & 0 deletions models/studentsubject.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
'use strict';
module.exports = (sequelize, DataTypes) => {
var StudentSubject = sequelize.define('StudentSubject', {
id:{ type:DataTypes.INTEGER,primaryKey: true,autoIncrement: true},
id_student: DataTypes.INTEGER,
id_subject: DataTypes.INTEGER,
score: DataTypes.INTEGER
}, {});
StudentSubject.associate = function(models) {
StudentSubject.belongsTo(models.Student,{foreignKey:'id_student'})
StudentSubject.belongsTo(models.Subject,{foreignKey:'id_subject'})
};
return StudentSubject;
};
20 changes: 20 additions & 0 deletions models/subject.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
'use strict';
module.exports = (sequelize, DataTypes) => {
var Subject = sequelize.define('Subject', {
subject_name: DataTypes.STRING
}, {});
Subject.associate = function(models) {
// associations can be defined here
Subject.hasMany(models.Teacher, {
foreignKey: 'id_subject'
})

Subject.belongsToMany(models.Student, { through: models.StudentSubject, foreignKey: 'id_subject'});
Subject.hasMany(models.StudentSubject,{
foreignKey: 'id_subject'
})


};
return Subject;
};
16 changes: 16 additions & 0 deletions models/teacher.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
'use strict';
module.exports = (sequelize, DataTypes) => {
var Teacher = sequelize.define('Teacher', {
first_name: DataTypes.STRING,
last_name: DataTypes.STRING,
email: DataTypes.STRING,
id_subject : DataTypes.INTEGER
}, {});
Teacher.associate = function(models) {
// associations can be defined here
Teacher.belongsTo(models.Subject, {
foreignKey: 'id_subject'
})
};
return Teacher;
};
Loading