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

Wika silo #9

Open
wants to merge 3 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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
node_modules
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
24 changes: 24 additions & 0 deletions app.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
'use strict'

const express = require('express');
const bodyParser = require('body-parser');

const app = express();
const PORT = 3000;

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

//SET Body Parser
app.use(bodyParser.urlencoded({ extended: false }))

//SET Helper
app.locals.helpers = require('./helpers')

//MIDDLEWERE ke routes
app.use('/', require('./routes'));

app.listen(PORT, () => {
console.log(`Aplikasi live-coding4 ini berjalan di 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": "postgres",
"password": null,
"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"
}
}
10 changes: 10 additions & 0 deletions helpers/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
'use strict'

function format_currency(num) {
num = num.toLocaleString();
return `Rp. ${num}`
}

module.exports = {
format_currency: format_currency
}
30 changes: 30 additions & 0 deletions migrations/20180301021347-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/20180301021517-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');
}
};
25 changes: 25 additions & 0 deletions migrations/20180301031026-menu-add-column-restaurantId.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', 'RestaurantId', { type: Sequelize.INTEGER });
},

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

Example:
return queryInterface.dropTable('users');
*/
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;
22 changes: 22 additions & 0 deletions models/menu.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
'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
}, {});
Menu.associate = function(models) {
// associations can be defined here
Menu.belongsTo(models.Restaurant);
};
return Menu;
};
12 changes: 12 additions & 0 deletions models/restaurant.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
'use strict';
module.exports = (sequelize, DataTypes) => {
var Restaurant = sequelize.define('Restaurant', {
name: DataTypes.STRING,
address: DataTypes.STRING
}, {});
Restaurant.associate = function(models) {
// associations can be defined here
Restaurant.hasMany(models.Menu);
};
return Restaurant;
};
14 changes: 14 additions & 0 deletions routes/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
'use strict'
const router = require('express').Router();

router.get('/', (req, res) => {
res.render('./index.ejs')
})

router.use('/menus', require('./menu.js'))
router.use('/restaurant', require('./restaurant.js'))




module.exports = router;
91 changes: 91 additions & 0 deletions routes/menu.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
'use strict'
const router = require('express').Router();
const Models = require('../models')

router.get('/', (req,res) => {
Models.Menu.findAll({
include: [{model: Models.Restaurant}]
})
.then(menus => {
Models.Restaurant.findAll()
.then(restaurant => {
res.render('../views/menu/listMenu.ejs', {menus: menus, restaurant:restaurant, err:null})
// res.send(menus)
})
.catch(err => {
res.redirect('../views/menu/listMenu.ejs', {menus:menus, restaurant:restaurant, err: err.message});
})
})
.catch(err => {
res.send(err)
})
})

router.get('/:id/edit', (req, res) => {
Models.Menu.findById(req.params.id)
.then((menu) => {
Models.Restaurant.findAll()
.then(restaurant => {
res.render('./menu/editMenu.ejs', {menu: menu, restaurant:restaurant})
})
.catch(err => {
res.send(err)
})
})
.catch((err) => {
res.send(err);
})
})

router.post('/:id/edit', (req,res) => {
let obj = {
name: req.body.name,
menu_type: req.body.menu_type,
rating: req.body.rating,
price: req.body.price,
RestaurantId: req.body.RestaurantId,
}
Models.Menu.update(
obj,
{where: {id: req.params.id}}
)
.then((Menus) => {
res.redirect('/menus')
})
.catch((err) => {
res.send(err);
})
})

router.get('/:id/delete', (req, res) => {
Models.Menu.destroy(
{where: {id: req.params.id}}
)
.then((items) => {
res.redirect('/menus')
})
.catch((err) => {
res.send(err);
})
// res.send('form input delete');
})

router.post('/', (req,res) => {
let obj = {
name: req.body.name,
menu_type: req.body.menu_type,
rating: req.body.rating,
price: req.body.price,
RestaurantId: req.body.RestaurantId,
}
Models.Menu.create(obj)
.then((menu) => {
res.redirect('/menus')
})
.catch((err) => {
res.render('../views/menu/listMenu.ejs', {err: err.message});
})
// res.send(req.body);
})

module.exports = router
8 changes: 8 additions & 0 deletions routes/restaurant.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
'use strict'
const router = require('express').Router();

router.get('/', (req,res) => {
res.send('On developed restaurant')
})

module.exports = router
43 changes: 43 additions & 0 deletions seeders/20180301021825-restaurant-add-table.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
'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', [{
name: 'Hokben',
address: 'Pondok Indah Mall 1',
createdAt: new Date(),
updatedAt: new Date(),
},{
name: 'KFC',
address: 'Pondok Indah Mall 2',
createdAt: new Date(),
updatedAt: new Date(),
},{
name: 'Yoshinoya',
address: 'Gandaria City',
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, {});
*/
return queryInterface.bulkDelete('Restaurants', null, {});
}
};
Loading