This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
CREATE DATABASE IF NOT EXISTS `simple-rest`; | |
USE `simple-rest`; | |
DROP TABLE IF EXISTS `book`; | |
CREATE TABLE `book` ( | |
`id` int(11) NOT NULL AUTO_INCREMENT, | |
`isbn` varchar(50) NOT NULL, | |
`name` varchar(255) NOT NULL, | |
`year` varchar(4) NOT NULL, | |
`author` varchar(255) NOT NULL, |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
CREATE DATABASE IF NOT EXISTS `simple-rest`; | |
USE `simple-rest`; | |
DROP TABLE IF EXISTS `book`; | |
CREATE TABLE `book` ( | |
`id` int(11) NOT NULL AUTO_INCREMENT, | |
`isbn` varchar(50) NOT NULL, | |
`name` varchar(255) NOT NULL, | |
`year` varchar(4) NOT NULL, | |
`author` varchar(255) NOT NULL, |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const express = require('express'); | |
const app = express(); | |
app.get('/', (req, res) => { | |
res.send('Hello World'); | |
}) | |
app.listen(3000, () => console.log("server berjalan pada http://localhost:3000")) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const Sequelize = require('sequelize'); | |
const sequelize = new Sequelize('bookstore', 'root', 'passsword', { | |
host: 'localhost', | |
dialect: 'mysql', | |
pool: { | |
max: 5, | |
min: 0, | |
idle: 10000 | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const book = sequelize.define('book', { | |
'id': { | |
type: Sequelize.INTEGER, | |
primaryKey: true, | |
autoIncrement: true | |
}, | |
'isbn': Sequelize.STRING, | |
'name': Sequelize.STRING, | |
'year': Sequelize.STRING, | |
'author': Sequelize.STRING, |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
app.get('/book/', (req, res) => { | |
book.findAll().then(book => { | |
res.json(book) | |
}) | |
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const bodyParser = require('body-parser'); //post body handler | |
const { check, validationResult } = require('express-validator/check'); //form validation | |
const { matchedData, sanitize } = require('express-validator/filter'); //sanitize form params | |
const multer = require('multer'); //multipar form-data | |
const path = require('path'); | |
const crypto = require('crypto'); | |
//Set body parser for HTTP post operation | |
app.use(bodyParser.json()); // support json encoded bodies | |
app.use(bodyParser.urlencoded({ extended: true })); // support encoded bodies |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
app.post('/book/', [ | |
//File upload (karena pakai multer, tempatkan di posisi pertama agar membaca multipar form-data) | |
upload.single('image'), | |
//Set form validation rule | |
check('isbn') | |
.isLength({ min: 5 }) | |
.isNumeric() | |
.custom(value => { | |
return book.findOne({where: {isbn: value}}).then(b => { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
app.put('/book/', [ | |
//File upload (karena pakai multer, tempatkan di posisi pertama agar membaca multipar form-data) | |
upload.single('image'), | |
//Set form validation rule | |
check('isbn') | |
.isLength({ min: 5 }) | |
.isNumeric() | |
.custom(value => { | |
return book.findOne({where: {isbn: value}}).then(b => { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
app.delete('/book/:isbn',[ | |
//Set form validation rule | |
check('isbn') | |
.isLength({ min: 5 }) | |
.isNumeric() | |
.custom(value => { | |
return book.findOne({where: {isbn: value}}).then(b => { | |
if(!b){ | |
throw new Error('ISBN not found'); | |
} |
OlderNewer