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

Reynaldipane #22

Open
wants to merge 1 commit 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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
node_modules
16 changes: 16 additions & 0 deletions app.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
const express = require('express')
const app = express()
const bodyParser = require('body-parser')
const routes = require('./routes')
const PORT = 3000


app.use(bodyParser.urlencoded({extended:false}))
app.set('view engine', 'ejs')

app.use('/', routes)
app.locals.helper = require('./helpers')

app.listen(PORT,() => {
console.log("Apps run on port " + PORT);
})
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": "purge",
"password": "creatEpwD",
"database": "platinum_livecode4",
"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"
}
}
7 changes: 7 additions & 0 deletions helpers/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
class Helper {
static getPrice(number) {
return 'Rp. '+ parseInt(number).toLocaleString();
}
}

module.exports = Helper;
30 changes: 30 additions & 0 deletions migrations/20180301021014-create-restaurant.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('Restaurants', {
id: {
allowNull: false,
autoIncrement: true,
primaryKey: true,
type: Sequelize.INTEGER
},
name: {
type: Sequelize.STRING
},
address: {
type: Sequelize.STRING
},
createdAt: {
allowNull: false,
type: Sequelize.DATE
},
updatedAt: {
allowNull: false,
type: Sequelize.DATE
}
});
},
down: (queryInterface, Sequelize) => {
return queryInterface.dropTable('Restaurants');
}
};
36 changes: 36 additions & 0 deletions migrations/20180301021134-create-menu.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
'use strict';
module.exports = {
up: (queryInterface, Sequelize) => {
return queryInterface.createTable('Menus', {
id: {
allowNull: false,
autoIncrement: true,
primaryKey: true,
type: Sequelize.INTEGER
},
name: {
type: Sequelize.STRING
},
menu_type: {
type: Sequelize.STRING
},
rating: {
type: Sequelize.INTEGER
},
price: {
type: Sequelize.INTEGER
},
createdAt: {
allowNull: false,
type: Sequelize.DATE
},
updatedAt: {
allowNull: false,
type: Sequelize.DATE
}
});
},
down: (queryInterface, Sequelize) => {
return queryInterface.dropTable('Menus');
}
};
26 changes: 26 additions & 0 deletions migrations/20180301021310-add-RestaurantId-in-Menus.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
'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('Menus','RestaurantId',{
type : Sequelize.INTEGER
})
},

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

Example:
return queryInterface.dropTable('users');
*/
}
};
24 changes: 24 additions & 0 deletions migrations/20180301033947-changeColumn.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 });
*/
return queryInterface.changeColumn('Menus', 'price', Sequelize.STRING);
},

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;
57 changes: 57 additions & 0 deletions models/menu.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
'use strict';
module.exports = (sequelize, DataTypes) => {
var Menu = sequelize.define('Menu', {
name: DataTypes.STRING,
menu_type:{
type:DataTypes.STRING,
validate: {
checkMenuType(value) {
if(value != "food" && value != "drink") {
throw new Error('Isi menu type dengan food / drink')
}
},
checkTooManyFoods(value,callback) {
if(value == "food") {
Menu.findAll({
where : {
RestaurantId : this.RestaurantId,
menu_type : 'food'
}
})
.then(menus => {
if(menus.length >= 5) {
callback('Varian Food sudah maksimal !')
} else {
callback()
}
})
}
}
}
},
rating: DataTypes.INTEGER,
price: DataTypes.INTEGER,
RestaurantId: DataTypes.INTEGER
}, {
hooks :{
beforeCreate : (menu) => {
if (menu.price == '' && menu.menu_type == 'food') {
menu.price = 15000
} else if (menu.price == '' && menu.menu_type == 'drink') {
menu.price = 10000
}
},
beforeUpdate : (menu) => {
if (menu.price == '' && menu.menu_type == 'food') {
menu.price = 15000
} else if (menu.price == '' && menu.menu_type == 'drink') {
menu.price = 10000
}
}
}
});
Menu.associate = function(models) {
Menu.belongsTo(models.Restaurant)
};
return Menu;
};
11 changes: 11 additions & 0 deletions models/restaurant.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
'use strict';
module.exports = (sequelize, DataTypes) => {
var Restaurant = sequelize.define('Restaurant', {
name: DataTypes.STRING,
address: DataTypes.STRING
}, {});
Restaurant.associate = function(models) {
Restaurant.hasMany(models.Menu)
};
return Restaurant;
};
6 changes: 3 additions & 3 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,6 @@
"ejs": "^2.5.7",
"express": "^4.16.2",
"pg": "^7.4.1",
"sequelize": "^4.34.1"
"sequelize": "^4.35.0"
}
}
8 changes: 8 additions & 0 deletions routes/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
const routes = require('express').Router()
const menu = require('./menu')
const restaurant = require('./restaurant')

routes.use('/menus', menu)
routes.use('/restaurants', restaurant)

module.exports = routes;
Loading