Skip to content

Commit

Permalink
add Pub Put api
Browse files Browse the repository at this point in the history
  • Loading branch information
AdithyakrishnaV committed Jun 29, 2021
1 parent 761aee2 commit 8fa9e82
Show file tree
Hide file tree
Showing 3 changed files with 174 additions and 5 deletions.
21 changes: 18 additions & 3 deletions database.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [
Expand All @@ -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};
module.exports = {books, author, publications};
118 changes: 117 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ const database = require("./database");
// Initialization
const booky = express();

//Configuration
booky.use(express.json());

/*
Route /
Expand Down Expand Up @@ -177,4 +179,118 @@ booky.get("/publication/book/:isbn", (req, res) => {
return res.json({publication: getSpecificPublication});
});

booky.listen(3000, () => console.log("Hey server booting..."));
/*
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
40 changes: 39 additions & 1 deletion requirements.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,23 +13,61 @@

// What are all the APIs that we need.....

//GET
// Books
// We need an API
// to get all books ✅
// to get specific books✅
// 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] ✅
// 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

0 comments on commit 8fa9e82

Please sign in to comment.