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

initial commit #10

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
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
26 changes: 26 additions & 0 deletions app.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
const express = require('express')
const bodyParser = require('body-parser')
const app = express()
const db = require('./models')

// parse application/x-www-form-urlencoded
app.use(bodyParser.urlencoded({ extended: false }))

// parse application/json
app.use(bodyParser.json())

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

const teacher = require('./router/teacher')
const subject = require('./router/subject')
const student = require('./router/student');

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

app.listen(3000, function () {
db.sequelize.sync();
console.log('Example app listening on port 3000!')
})
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": "skull",
"host": "127.0.0.1",
"dialect": "postgres"
}
}
15 changes: 15 additions & 0 deletions helpers/score_to_abc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
function scoreToABC (obj){
if(obj > 85){
return 'A'
}else if(obj > 70 && obj <= 85){
return 'B'
}else if(obj > 55 && obj <= 70){
return 'C'
}else if(obj > 30 && obj <= 55){
return 'D'
}else{
return null
}
}

module.exports = scoreToABC
30 changes: 30 additions & 0 deletions migrations/20171102123807-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/20171102134031-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');
}
};
25 changes: 25 additions & 0 deletions migrations/20171102135756-add-new-email-to-user.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
'use strict';

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

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

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

Example:
return queryInterface.dropTable('users');
*/
}
};
33 changes: 33 additions & 0 deletions migrations/20171103135859-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');
}
};
25 changes: 25 additions & 0 deletions migrations/20171105112548-add-subjectid-to-teacher.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
'use strict';

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

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

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

Example:
return queryInterface.dropTable('users');
*/
}
};
30 changes: 30 additions & 0 deletions migrations/20171106060404-create-conj-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('ConjStudentSubjects', {
id: {
allowNull: false,
autoIncrement: true,
primaryKey: true,
type: Sequelize.INTEGER
},
StudentId: {
type: Sequelize.INTEGER
},
SubjejectId: {
type: Sequelize.INTEGER
},
createdAt: {
allowNull: false,
type: Sequelize.DATE
},
updatedAt: {
allowNull: false,
type: Sequelize.DATE
}
});
},
down: (queryInterface, Sequelize) => {
return queryInterface.dropTable('ConjStudentSubjects');
}
};
30 changes: 30 additions & 0 deletions migrations/20171106065911-create-subject-student.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('SubjectStudents', {
id: {
allowNull: false,
autoIncrement: true,
primaryKey: true,
type: Sequelize.INTEGER
},
SubjectId: {
type: Sequelize.INTEGER
},
StudentId: {
type: Sequelize.INTEGER
},
createdAt: {
allowNull: false,
type: Sequelize.DATE
},
updatedAt: {
allowNull: false,
type: Sequelize.DATE
}
});
},
down: (queryInterface, Sequelize) => {
return queryInterface.dropTable('SubjectStudents');
}
};
25 changes: 25 additions & 0 deletions migrations/20171106120322-add-score-to-subjectstudent.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
'use strict';

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

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

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

Example:
return queryInterface.dropTable('users');
*/
}
};
14 changes: 14 additions & 0 deletions models/conjstudentsubject.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
'use strict';
module.exports = (sequelize, DataTypes) => {
var ConjStudentSubject = sequelize.define('ConjStudentSubject', {
StudentId: DataTypes.INTEGER,
SubjejectId: DataTypes.INTEGER
}, {
classMethods: {
associate: function(models) {
// associations can be defined here
}
}
});
return ConjStudentSubject;
};
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;
42 changes: 42 additions & 0 deletions models/student.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
'use strict';
module.exports = (sequelize, DataTypes) => {
var Student = sequelize.define('Student', {
first_name: {
type: DataTypes.STRING,
validate: {
isAlpha: {msg: 'No number in your name right?'},
notEmpty: {msg: 'How should i call you?'}
}
},
last_name: {
type: DataTypes.STRING,
validate: {
isAlpha: {msg: 'No number in your name right?'},
notEmpty: {msg: 'How should i call you?'}
}
},
email: {
type: DataTypes.STRING,
validate: {
isEmail: {msg: 'Email format should be like this: [user]@[domainname].[domain]'}
}
}
}, {
classMethods: {
associate: function(models) {
// associations can be defined here
}
}
});

Student.prototype.gabungNama = function(){
return `${this.first_name} ${this.last_name}`
}

Student.associate = function(models){
Student.belongsToMany(models.Subject, {through: 'SubjectStudent'})
Student.hasMany(models.SubjectStudent)
}

return Student;
};
Loading