let mongoose = require('mongoose');
let Book = require('../models/book');
/*
* GET /book route to retrieve all the books.
*/
function getBooks(req, res) {
//Query the DB and if no errors, send all the books
let query = Book.find({});
query.exec((err, books) => {
if(err) res.send(err);
//If no errors, send them back to the client
res.json(books);
});
}
/*
* POST /book to save a new book.
*/
function postBook(req, res) {
//Creates a new book
var newBook = new Book(req.body);
//Save it into the DB.
newBook.save((err,book) => {
if(err) {
res.send(err);
}
else { //If no errors, send it back to the client
res.json({message: "Book successfully added!", book });
}
});
}
/*
* GET /book/:id route to retrieve a book given its id.
*/
function getBook(req, res) {
Book.findById(req.params.id, (err, book) => {
if(err) res.send(err);
//If no errors, send it back to the client
res.json(book);
});
}
/*
* DELETE /book/:id to delete a book given its id.
*/
function deleteBook(req, res) {
Book.remove({_id : req.params.id}, (err, result) => {
res.json({ message: "Book successfully deleted!", result });
});
}
/*
* PUT /book/:id to updatea a book given its id
*/
function updateBook(req, res) {
Book.findById({_id: req.params.id}, (err, book) => {
if(err) res.send(err);
Object.assign(book, req.body).save((err, book) => {
if(err) res.send(err);
res.json({ message: 'Book updated!', book });
});
});
}
//export all the functions
module.exports = { getBooks, postBook, getBook, deleteBook, updateBook };
Please Whitelist JSFiddle in your content blocker.
Help keep JSFiddle free for always by one of two ways:
Whitelist JSFiddle in your content blocker (two clicks)
Go PRO and get access to additional PRO features →
Join the 4+ million users, and keep the JSFiddle dream alive.
Ad-free
All ads in the editor and listing pages are turned completely off.
Use pre-released features
You get to try and use features (like the Palette Color Generator) months before everyone else.
Fiddle collections
Sort and categorize your Fiddles into multiple collections.
Private collections and fiddles
You can make as many Private Fiddles, and Private Collections as you wish!
Console
Debug your Fiddle with a minimal built-in JavaScript console.