diff --git a/database.js b/database.js index ef76291..569626e 100644 --- a/database.js +++ b/database.js @@ -8,6 +8,16 @@ const books = [{ publication:[1], category:["tech", "programing", "education", "triller"], }, + { + ISBN:"1234TWO", + title:"Say hello to HTML", + pubDate:"20201-07-07", + language:"en", + numPage:250, + author:[1, 2], + publication:[2], + category:["tech", "programing", "education", "triller"], + } ]; const author = [ @@ -23,12 +33,17 @@ const author = [ }, ]; -const publication = [ +const publications = [ { id:1, name:"Writex", books:["1234BOOK"] - } + }, + { + id:2, + name:"Trex", + books:[] + } ]; -module.exports = {books, author, publication}; \ No newline at end of file +module.exports = {books, author, publications}; \ No newline at end of file diff --git a/index.js b/index.js index 3d7a72f..4f938ba 100644 --- a/index.js +++ b/index.js @@ -7,6 +7,8 @@ const database = require("./database"); // Initialization const booky = express(); +//Configuration +booky.use(express.json()); /* Route / @@ -177,4 +179,118 @@ booky.get("/publication/book/:isbn", (req, res) => { return res.json({publication: getSpecificPublication}); }); -booky.listen(3000, () => console.log("Hey server booting...")); \ No newline at end of file +/* +Route /book/add +Discription Add new book +Access Public +Parameter isbn +Method POST +*/ +booky.post("/book/add", (req, res) => { + const {newBook} = req.body; // const newBook = req.body.newBook; -> const {newBook} = req.body; --> destructuring + database.books.push(newBook); + return res.json({ book: database.books }); +}); + +/* +Route /author/add +Discription Add new author +Access Public +Parameter isbn +Method POST +*/ +booky.post("/author/add", (req, res) => { + const {newAuthor} = req.body; + database.author.push(newAuthor); + return res.json({ author: database.author }); +}); + +/* +Route /publication/add +Discription Add new author +Access Public +Parameter isbn +Method POST +*/ +booky.post("/publication/add", (req, res) => { + const {newPublication} = req.body; + database.publication.push(newPublication); + return res.json({ author: database.publication }); +}); + +/* +Route /book/update/title +Discription Update book title +Access Public +Parameter isbn +Method PUT +*/ +booky.put("/book/update/title/:isbn", (req, res) => { + database.books.forEach((book) => { + if(book.ISBN === req.params.isbn) { + book.title = req.body.newBookTitle; + return; + } + }); + + return res.json({ book: database.books}); +}); + +/* +Route /book/update/author +Discription Add / update new author for a book +Access Public +Parameter isbn +Method PUT +*/ +booky.put("/book/update/author/:isbn/:authorId", (req, res) => { + //update book database + database.books.forEach((book) => { + if (book.ISBN === req.params.isbn) { + return book.author.push(parseInt(req.params.authorId)); + } + }); + + // update author database + + database.author.forEach((author) => { + if (author.id === parseInt(req.params.authorId)) + return author.books.push(req.params.isbn); + }); + + return res.json({ books: database.books, author: database.author }); + }); + +/* +Route /publication/update/book +Discription Update/add books to publications +Access Public +Parameter isbn +Method PUT +*/ +booky.put("/publication/update/book/:isbn", (req, res) => { + //update the publication database + database.publications.forEach((publication) => { + if (publication.id === req.body.pubId) { + return publication.books.push(req.params.isbn); + } + }); + + //update book database + database.books.forEach((book) => { + if (book.ISBN === req.params.isbn) { + book.publication = req.body.pubId; + return; + } + }); + + return res.json({ + books: database.books, + publications: database.publications, + message: "Successfully updated the publication", + }); + }); + +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 e06eb3a..ca0b0d4 100644 --- a/requirements.js +++ b/requirements.js @@ -13,6 +13,7 @@ // What are all the APIs that we need..... +//GET // Books // We need an API // to get all books ✅ @@ -20,16 +21,53 @@ // to get list of books based on category✅ // to get list of books based on languages [task]✅ +//POST ->to send data +//Add new book✅ + +//PUT -> update +//Update book title ✅ +//Add / update new author✅ + +//DELETE +//Delete a book +//Delete an author + // Authors +//GET // We need an api // to get all authors✅ // to get specific authors [task]✅ // to get list of authors based on books ✅ +// POST +// Add new author✅ + +// PUT +// Update Author name using it's id [Task] ✅ + +// DELETE +// Delete an author + + // Publications +//GET // we need an API // to get all publication ✅ // to get specific publication [task] ✅ -// to get list of publication based on book [task] ✅ \ No newline at end of file +// to get list of publication based on book [task] ✅ + +// POST +// Add new publications [Task] 🚀 + +// PUT +// UPdate the publication name using it's id [Task] 🚀 +// update/add books to publications + +// DELETE +// Delete the publication +// delete a book from publication. + + +//How the server serves the request \ No newline at end of file