We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
2 parents ef0d7f3 + 3161ccb commit a36234aCopy full SHA for a36234a
FinalProjectReact-Node-Back
controllers/books/add.js
@@ -1,15 +1,15 @@
1
const {Book} = require("../../models/book");
2
-
3
-// const add = async (req, res) => {
4
-// const result = await Book.create(req.body);
5
-// res.status(201).json(result);
6
-// }
+const { RequestError } = require("../../helpers");
7
8
const add = async(req, res) => {
9
const {_id: owner} = req.user;
+ const {author, title} = req.body;
+ const book = await Book.findOne({author, title});
+ if (book) {
+ throw RequestError(409, "This book already exsist in your library")
10
+ }
11
const result = await Book.create({...req.body, owner});
12
res.status(201).json(result);
13
}
14
15
module.exports = add;
0 commit comments