From 807b98dfd653af0c1e7bcc0c7d2252f6570804ef Mon Sep 17 00:00:00 2001 From: AdithyakrishnaV Date: Fri, 2 Jul 2021 08:21:53 +0530 Subject: [PATCH] add operator --- index.js | 74 +++++++++++++++++++++++++++++++++----------------------- 1 file changed, 44 insertions(+), 30 deletions(-) diff --git a/index.js b/index.js index c25323e..25c343e 100644 --- a/index.js +++ b/index.js @@ -358,52 +358,66 @@ Access Public Parameter isbn Method DELETE */ -booky.delete("/book/delete/:isbn", (req, res) => { - - const updatedBookDatabase = database.books.filter( - (book) => book.ISBN !== req.params.isbn - ); +booky.delete("/book/delete/:isbn", async (req, res) => { - database.books = updatedBookDatabase; - return res.json({ books: database.books }); + const updatedBookDatabase = await BookModel.findOneAndDelete( + { + ISBN: req.params.isbn + } + ); + + // const updatedBookDatabase = database.books.filter( + // (book) => book.ISBN !== req.params.isbn + // ); + // database.books = updatedBookDatabase; + return res.json({ books: updatedBookDatabase }); }); /* Route /book/delete/author -Discription Delete a book from publication +Discription Delete a author from book Access Public Parameter isbn, author id Method DELETE */ -booky.delete("/book/delete/author/:isbn/:authorId", (req, res) => { - //update book database - database.books.forEach((book) => { - if(book.ISBN === req.params.isbn) { - const newAuthorList = book.authors.filter( - (author) => author !== parseInt(req.params.authorId) - );//convert string to no +booky.delete("/book/delete/author/:isbn/:authorId", async (req, res) => { - book.authors = newAuthorList; - return; - } - }); + //update book database + + const updatedBook = await BookModel.findOneAndUpdate( + { + ISBN: req.params.isbn + }, + { + $pull:{ + authors: parseInt(req.params.authorId) + } + }, + { + new: true + } + ); //update author database - database.author.forEach((author) => { - if(author.id === parseInt(req.params.authorId)) { - const newBooksList = author.books.filter( - (book) => book !== req.params.isbn - ); - - author.books = newBooksList; - return; + + const updatedAuthor = await AuthorModel.findOneAndUpdate( + { + id: parseInt(req.params.authorId) + }, + { + $pull: { + books: req.params.isbn + } + }, + { + new: true } - }); + ); return res.json({ message: "author was deleted", - book: database.books, - author: database.author, + book: updatedBook, + author: updatedAuthor, }) });