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

fase2.push(davidyunius) #21

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
20 changes: 20 additions & 0 deletions app.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
'use strict'
const express = require('express')
const app = express()
const bodyParser = require('body-parser')
app.set('views', './views')
app.set('view engine', 'ejs')
app.use(bodyParser.urlencoded({ extended: false }));

const index = require('./routes/index')
const menu = require('./routes/menu')
const restaurant = require('./routes/restaurant')

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


app.listen(3000, () => {
console.log('App is now listening on port 3000');
})
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": "davidyunius",
"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"
}
}
16 changes: 16 additions & 0 deletions helpers/currencyHelpers.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
30 changes: 30 additions & 0 deletions migrations/20180301021339-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');
}
};
39 changes: 39 additions & 0 deletions migrations/20180301021641-create-menu.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
'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
},
restaurantId: {
type: Sequelize.INTEGER
},
createdAt: {
allowNull: false,
type: Sequelize.DATE
},
updatedAt: {
allowNull: false,
type: Sequelize.DATE
}
});
},
down: (queryInterface, Sequelize) => {
return queryInterface.dropTable('Menus');
}
};
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;
42 changes: 42 additions & 0 deletions models/menu.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
'use strict';
const Op = require('sequelize').Op
module.exports = (sequelize, DataTypes) => {
var Menu = sequelize.define('Menu', {
name: DataTypes.STRING,
menu_type: {
type: DataTypes.STRING,
validate: {
isCorrectFormat: (value) => {
let check = /(^food$|^drink$)/
if (value.search(check) === -1) {
throw new Error(`Validation error: Isi menu type dengan food / drink`)
}
},
checkVariant: (value, next) => {
Menu.findAll({
where: {
menu_type: value
}
}).then(value => {
if (value === 'food' && value > 5) {
next(`Variant Food sudah maksimal!`)
}else {
next()
}
}).catch(err => {
next(err)
})
}
}
},
rating: DataTypes.INTEGER,
price: DataTypes.INTEGER,
restaurantId: DataTypes.INTEGER
});
Menu.associate = function (models) {
Menu.belongsTo(models.Restaurant, {
foreignKey: 'restaurantId'
})
}
return Menu;
};
13 changes: 13 additions & 0 deletions models/restaurant.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
'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, {
foreignKey: 'restaurantId'
})
}
return Restaurant;
};
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,16 @@
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
"test": "echo \"Error: no test specified\" && exit 1",
"start": "nodemon app.js"
},
"author": "Windiana",
"license": "ISC",
"dependencies": {
"body-parser": "^1.18.2",
"ejs": "^2.5.7",
"express": "^4.16.2",
"nodemon": "^1.17.1",
"pg": "^7.4.1",
"sequelize": "^4.34.1"
}
Expand Down
9 changes: 9 additions & 0 deletions routes/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
'use strict'
const express = require('express')
const router = express.Router()

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

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

const Models = require('../models')
const currencyHelpers = require('../helpers/currencyHelpers')


router.get('/', (req, res, next) => {
Models.Menu.findAll({
include: [{model: Models.Restaurant, attributes: ['name']}]
}).then(data => {
Models.Restaurant.findAll().then(dataRestaurant => {
res.render('./menu-view/menu', { data, dataRestaurant, currencyHelpers: currencyHelpers, err: req.query.err });
})
})
})

router.post('/add', (req, res, next) => {
Models.Menu.create(req.body).then(data => {
res.redirect('/menus')
}).catch(err => {
if (!err.errors[0]) {
err.errors[0].message = null
}
res.redirect(`/menus?err=${err.errors[0].message}`)
})
})

router.get('/:id/edit', (req, res) => {
Models.Menu.findById(req.params.id).then(data => {
Models.Restaurant.findAll().then(dataRestaurant => {
res.render('./menu-view/edit-menu', { data, dataRestaurant })
})
})
})

router.post('/:id/edit', (req, res) => {
Models.Menu.update({
name: req.body.name,
menu_type: req.body.menu_type,
rating: req.body.rating,
price: req.body.price,
restaurantId: req.body.restaurantId,
updatedAt: new Date()
}, {where: {id:req.params.id}}).then(data => {
res.redirect('/menus')
})
})

router.get('/:id/delete', (req, res) => {
Models.Menu.destroy({where: {id:req.params.id}}).then(data => {
res.redirect('/menus')
})
})

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

const Models = require('../models')
const currencyHelpers = require('../helpers/currencyHelpers')

router.get('/', (req, res, next) => {
Models.Restaurant.findAll({
include: [{ model: Models.Menu }]
}).then(data => {
Models.Menu.findAll().then(dataMenu => {
res.render('./restaurant-view/restaurant', { data, dataMenu, currencyHelpers: currencyHelpers });
})
})

router.get('/:id/view_menu', (req, res) => {
Models.Restaurant.findById(req.params.id).then(data => {
Models.Menu.findAll({
include: [{model: Models.Menu, attributes: ['id', 'name', 'menu_types', 'rating', 'price', 'restaurantId']}]
}).then(dataMenu => {
res.render('./restaurant-view/view-menu', {data, dataMenu, currencyHelpers: currencyHelpers})
})
})
})

})

module.exports = router
26 changes: 26 additions & 0 deletions seeders/20180301021744-demo-restaurant.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
'use strict';

module.exports = {
up: (queryInterface, Sequelize) => {
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) => {
return queryInterface.bulkDelete('Restaurants', null, {});
}
};
Loading