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

Haripermadi #18

Open
wants to merge 7 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
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
.DS_Store
node_modules
18 changes: 18 additions & 0 deletions app.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
const express = require('express');
const app = express();
const bodyParser = require('body-parser');

const menus = require('./routes/menus')
const resto = require('./routes/restaurants')



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

app.set('view engine', 'ejs');
app.use('/menus',menus)
app.use('/restaurants',resto)


app.listen(3000, () => console.log('connected!'))
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": "hari2018",
"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"
}
}
4 changes: 4 additions & 0 deletions helpers/currency.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
function format_currency (number){
return 'Rp. '+number.toLocaleString();
}
module.exports = {format_currency}
30 changes: 30 additions & 0 deletions migrations/20180301021312-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/20180301021506-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/20180301021718-add_restaurantId.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',Sequelize.INTEGER, {});
},

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

Example:
return queryInterface.dropTable('users');
*/
return queryInterface.removeColumn('Menus','RestaurantId',null, {});
}
};

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;
45 changes: 45 additions & 0 deletions models/menu.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
'use strict';
module.exports = (sequelize, DataTypes) => {
var Menu = sequelize.define('Menu', {
name: DataTypes.STRING,
menu_type:{
type:DataTypes.STRING,
validate:{
isIn: {
args: [['food', 'drink']],
msg: "Isi menu type dengan food / drink"
}
}
} ,
rating: DataTypes.INTEGER,
price: DataTypes.INTEGER,
RestaurantId : DataTypes.INTEGER,
},{
hooks:{
beforeCreate:function(Menu,options){
console.log(Menu)
if(Menu.price == 0 || Menu.price === null){
if(Menu.menu_type === 'food'){
Menu.price = 15000
}else{
Menu.price = 10000
}
}
},
beforeUpdate:function(Menu,options){
console.log(Menu)
if(Menu.price == 0 || Menu.price === null){
if(Menu.menu_type === 'food'){
Menu.price = 15000
}else{
Menu.price = 10000
}
}
}}
});

Menu.associate = function(models){
Menu.belongsTo(models.Restaurant,{foreignKey: 'RestaurantId'})
};
return Menu;
};
21 changes: 21 additions & 0 deletions models/restaurant.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
'use strict';
module.exports = (sequelize, DataTypes) => {
var Restaurant = sequelize.define('Restaurant', {
name: DataTypes.STRING,
address: DataTypes.STRING
},{
validate:{
maxFood:function(idresto,menu){
sequelize.models.Restaurant.findOne({
where:{id:idresto}
})
}
}
});

Restaurant.associate = function(models){
Restaurant.hasMany(models.Menu,{foreignKey: 'RestaurantId'})
};

return Restaurant;
};
74 changes: 74 additions & 0 deletions routes/menus.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
const express = require('express');
const app = express();
const router = express.Router()
const models = require('../models')
const Menu = models.Menu
const Restaurant = models.Restaurant
const currency = require('../helpers/currency')

router.get('/',function(req,res){
Menu.findAll({
include:[Restaurant]
}).then(data=>{
Restaurant.findAll().then(listRestaurant=>{
// res.send(data)
res.render('menu/list_menu',{dataMenu:data,dataResto:listRestaurant,format:currency,error:null})
})
}).catch(err=>{
res.send(err)
})
})

router.post('/',function(req,res){
console.log(req.body)
Menu.create(req.body).then(data=>{
res.redirect('/menus')
}).catch(err=>{
Menu.findAll({
include:[Restaurant]
}).then(data=>{
Restaurant.findAll().then(listRestaurant=>{
// res.send(data)
res.render('menu/list_menu',{dataMenu:data,dataResto:listRestaurant,format:currency,error:err.errors[0].message})
})
})
// res.render('menu/list_menu',{dataMenu:data,error:err.errors[0].message})
})
})

router.get('/:id/edit',function(req,res){
let id = req.params.id
Menu.findById(id).then(data=>{
// res.send(data)
Restaurant.findAll().then(detail=>{
res.render('menu/edit_menu',{menu:data,dataResto:detail})
})

}).catch(err=>{
res.send(err)
})
})

router.post('/:id/edit',function(req,res){
let id = req.params.id
console.log(id)
Menu.update(req.body,{
where:{id:id},
individualHooks:true
}).then(data=>{
res.redirect('/menus')
}).catch(err=>{
res.send(err)
})
})
router.get('/:id/delete',function(req,res){
let id = req.params.id
Menu.destroy({
where:{id:id}})
.then(()=>{
res.redirect('/menus')
})
})

module.exports = router

49 changes: 49 additions & 0 deletions routes/restaurants.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
const express = require('express');
const app = express();
const router = express.Router()
const models = require('../models')
const Menu = models.Menu
const Restaurant = models.Restaurant
const currency = require('../helpers/currency')
const Op = require('sequelize').Op

router.get('/',function(req,res){
Restaurant.findAll().then(data=>{
res.render('resto/list_resto',{resto:data})
}).catch(err=>{
res.send(err)
})
})
router.get('/:id/view_menu',function(req,res){
Restaurant.findOne({
include:[Menu]
}).then(data=>{
// res.send(data)
res.render('resto/view_menu',{menu:data,format:currency})
}).catch(err=>{
res.send(err)
})
})
router.post('/:id/search',function(req,res){
let keyword = req.body.keyword
let category = req.body.category
// let obj={};
// obj[category]

// console.log(obj)
console.log(category)
Restaurant.findOne({
include:[{model:Menu,where:{[category]:{[Op.iLike]:`%${keyword}%`}}}],
where:{
id:req.params.id
}
}).then(data=>{
// res.send(data)
res.render('resto/view_menu',{menu:data,format:currency})

}).catch(err=>{
res.send(err)
})
})
module.exports = router

Loading