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

Fatimahazzhr #13

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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
node_modules
27 changes: 27 additions & 0 deletions app.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
const express = require('express')
const app = express()
const PORT = 3000

const bodyParser = require('body-parser')

const index = require('./routes/index')
const menus = require('./routes/menus')
const restaurants = require('./routes/restaurant')

app.locals.helper = require('./helpers/formatuang.js')
app.use(bodyParser.urlencoded({extended: false}))
app.set('view engine', 'ejs')

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

//Testing
// app.get('/', (req, res) => {
// res.status(200).json({message: 'Connected!'})
// })

// Server
app.listen(PORT, () => {
console.log(`connected to 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": "020320",
"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"
}
}
8 changes: 8 additions & 0 deletions helpers/formatuang.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
function formatUang(money) {
return `Rp. ${money.toLocaleString()}`
}

// console.log(formatUang(45000))
module.exports = {
formatUang: formatUang
}
30 changes: 30 additions & 0 deletions migrations/20180301021338-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/20180301021436-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/20180301023116-addColRestaurantId.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
'use strict';

module.exports = {
up: (queryInterface, Sequelize) => {
return queryInterface.addColumn('Menus', 'RestaurantId', Sequelize.INTEGER)
/*
Add altering commands here.
Return a promise to correctly handle asynchronicity.

Example:
return queryInterface.createTable('users', { id: Sequelize.INTEGER });
*/
},

down: (queryInterface, Sequelize) => {
return queryInterface.removeColumn('Menus', 'RestaurantId')
/*
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;
55 changes: 55 additions & 0 deletions models/menu.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
'use strict';
module.exports = (sequelize, DataTypes) => {
var Menu = sequelize.define('Menu', {
name: DataTypes.STRING,
menu_type: {
type: DataTypes.STRING,
allowNull: true,
validate: {
is: {
args: /(food|drink)/,
msg: 'Isi menu type dengan food / drink'
},
// isFoodOrDrink(value) {
// console.log(value)
// if(value !== 'food' || value !== 'drink') {
// throw new Error('Isi menu type dengan food / drink')
// }
// }
}
},
rating: DataTypes.INTEGER,
price: DataTypes.INTEGER,
RestaurantId: DataTypes.INTEGER
}, {
hooks: {
beforeCreate: (instance) => {
if(instance.dataValues.menu_type == 'food') {
if(!Number(instance.dataValues.price) || instance.dataValues.price == '') {
instance.dataValues.price = 15000
}
} else if(instance.dataValues.menu_type == 'drink') {
if(!Number(instance.dataValues.price) || instance.dataValues.price == '') {
instance.dataValues.price = 10000
}
}
},
beforeBulkUpdate: (instance) => {
if(instance.attributes.menu_type == 'food') {
if(!Number(instance.attributes.price) || instance.attributes.price == '') {
instance.attributes.price = 15000
}
} else if(instance.attributes.menu_type == 'drink') {
if(!Number(instance.attributes.price) || instance.attributes.price == '') {
instance.attributes.price = 10000
}
}
}
}
});
Menu.associate = function(models) {
Menu.belongsTo(models.Restaurant)
// associations can be defined here
};
return Menu;
};
15 changes: 15 additions & 0 deletions models/restaurant.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
'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)
};
return Restaurant;

Restaurant.prototype.getRestaurantName = function() {
return `${this.name}`
}
};
7 changes: 7 additions & 0 deletions routes/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
const routes = require('express').Router()

routes.get('/', (req, res) => {
res.status(200).json({message: 'Connected!'})
})

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

routes.get('/', (req, res) => {
console.log(req.query)
// res.status(200).json({message: 'Connected!'})
let err = {
msg: req.query.err
}
Models.Menu.findAll({
include: [{
model: Models.Restaurant
}],
order: [
['id', 'ASC']
]
}).then((menus) => {
Models.Restaurant.findAll()
.then((restaurants) => {
// res.send(menus)
res.render('menus.ejs', {restaurants: restaurants, menus: menus, err: err})
}).catch(err => {
console.log(err)
})
}).catch(err => {
console.log(err)
})
})

routes.post('/', (req, res) => {
// res.send(req.body)
let addMenu = {
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(addMenu).then(() => {
res.redirect('/menus')
}).catch(err => {
res.redirect(`/menus?err=${err.message}`)
console.log(err)
})
})

routes.get('/:id/edit', (req, res) => {
console.log(req.query)
let err = {
msg: req.query.err
}
Models.Menu.findById(req.params.id)
.then(menu => {
Models.Restaurant.findAll()
.then((restaurants) => {
// res.send(restaurants)
// res.send(menu)
res.render('menu-edit.ejs', {restaurants: restaurants, menu: menu, err: err})
}).catch(err => {
console.log(err)
})
})
.catch(err => {
console.log(err)
})
})

routes.post('/:id/edit', (req, res) => {
// res.send(req.body)
let editMenu = {
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(editMenu, {
where: {
id: req.params.id
}
}).then(() => {
res.redirect('/menus')
}).catch(err => {
res.redirect(`/menus/${req.params.id}/edit?err=${err.message}&name=${editMenu.name}&menu_type=${editMenu.menu_type}&rating=${editMenu.rating}&price=${editMenu.price}&RestaurantId=${editMenu.RestaurantId}`)
// console.log(err)
})
})

routes.get('/:id/delete', (req, res) => {
Models.Menu.findById(req.params.id)
.then(menu => {
menu.destroy()
.then(() => {
res.redirect('/menus')
}).catch(err => {
console.log(err)
})
}).catch(err => {
console.log(err)
})
})

module.exports = routes;
Loading