1
|
|
|
'use strict'; |
2
|
|
|
|
3
|
|
|
const Author = require('../../models/author'); |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* GET /api/authors route to retreive all authors |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
function getAuthors(req, res) { |
10
|
|
|
// Query the DB and if no errors, send all authors |
11
|
|
|
let query = Author.find({}); |
12
|
|
|
query.exec((err, authors) => { |
13
|
|
|
if (err) res.send(err); |
|
|
|
|
14
|
|
|
//If no errors, send them back to the clients |
15
|
|
|
res.json(authors); |
16
|
|
|
}); |
17
|
|
|
} |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* POST /api/authors to save a new Author |
21
|
|
|
*/ |
22
|
|
|
|
23
|
|
|
function postAuthor(req, res) { |
24
|
|
|
// Create a new Author |
25
|
|
|
const newAuthor = new Author(req.body); |
26
|
|
|
//Save it into the DB |
27
|
|
|
newAuthor.save() |
28
|
|
|
.then((author) => { |
29
|
|
|
return res.status(201).send({ |
30
|
|
|
message: 'Author successfully added!', |
31
|
|
|
author |
32
|
|
|
}); |
33
|
|
|
}) |
34
|
|
|
.catch((err) => { |
35
|
|
|
return res.status(400).send(err); |
36
|
|
|
}); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* GET /api/authors/:id to retrieve an author given its ID |
41
|
|
|
*/ |
42
|
|
|
|
43
|
|
|
function getAuthor(req, res) { |
44
|
|
|
Author.findById(req.params.id) |
45
|
|
|
.then((author) => { |
46
|
|
|
if (author) { |
47
|
|
|
return res.json(author); |
48
|
|
|
} else { |
49
|
|
|
res.status(404).send({ |
50
|
|
|
message: 'Author not found!', |
51
|
|
|
id: req.params.id |
52
|
|
|
}); |
|
|
|
|
53
|
|
|
} |
54
|
|
|
}) |
55
|
|
|
.catch((err) => { |
56
|
|
|
return res.status(400).send(err); |
57
|
|
|
}); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* Delete /api/authors/:id to delete an author given its ID |
62
|
|
|
*/ |
63
|
|
|
function deleteAuthor(req, res) { |
64
|
|
|
const query = { |
65
|
|
|
_id: req.params.id |
66
|
|
|
}; |
67
|
|
|
Author.remove(query) |
68
|
|
|
.then((result) => { |
69
|
|
|
if (result.result.n === 0) { |
70
|
|
|
return res.status(404).send({ |
71
|
|
|
message: 'Author not found!', |
72
|
|
|
id: req.params.id |
73
|
|
|
}); |
74
|
|
|
} else { |
75
|
|
|
return res.json({ |
76
|
|
|
message: 'Author successfully deleted!', |
77
|
|
|
result |
78
|
|
|
}); |
79
|
|
|
} |
80
|
|
|
}) |
81
|
|
|
.catch((err) => { |
82
|
|
|
return res.status(400).send(err); |
83
|
|
|
}); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* PUT /api/authors/:id to update an author given its ID |
88
|
|
|
*/ |
89
|
|
|
|
90
|
|
|
function updateAuthor(req, res) { |
91
|
|
|
Author.findById(req.params.id) |
92
|
|
|
.then((author) => { |
93
|
|
|
if (!author) { |
94
|
|
|
return res.status(404).send({ |
95
|
|
|
message: 'Author not found!', |
96
|
|
|
id: req.params.id |
97
|
|
|
}); |
98
|
|
|
} else { |
99
|
|
|
Object.assign(author, req.body) |
100
|
|
|
.save() |
101
|
|
|
.then((author) => { |
102
|
|
|
return res.json({ |
103
|
|
|
message: 'Author updated!', |
104
|
|
|
author |
105
|
|
|
}); |
106
|
|
|
}) |
107
|
|
|
.catch((err) => { |
108
|
|
|
return res.status(500).send(err); |
109
|
|
|
}); |
|
|
|
|
110
|
|
|
} |
111
|
|
|
}) |
112
|
|
|
.catch((err) => { |
113
|
|
|
return res.status(400).send(err); |
114
|
|
|
}); |
115
|
|
|
} |
116
|
|
|
// Export all the functions |
117
|
|
|
module.exports = { |
118
|
|
|
getAuthors, |
119
|
|
|
postAuthor, |
120
|
|
|
getAuthor, |
121
|
|
|
deleteAuthor, |
122
|
|
|
updateAuthor |
123
|
|
|
}; |
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.