Skip to content

Instantly share code, notes, and snippets.

@amandeepmittal
Created October 9, 2018 08:40
Show Gist options
  • Save amandeepmittal/fd73f97b37d2c49c750b4ee33100ed2e to your computer and use it in GitHub Desktop.
Save amandeepmittal/fd73f97b37d2c49c750b4ee33100ed2e to your computer and use it in GitHub Desktop.
// booksControllers.js
const Book = require('../models/Books');
// Defining all methods and business logic for routes
module.exports = {
findAll: function(req, res) {
Book.find(req.query)
.then(books => res.json(books))
.catch(err => res.status(422).json(err));
},
findById: function(req, res) {
Book.findById(req.params.id)
.then(book => res.json(book))
.catch(err => res.status(422).json(err));
},
create: function(req, res) {
Book.create(req.body)
.then(newBook => res.json(newBook))
.catch(err => res.status(422).json(err));
},
update: function(req, res) {
Book.findOneAndUpdate({ _id: req.params.id }, req.body)
.then(book => res.json(book))
.catch(err => res.status(422).json(err));
},
remove: function(req, res) {
Book.findById({ _id: req.params.id })
.then(book => book.remove())
.then(allbooks => res.json(allbooks))
.catch(err => res.status(422).json(err));
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment