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 kkak #25

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
Empty file modified Readme.md
100644 → 100755
Empty file.
Empty file modified after_search.png
100644 → 100755
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
17 changes: 17 additions & 0 deletions app.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
const express = require('express')
const app = express()

var restaurant=require('./routes/restaurantroute')
var menu=require('./routes/menuroute')
const bodyparser=require('body-parser')
app.locals.helper=require('./helper/')

app.set('view engine','ejs')

app.use(bodyparser.urlencoded({extended:false}))
app.use(express.static('public'))

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

app.listen(3000,console.log('port 3000 succes'))
Empty file modified before_search.png
100644 → 100755
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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": 123456,
"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"
}
}
Empty file modified edit_menus.png
100644 → 100755
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Empty file modified error_validation.png
100644 → 100755
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
16 changes: 16 additions & 0 deletions helper/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
function formatuang (number){
reverse = number.toString().split('').reverse();
arr= [];
for(var i = 0; i<reverse.length;i++){
if((i+1) % 3 === 0 && (i+1) !== reverse.length){
arr.push(reverse[i]);
arr.push(',');
}else{
arr.push(reverse[i]);
}
}

return 'Rp. '+arr.reverse().join('');
}

module.exports={formatuang:formatuang}
Empty file modified menus.png
100644 → 100755
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
30 changes: 30 additions & 0 deletions migrations/20180301022013-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
},
nama: {
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/20180301022159-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
},
nama: {
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');
}
};
25 changes: 25 additions & 0 deletions migrations/20180301022259-addcolumidrestaurant.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
'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', 'restaurant_Id', Sequelize.INTEGER, {
after: 'price'});
},

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;
56 changes: 56 additions & 0 deletions models/menu.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
'use strict';
const Sequelize = require('sequelize');
const Op = Sequelize.Op;
module.exports = (sequelize, DataTypes) => {
var menu = sequelize.define('menu', {
nama: DataTypes.STRING,
menu_type: {
type: DataTypes.STRING,
validate:{
pilihan(value,next) {
if(value ==='food' || value ==='drink'){
next()
}else{
next('isi menu type food atau drink')
}
}
}
},
rating: DataTypes.INTEGER,
price: DataTypes.INTEGER,
restaurant_Id : {
type:DataTypes.INTEGER,
validate:{
stock(value,next) {
menu.findAll({
where: {
restaurant_Id:value}
}).then(data=>{
if(data.length>5){
next('Varian Food sudah maksimal !')
}else{
next()
}
})
}
}
}
}, {
hooks: {
beforeCreate: (values,option) => {
if (values.price==='0') {
if(values.menu_type==='food'){
values.price=15000
}
else if(values.menu_type==='drink'){
values.price=10000
}
}
}
}
});
menu.associate = function(models) {
menu.belongsTo(models.restaurant, { foreignKey: 'restaurant_Id'})
};
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', {
nama: DataTypes.STRING,
address: DataTypes.STRING
}, {});
restaurant.associate = function(models) {
// associations can be defined here
};
return restaurant;
};
14 changes: 7 additions & 7 deletions package-lock.json
100644 → 100755

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
100644 → 100755
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"
}
}
Empty file modified restaurant.png
100644 → 100755
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Empty file modified restaurant_and_price.png
100644 → 100755
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Empty file modified restaurant_detail.png
100644 → 100755
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
51 changes: 51 additions & 0 deletions routes/menuroute.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
const menuroute = require('express').Router();
const model = require('../models')


menuroute.get('/',(req,res) => {
model.menu.findAll({include:[{model:model.restaurant}]}).then(datas =>{
model.restaurant.findAll().then(datarestaurant =>{
res.render('menu',{data:datas,restaurant:datarestaurant,error:req.query})
})
})
})

menuroute.post('/add',(req,res) => {
model.menu.create({nama:req.body.nama,menu_type:req.body.menu_type,rating:req.body.rating,price:req.body.price,restaurant_Id:req.body.restaurant_id}).then(()=>{
res.redirect('/menus')
}).catch(err => {
res.redirect(`/menus?err=${err.message}`)
})
})


menuroute.get('/update/:id',(req,res) => {
model.menu.findById(req.params.id).then(datas =>{
model.restaurant.findAll().then(datarestaurant =>{
res.render('menuupdateform',{data:datas,restaurant:datarestaurant,error:req.query})
})

})
})

menuroute.post('/update/:id',(req,res) => {
model.menu.update({
nama:req.body.nama,
menu_type:req.body.menu_type,
rating:req.body.rating,
price:req.body.price,
restaurant_Id:req.body.restaurant_id},{where:{id:req.params.id}
}).then(()=>{
res.redirect('/menus')
}).catch(err => {
res.redirect(`/menus/update/${req.params.id} ?err=${err.message}`)
})
})

menuroute.get('/delete/:id',(req,res) => {
model.menu.destroy({where:{id:req.params.id}}).then(()=>{
res.redirect('/menus')
})
})

module.exports = menuroute
17 changes: 17 additions & 0 deletions routes/restaurantroute.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
const routerrestaurant = require('express').Router();
const model = require('../models')


routerrestaurant.get('/',(req,res) => {
model.restaurant.findAll().then(datas =>{
res.render('restaurant',{data:datas})
})
})

routerrestaurant.get('/viewmenu/:id',(req,res) => {
model.restaurant.findAll({include:[{model:model.menu}]}).then(datas =>{
res.send(datas)
})
})

module.exports = routerrestaurant
Empty file modified search_form.png
100644 → 100755
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading