diff --git a/database.js b/database.js index 569626e..040c449 100644 --- a/database.js +++ b/database.js @@ -1,10 +1,10 @@ -const books = [{ +let books = [{ ISBN:"1234BOOK", title:"Getting started with MERN", pubDate:"20201-07-07", language:"en", numPage:250, - author:[1, 2], + authors:[1, 2], publication:[1], category:["tech", "programing", "education", "triller"], }, @@ -14,7 +14,7 @@ const books = [{ pubDate:"20201-07-07", language:"en", numPage:250, - author:[1, 2], + authors:[1, 2], publication:[2], category:["tech", "programing", "education", "triller"], } diff --git a/index.js b/index.js index 4f938ba..1c50712 100644 --- a/index.js +++ b/index.js @@ -291,6 +291,62 @@ booky.put("/publication/update/book/:isbn", (req, res) => { }); }); + /* +Route /book/delete +Discription Delete a book +Access Public +Parameter isbn +Method DELETE +*/ +booky.delete("/book/delete/:isbn", (req, res) => { + + const updatedBookDatabase = database.books.filter( + (book) => book.ISBN !== req.params.isbn + ); + + database.books = updatedBookDatabase; + return res.json({ books: database.books }); +}); + + /* +Route /book/delete/author +Discription Delete a book from publication +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 + + book.authors = newAuthorList; + return; + } + }); + + //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; + } + }); + + return res.json({ + message: "author was deleted", + book: database.books, + author: database.author, + }) +}); + booky.listen(3000, () => console.log("Hey server booting...")); //HTTP client -> helper who helps you to make http request \ No newline at end of file diff --git a/requirements.js b/requirements.js index ca0b0d4..e8297bb 100644 --- a/requirements.js +++ b/requirements.js @@ -63,11 +63,11 @@ // PUT // UPdate the publication name using it's id [Task] 🚀 -// update/add books to publications +// update/add books to publications✅ // DELETE -// Delete the publication -// delete a book from publication. +// Delete the publication✅ +// delete a book from publication.✅ //How the server serves the request \ No newline at end of file