Skip to content

Commit

Permalink
add operator
Browse files Browse the repository at this point in the history
  • Loading branch information
AdithyakrishnaV committed Jul 2, 2021
1 parent 8ce85cc commit 807b98d
Showing 1 changed file with 44 additions and 30 deletions.
74 changes: 44 additions & 30 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
})
});

Expand Down

0 comments on commit 807b98d

Please sign in to comment.