Skip to content

Commit

Permalink
add method Delete api
Browse files Browse the repository at this point in the history
  • Loading branch information
AdithyakrishnaV committed Jun 29, 2021
1 parent 8fa9e82 commit 878deb6
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 6 deletions.
6 changes: 3 additions & 3 deletions database.js
Original file line number Diff line number Diff line change
@@ -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"],
},
Expand All @@ -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"],
}
Expand Down
56 changes: 56 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
6 changes: 3 additions & 3 deletions requirements.js
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 878deb6

Please sign in to comment.