Skip to content

Instantly share code, notes, and snippets.

View budasuyasa's full-sized avatar
🏠
Working from home

Buda Suyasa budasuyasa

🏠
Working from home
  • PT Hooki Global Kreasi
  • Indonesia
View GitHub Profile
@budasuyasa
budasuyasa / db.sql
Last active April 26, 2018 09:16
simple-rest-medium-hello-world
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,
@budasuyasa
budasuyasa / simple-rest.sql
Created April 26, 2018 09:11
simple-rest-medium-create-db
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,
@budasuyasa
budasuyasa / index.js
Created May 6, 2018 01:46
Hello World
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"))
@budasuyasa
budasuyasa / index.js
Last active May 6, 2018 02:14
medium-post-simple-rest-1
const Sequelize = require('sequelize');
const sequelize = new Sequelize('bookstore', 'root', 'passsword', {
host: 'localhost',
dialect: 'mysql',
pool: {
max: 5,
min: 0,
idle: 10000
}
@budasuyasa
budasuyasa / index.js
Created May 6, 2018 02:29
medium-post-simple-rest-02
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,
@budasuyasa
budasuyasa / index.js
Created May 6, 2018 02:42
medium-post-simple-rest-03
app.get('/book/', (req, res) => {
book.findAll().then(book => {
res.json(book)
})
})
@budasuyasa
budasuyasa / index.js
Created May 6, 2018 03:51
medium-post-simple-rest-04
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
@budasuyasa
budasuyasa / index.js
Created May 6, 2018 04:01
medium-post-simple-rest-05
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 => {
@budasuyasa
budasuyasa / index.js
Created May 6, 2018 04:32
medium-post-simple-rest-06
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 => {
@budasuyasa
budasuyasa / index.js
Created May 6, 2018 04:52
medium-post-simple-rest-07
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');
}