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