mirror of
https://github.com/JonasunderscoreJones/jonas_jones-api.git
synced 2025-10-22 16:59:17 +02:00
40 lines
945 B
JavaScript
40 lines
945 B
JavaScript
'use strict';
|
|
const mongoose = require('mongoose'),
|
|
Book = mongoose.model('Books');exports.list_all_books = (req, res) => {
|
|
Book.find({}, (err, book) => {
|
|
if (err)
|
|
res.send(err);
|
|
res.json(book);
|
|
});
|
|
};
|
|
exports.create_a_book = (req, res) => {
|
|
let new_book = new Book(req.body);
|
|
new_book.save((err, book) => {
|
|
if (err)
|
|
res.send(err);
|
|
res.json(book);
|
|
});
|
|
};
|
|
exports.read_a_book = (req, res) => {
|
|
Book.findById(req.params.bookId, (err, book) => {
|
|
if (err)
|
|
res.send(err);
|
|
res.json(book);
|
|
});
|
|
};
|
|
exports.update_a_book = (req, res) => {
|
|
Book.findOneAndUpdate({_id: req.params.bookId}, req.body, {new: true}, (err, task) => {
|
|
if (err)
|
|
res.send(err);
|
|
res.json(book);
|
|
});
|
|
};
|
|
exports.delete_a_book = (req, res) => {
|
|
Book.remove({
|
|
_id: req.params.bookId
|
|
}, (err, book) => {
|
|
if (err)
|
|
res.send(err);
|
|
res.json({ message: 'Book successfully deleted' });
|
|
});
|
|
};
|