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

Sudah selesai #8

Open
wants to merge 2 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
24 changes: 24 additions & 0 deletions app.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
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';

// Use connect method to connect to the server



app.use('/', books);

app.listen(3000, ()=>{
// console.log("aa");
})

// module.exports = app;
154 changes: 154 additions & 0 deletions controller/books_controller.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,154 @@
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)=>{
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) {
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

}
25 changes: 25 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
{
"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",
"mongoose": "^4.10.2",
"nodemon": "^1.11.0"
}
}
16 changes: 16 additions & 0 deletions routes/books.js
Original file line number Diff line number Diff line change
@@ -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;