From d5d754e0d03b9deb62c4ac014b2a141d415948cf Mon Sep 17 00:00:00 2001 From: ambodalle Date: Tue, 23 May 2017 14:41:28 +0700 Subject: [PATCH 1/2] mongo oke, minus update --- .gitignore | 1 + app.js | 23 +++++ controller/books_controller.js | 152 +++++++++++++++++++++++++++++++++ package.json | 24 ++++++ routes/books.js | 16 ++++ 5 files changed, 216 insertions(+) create mode 100644 .gitignore create mode 100644 app.js create mode 100644 controller/books_controller.js create mode 100644 package.json create mode 100644 routes/books.js diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..3c3629e --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +node_modules diff --git a/app.js b/app.js new file mode 100644 index 0000000..adea4d6 --- /dev/null +++ b/app.js @@ -0,0 +1,23 @@ +const express = require('express'); +const app = express(); + +var books = require('./routes/books') + +var MongoClient = require('mongodb').MongoClient + + +// Connection URL +var url = 'mongodb://localhost/mongodb-crud'; + +// Use connect method to connect to the server + + + + +app.use('/', books); + +app.listen(3000, ()=>{ + // console.log("aa"); +}) + +// module.exports = app; diff --git a/controller/books_controller.js b/controller/books_controller.js new file mode 100644 index 0000000..554c3c0 --- /dev/null +++ b/controller/books_controller.js @@ -0,0 +1,152 @@ +var MongoClient = require('mongodb').MongoClient +var url = 'mongodb://localhost/mongodb-crud'; +var ObjectID = require('mongodb').ObjectId + + +var findAllBooks = (req,res,next) =>{ + MongoClient.connect(url, (err, db)=>{ + if (err) { + res.send(err) + } else { + let bookstore = db.collection('collection_books') + bookstore.find().toArray((err, documents)=>{ + if (err) { + res.send(err) + } else { + res.send(documents) + db.close(); + } + }) + } + }) +} + + +var findOneBook = (req,res,next) =>{ + MongoClient.connect(url, (err, db)=>{ + if(err) { + res.send(err) + } else { + let bookstore = db.collection('collection_books') + bookstore.find({ id : req.params.id }).toArray((err, documents)=>{ + if(err) { + res.send(err) + } else { + res.send(documents) + db.close(); + } + }) + } + }) +} + + +var insertManyBook = (req,res,next) =>{ + MongoClient.connect(url, (err, db)=>{ + if (err) { + res.send(err) + } else { + let bookstore = db.collection('collection_books') + bookstore.insertMany([ + { + isbn : "978-1-60309-057-5", + title : "Dragon Puncher", + author : "James Kchalka", + category : "All Ages", + stok : 3 + }, { + isbn : "978-1-891830-77-8", + title : "Every Girls is the End of the World for me", + author : "Jeffrey Brown", + category : "Mature (16+)", + stok : 5 + } + ], (err, documents)=>{ + res.send(documents) + db.close(); + }) + + } + }) +} + +var DeleteBook = (req,res,next)=>{ + MongoClient.connect(url, (err, db)=>{ + if (err) { + res.send(err) + } else { + let bookstore = db.collection('collection_books') + bookstore.deleteOne({ id : req.params.id}, function(err, documents) { + res.send(documents) + db.close(); + }) + } + }) +} + +var updateBook = (req,res,next)=>{ + MongoClient.connect(url, (err, db)=>{ + console.log('MongoClient'); + if (err) { + console.log('if abis MongoClient'); + res.send(err) + } else { + console.log('Else MongoClient'); + let bookstore = db.collection('collection_books') + bookstore.updateOne({ + _id : ObjectID(req.params.id) + }, + { + $set : req.body + }, {new : true }, (err, documents)=>{ + console.log('abis update'); + if (err) { + console.log('abis if update'); + res.send(err) + } else { + res.send(documents) + db.close(); + } + }) + } + }) +} + +// var updateBook = (req,res,next)=>{ +// MongoClient.connect(url, (err, db)=>{ +// if (err) { +// res.send(err) +// } else { +// let bookstore = db.collection('collection_books') +// bookstore.updateOne({ _id : ObjectID(req.params.id) +// }, +// {$set : { +// isbn : req.body.isbn, +// title : req.body.title, +// author : req.body.author, +// category : req.body.category, +// stok : req.body.stok +// }}, {new : true },(err, documents)=>{ +// if (err) { +// res.send(err) +// } else { +// res.send(documents) +// db.close(); +// } +// }) +// } +// }) +// } + + + + + +module.exports = { + findAllBooks, + findOneBook, + insertManyBook, + DeleteBook, + updateBook + +} diff --git a/package.json b/package.json new file mode 100644 index 0000000..3192e4a --- /dev/null +++ b/package.json @@ -0,0 +1,24 @@ +{ + "name": "mongodb-crud", + "version": "1.0.0", + "description": "", + "main": "index.js", + "scripts": { + "start" : "nodemon app.js" + }, + "repository": { + "type": "git", + "url": "git+https://github.com/ambo-dalle/mongodb-crud.git" + }, + "author": "", + "license": "ISC", + "bugs": { + "url": "https://github.com/ambo-dalle/mongodb-crud/issues" + }, + "homepage": "https://github.com/ambo-dalle/mongodb-crud#readme", + "dependencies": { + "express": "^4.15.3", + "mongodb": "^2.2.27", + "nodemon": "^1.11.0" + } +} diff --git a/routes/books.js b/routes/books.js new file mode 100644 index 0000000..d7206bb --- /dev/null +++ b/routes/books.js @@ -0,0 +1,16 @@ +const express = require('express'); +var router = express.Router(); +var Books = require('../controller/books_controller'); + + +router.get('/', Books.findAllBooks); +router.get('/:id', Books.findOneBook); +router.post('/', Books.insertManyBook); +router.delete('/:id', Books.DeleteBook); +router.patch('/:id', Books.updateBook) + +// router.get('/', function(req,res,next) { +// res.send('inininin') +// }) + +module.exports = router; From ebcb146af79d68afca2fcfc20b87c0a8022897fc Mon Sep 17 00:00:00 2001 From: ambodalle Date: Tue, 23 May 2017 15:16:50 +0700 Subject: [PATCH 2/2] sudah done --- app.js | 5 +++-- controller/books_controller.js | 12 +++++++----- package.json | 3 ++- 3 files changed, 12 insertions(+), 8 deletions(-) diff --git a/app.js b/app.js index adea4d6..d5177f6 100644 --- a/app.js +++ b/app.js @@ -2,10 +2,12 @@ const express = require('express'); const app = express(); var books = require('./routes/books') +var bodyParser = require('body-parser'); var MongoClient = require('mongodb').MongoClient - +app.use(bodyParser.json()); +app.use(bodyParser.urlencoded({ extended: false })); // Connection URL var url = 'mongodb://localhost/mongodb-crud'; @@ -13,7 +15,6 @@ var url = 'mongodb://localhost/mongodb-crud'; - app.use('/', books); app.listen(3000, ()=>{ diff --git a/controller/books_controller.js b/controller/books_controller.js index 554c3c0..d759f3f 100644 --- a/controller/books_controller.js +++ b/controller/books_controller.js @@ -86,20 +86,22 @@ var DeleteBook = (req,res,next)=>{ var updateBook = (req,res,next)=>{ MongoClient.connect(url, (err, db)=>{ - console.log('MongoClient'); if (err) { - console.log('if abis MongoClient'); res.send(err) } else { - console.log('Else MongoClient'); let bookstore = db.collection('collection_books') bookstore.updateOne({ _id : ObjectID(req.params.id) }, { - $set : req.body + $set : { + isbn : req.body.isbn, + title : req.body.title, + author : req.body.author, + category : req.body.category, + stok : req.body.stok + } }, {new : true }, (err, documents)=>{ - console.log('abis update'); if (err) { console.log('abis if update'); res.send(err) diff --git a/package.json b/package.json index 3192e4a..2471795 100644 --- a/package.json +++ b/package.json @@ -4,7 +4,7 @@ "description": "", "main": "index.js", "scripts": { - "start" : "nodemon app.js" + "start": "nodemon app.js" }, "repository": { "type": "git", @@ -19,6 +19,7 @@ "dependencies": { "express": "^4.15.3", "mongodb": "^2.2.27", + "mongoose": "^4.10.2", "nodemon": "^1.11.0" } }