-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbook-controller.js
54 lines (48 loc) · 1.23 KB
/
book-controller.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
var Book = require('./models/bookModel');
//Creating book
exports.createBook = function (req, res) {
var newbook = new Book(req.body);
newbook.save(function (err, book) {
if (err) {
res.status(400).json(err);
}
res.json(book);
});
};
//Getting all books
exports.getBooks = function (req, res) {
Book.find({}, function (err, books) {
if (err) {
res.status(400).json(err);
}
res.send(books);
});
};
//Getting one book
exports.getBook = function (req, res) {
Book.findOne({ title: req.params.book }, function (err, book) {
if (err) {
res.status(400).json(err);
}
res.json(book);
});
};
//Updating a book
exports.updateBook = function(req, res) {
Book.findOneAndUpdate({title: req.params.title }, req.body, {new: true}, function (err, books) {
if (err) {
res.status(400).json(err);
}
res.json(books);
});
};
//Deleting a book
exports.deleteBook = function (req, res) {
console.log(req.params);
Book.findOneAndRemove({ title: req.params.title }, function (err, books) {
if (err) {
res.status(400).json(err);
}
res.json(books);
});
};