1
|
|
|
'use strict'; |
2
|
|
|
|
3
|
|
|
const Book = require('../../models/book'); |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* GET /book route to retrieve all the books |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
function getBooks(req, res) { |
10
|
|
|
// Query the DB and if no errors, send all the books |
11
|
|
|
let query = Book.find({}); |
12
|
|
|
query.exec((err, books) => { |
13
|
|
|
if (err) res.send(err); |
|
|
|
|
14
|
|
|
//If no errors, send them back to the clients |
15
|
|
|
res.json(books); |
16
|
|
|
}); |
17
|
|
|
} |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* POST /book to save a new book |
21
|
|
|
*/ |
22
|
|
|
|
23
|
|
|
function postBook(req, res) { |
24
|
|
|
// Create a new book |
25
|
|
|
const newBook = new Book(req.body); |
26
|
|
|
// Save it into the DB |
27
|
|
|
newBook.save((err, book) => { |
28
|
|
|
if (err) { |
29
|
|
|
res.send(err); |
30
|
|
|
} else { |
31
|
|
|
// If no erros, send it back to the clients |
32
|
|
|
res.send({ |
33
|
|
|
message: 'Book successfully added!', |
34
|
|
|
book |
35
|
|
|
}); |
36
|
|
|
} |
37
|
|
|
}); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* GET /book/:id route to retrieve a book given its id. |
42
|
|
|
*/ |
43
|
|
|
function getBook(req, res) { |
44
|
|
|
Book.findById(req.params.id, (err, book) => { |
45
|
|
|
if (err) { |
46
|
|
|
return res.status(400).send(err); |
47
|
|
|
} |
48
|
|
|
if (book) { |
49
|
|
|
return res.send(book); |
50
|
|
|
} else { |
51
|
|
|
return res.status(404).send({ |
52
|
|
|
message: 'Book not found!', |
53
|
|
|
id: req.params.id |
54
|
|
|
}); |
55
|
|
|
} |
56
|
|
|
}); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* DELETE /book/:id to delete a book given its id. |
61
|
|
|
*/ |
62
|
|
|
function deleteBook(req, res) { |
63
|
|
|
Book.remove({ |
64
|
|
|
_id: req.params.id |
65
|
|
|
}, (err, result) => { |
66
|
|
|
if (err) { |
67
|
|
|
return res.status(400).send(err); |
68
|
|
|
} |
69
|
|
|
if (result.result.n === 0) { |
70
|
|
|
return res.status(404).send({ |
71
|
|
|
message: 'Book not found!', |
72
|
|
|
id: req.params.id |
73
|
|
|
}); |
74
|
|
|
} else { |
75
|
|
|
return res.json({ |
76
|
|
|
message: 'Book successfully deleted!', |
77
|
|
|
result |
78
|
|
|
}); |
79
|
|
|
} |
80
|
|
|
}); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* PUT /book/:id to update a book given its id |
85
|
|
|
*/ |
86
|
|
|
|
87
|
|
|
function updateBook(req, res) { |
88
|
|
|
Book.findById({ |
89
|
|
|
_id: req.params.id |
90
|
|
|
}, (err, book) => { |
91
|
|
|
if (err) { |
92
|
|
|
return res.status(400).send(err); |
93
|
|
|
} |
94
|
|
|
if (!book) { |
95
|
|
|
return res.status(404).send({ |
96
|
|
|
message: 'Book not found!', |
97
|
|
|
id: req.params.id |
98
|
|
|
}); |
99
|
|
|
} else { |
100
|
|
|
Object.assign(book, req.body).save((err, book) => { |
101
|
|
|
if (err) { |
102
|
|
|
return res.status(500).send(err); |
103
|
|
|
} |
104
|
|
|
return res.json({ |
105
|
|
|
message: 'Book updated!', |
106
|
|
|
book |
107
|
|
|
}); |
108
|
|
|
}); |
|
|
|
|
109
|
|
|
} |
110
|
|
|
}); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
// Export all the functions |
114
|
|
|
|
115
|
|
|
module.exports = { |
116
|
|
|
getBooks, |
117
|
|
|
postBook, |
118
|
|
|
getBook, |
119
|
|
|
deleteBook, |
120
|
|
|
updateBook |
121
|
|
|
}; |
Consider adding curly braces around all statements when they are executed conditionally. This is optional if there is only one statement, but leaving them out can lead to unexpected behaviour if another statement is added later.
Consider:
If you or someone else later decides to put another statement in, only the first statement will be executed.
In this case the statement
b = 42
will always be executed, while the logging statement will be executed conditionally.ensures that the proper code will be executed conditionally no matter how many statements are added or removed.