From b54a830730bd050a875c29450eba99dc1f4554bc Mon Sep 17 00:00:00 2001 From: irsyadpahlapi Date: Wed, 28 Feb 2018 23:53:28 -0500 Subject: [PATCH] done kkak --- .gitignore | 1 + Readme.md | 0 after_search.png | Bin app.js | 17 +++++ before_search.png | Bin config/config.json | 23 ++++++ edit_menus.png | Bin error_validation.png | Bin helper/index.js | 16 +++++ menus.png | Bin .../20180301022013-create-restaurant.js | 30 ++++++++ migrations/20180301022159-create-menu.js | 36 ++++++++++ .../20180301022259-addcolumidrestaurant.js | 25 +++++++ models/index.js | 36 ++++++++++ models/menu.js | 56 +++++++++++++++ models/restaurant.js | 11 +++ package-lock.json | 14 ++-- package.json | 2 +- restaurant.png | Bin restaurant_and_price.png | Bin restaurant_detail.png | Bin routes/menuroute.js | 51 ++++++++++++++ routes/restaurantroute.js | 17 +++++ search_form.png | Bin seeders/20180301023728-addmenu.js | 26 +++++++ seeders/20180301024008-addrestaurant.js | 57 +++++++++++++++ views/menu.ejs | 66 ++++++++++++++++++ views/menuupdateform.ejs | 47 +++++++++++++ views/restaurant.ejs | 23 ++++++ views/restaurantview.ejs | 17 +++++ 30 files changed, 563 insertions(+), 8 deletions(-) create mode 100755 .gitignore mode change 100644 => 100755 Readme.md mode change 100644 => 100755 after_search.png create mode 100755 app.js mode change 100644 => 100755 before_search.png create mode 100755 config/config.json mode change 100644 => 100755 edit_menus.png mode change 100644 => 100755 error_validation.png create mode 100644 helper/index.js mode change 100644 => 100755 menus.png create mode 100755 migrations/20180301022013-create-restaurant.js create mode 100755 migrations/20180301022159-create-menu.js create mode 100755 migrations/20180301022259-addcolumidrestaurant.js create mode 100755 models/index.js create mode 100755 models/menu.js create mode 100755 models/restaurant.js mode change 100644 => 100755 package-lock.json mode change 100644 => 100755 package.json mode change 100644 => 100755 restaurant.png mode change 100644 => 100755 restaurant_and_price.png mode change 100644 => 100755 restaurant_detail.png create mode 100644 routes/menuroute.js create mode 100755 routes/restaurantroute.js mode change 100644 => 100755 search_form.png create mode 100755 seeders/20180301023728-addmenu.js create mode 100755 seeders/20180301024008-addrestaurant.js create mode 100644 views/menu.ejs create mode 100644 views/menuupdateform.ejs create mode 100644 views/restaurant.ejs create mode 100644 views/restaurantview.ejs diff --git a/.gitignore b/.gitignore new file mode 100755 index 0000000..3c3629e --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +node_modules diff --git a/Readme.md b/Readme.md old mode 100644 new mode 100755 diff --git a/after_search.png b/after_search.png old mode 100644 new mode 100755 diff --git a/app.js b/app.js new file mode 100755 index 0000000..c67a5ba --- /dev/null +++ b/app.js @@ -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')) diff --git a/before_search.png b/before_search.png old mode 100644 new mode 100755 diff --git a/config/config.json b/config/config.json new file mode 100755 index 0000000..43b7374 --- /dev/null +++ b/config/config.json @@ -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" + } +} diff --git a/edit_menus.png b/edit_menus.png old mode 100644 new mode 100755 diff --git a/error_validation.png b/error_validation.png old mode 100644 new mode 100755 diff --git a/helper/index.js b/helper/index.js new file mode 100644 index 0000000..45ca34c --- /dev/null +++ b/helper/index.js @@ -0,0 +1,16 @@ +function formatuang (number){ + reverse = number.toString().split('').reverse(); + arr= []; + for(var i = 0; i { + 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'); + } +}; \ No newline at end of file diff --git a/migrations/20180301022159-create-menu.js b/migrations/20180301022159-create-menu.js new file mode 100755 index 0000000..20e27d2 --- /dev/null +++ b/migrations/20180301022159-create-menu.js @@ -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'); + } +}; \ No newline at end of file diff --git a/migrations/20180301022259-addcolumidrestaurant.js b/migrations/20180301022259-addcolumidrestaurant.js new file mode 100755 index 0000000..29be43e --- /dev/null +++ b/migrations/20180301022259-addcolumidrestaurant.js @@ -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'); + */ + } +}; diff --git a/models/index.js b/models/index.js new file mode 100755 index 0000000..5662f10 --- /dev/null +++ b/models/index.js @@ -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; diff --git a/models/menu.js b/models/menu.js new file mode 100755 index 0000000..4deafa9 --- /dev/null +++ b/models/menu.js @@ -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; +}; diff --git a/models/restaurant.js b/models/restaurant.js new file mode 100755 index 0000000..6530ab4 --- /dev/null +++ b/models/restaurant.js @@ -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; +}; \ No newline at end of file diff --git a/package-lock.json b/package-lock.json old mode 100644 new mode 100755 index f726f40..71aad44 --- a/package-lock.json +++ b/package-lock.json @@ -15,9 +15,9 @@ "integrity": "sha512-CTUtLb6WqCCgp6P59QintjHWqzf4VL1uPA27bipLAPxFqrtK1gEYllePzTICGqQ8rYsCbpnsNypXjjDzGAAjEQ==" }, "accepts": { - "version": "1.3.4", - "resolved": "https://registry.npmjs.org/accepts/-/accepts-1.3.4.tgz", - "integrity": "sha1-hiRnWMfdbSGmR0/whKR0DsBesh8=", + "version": "1.3.5", + "resolved": "https://registry.npmjs.org/accepts/-/accepts-1.3.5.tgz", + "integrity": "sha1-63d99gEXI6OxTopywIBcjoZ0a9I=", "requires": { "mime-types": "2.1.18", "negotiator": "0.6.1" @@ -142,7 +142,7 @@ "resolved": "https://registry.npmjs.org/express/-/express-4.16.2.tgz", "integrity": "sha1-41xt/i1kt9ygpc1PIXgb4ymeB2w=", "requires": { - "accepts": "1.3.4", + "accepts": "1.3.5", "array-flatten": "1.1.1", "body-parser": "1.18.2", "content-disposition": "0.5.2", @@ -498,9 +498,9 @@ } }, "sequelize": { - "version": "4.34.1", - "resolved": "https://registry.npmjs.org/sequelize/-/sequelize-4.34.1.tgz", - "integrity": "sha512-RWV/DrjfjaAcBqCjNntgknko4r9dbw40HFSdEhHAv3vMa6v6abhHMmou6ZFUXxaTXNo0JxANukg124/HgLrvyw==", + "version": "4.35.0", + "resolved": "https://registry.npmjs.org/sequelize/-/sequelize-4.35.0.tgz", + "integrity": "sha512-U/pMHkxzisEQQnZ18dh2qiKhdhF7/b+IH2vhQ9PFisHBcYRcaivt2y9jhUx/ZCUAgrfgAL1Bv24hpVzVBsSv8g==", "requires": { "bluebird": "3.5.1", "cls-bluebird": "2.1.0", diff --git a/package.json b/package.json old mode 100644 new mode 100755 index f15ec6e..5ba59e0 --- a/package.json +++ b/package.json @@ -13,6 +13,6 @@ "ejs": "^2.5.7", "express": "^4.16.2", "pg": "^7.4.1", - "sequelize": "^4.34.1" + "sequelize": "^4.35.0" } } diff --git a/restaurant.png b/restaurant.png old mode 100644 new mode 100755 diff --git a/restaurant_and_price.png b/restaurant_and_price.png old mode 100644 new mode 100755 diff --git a/restaurant_detail.png b/restaurant_detail.png old mode 100644 new mode 100755 diff --git a/routes/menuroute.js b/routes/menuroute.js new file mode 100644 index 0000000..c36bd49 --- /dev/null +++ b/routes/menuroute.js @@ -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 diff --git a/routes/restaurantroute.js b/routes/restaurantroute.js new file mode 100755 index 0000000..9fe24b8 --- /dev/null +++ b/routes/restaurantroute.js @@ -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 diff --git a/search_form.png b/search_form.png old mode 100644 new mode 100755 diff --git a/seeders/20180301023728-addmenu.js b/seeders/20180301023728-addmenu.js new file mode 100755 index 0000000..e667a8b --- /dev/null +++ b/seeders/20180301023728-addmenu.js @@ -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.bulkInsert('Person', [{ + name: 'John Doe', + isBetaMember: false + }], {}); + */ + }, + + down: (queryInterface, Sequelize) => { + /* + Add reverting commands here. + Return a promise to correctly handle asynchronicity. + + Example: + return queryInterface.bulkDelete('Person', null, {}); + */ + } +}; diff --git a/seeders/20180301024008-addrestaurant.js b/seeders/20180301024008-addrestaurant.js new file mode 100755 index 0000000..0af55af --- /dev/null +++ b/seeders/20180301024008-addrestaurant.js @@ -0,0 +1,57 @@ +'use strict'; + +module.exports = { + up: (queryInterface, Sequelize) => { + /* + Add altering commands here. + Return a promise to correctly handle asynchronicity. + + Example: + return queryInterface.bulkInsert('Person', [{ + name: 'John Doe', + isBetaMember: false + }], {}); + */ + + return queryInterface.bulkInsert('restaurants', [{ + nama: 'hokben', + address: 'imogiri barat', + createdAt:new Date(), + updatedAt: new Date() + }, + { + nama: 'kfc', + address: 'pondok pinang', + createdAt:new Date(), + updatedAt: new Date() + }, + { + nama: 'mcd', + address: 'pondok indah', + createdAt:new Date(), + updatedAt: new Date() + }, + { + nama: 'cfc', + address: 'veteran', + createdAt:new Date(), + updatedAt: new Date() + }, + { + nama: 'SS', + address: 'janturan', + createdAt:new Date(), + updatedAt: new Date() + }], {}); + }, + + down: (queryInterface, Sequelize) => { + /* + Add reverting commands here. + Return a promise to correctly handle asynchronicity. + + Example: + return queryInterface.bulkDelete('Person', null, {}); + */ + } +}; diff --git a/views/menu.ejs b/views/menu.ejs new file mode 100644 index 0000000..19c9c3b --- /dev/null +++ b/views/menu.ejs @@ -0,0 +1,66 @@ + + + + + + + + + + + + + + + + + + <% for(let i=0;i + + + + + + + + + + <%}%> + +
namamenu_typeratingpricerestaurant_Idaction
<%= data[i].nama%><%= data[i].menu_type%><%= data[i].rating%><%- helper.formatuang(data[i].price)%><%= data[i].restaurant.nama%> update delete
+

<%= error.err %>

+
+ + + + + + + + + + + + + + + + + + + + + + + + +
nama
menu_type
rating
price
restaurant + +
+
+ + diff --git a/views/menuupdateform.ejs b/views/menuupdateform.ejs new file mode 100644 index 0000000..8e24621 --- /dev/null +++ b/views/menuupdateform.ejs @@ -0,0 +1,47 @@ + + + + + + + +

<%= error.err %>

+
+ + + + + + + + + + + + + + + + + + + + + + + + +
nama
menu_type
rating
price
+ +
+
+ + diff --git a/views/restaurant.ejs b/views/restaurant.ejs new file mode 100644 index 0000000..2660ee9 --- /dev/null +++ b/views/restaurant.ejs @@ -0,0 +1,23 @@ + + + + + + + + + + + + + + <% for(let i=0;i + + + + + + <%}%> +
nameaddressaction
<%= data[i].nama%><%= data[i].address %> view menu
+ + diff --git a/views/restaurantview.ejs b/views/restaurantview.ejs new file mode 100644 index 0000000..d326bd0 --- /dev/null +++ b/views/restaurantview.ejs @@ -0,0 +1,17 @@ + + + + + + + + + + + + + + +
namamenu typeratingprice
+ +