Skip to content

Commit

Permalink
add mogoosee validation
Browse files Browse the repository at this point in the history
  • Loading branch information
AdithyakrishnaV committed Jul 4, 2021
1 parent 80ad001 commit 7d62ecb
Show file tree
Hide file tree
Showing 6 changed files with 229 additions and 115 deletions.
57 changes: 35 additions & 22 deletions API/Author/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,12 @@ Parameter None
Method GET
*/
Router.get("/", async (req, res) => {
try {
const getAllAuthors = await AuthorModel.findOne();
return res.json({ getAllAuthors });
} catch (error) {
return res.json({error: error.message});
}
});

/*
Expand All @@ -25,15 +29,18 @@ Access Public
Parameter name
Method GET
*/
Router.get("/:name", (req, res) => {
const getSpecificBook = database.author.filter(
(author) => author.name === req.params.name
);
if (getSpecificBook.length === 0) {
return res.json({error:`No book found for the author of ${req.params.name}`,});
Router.get("/:name", async (req, res) => {
try {
const getSpecificBook = await AuthorModel.findOne({name: req.params.name});

if (!getSpecificBook) {
return res.json({error:`No book found for the author of ${req.params.name}`,});
}

return res.json({author: getSpecificBook});
} catch (error) {
return res.json({error: error.message});
}

return res.json({author: getSpecificBook});
});

/*
Expand All @@ -43,17 +50,19 @@ Access Public
Parameter isbn
Method GET
*/
Router.get("/book/:isbn", (req, res) => {
const getSpecificAuthor = database.author.filter(
(author) => author.books.includes(req.params.isbn)
);

if (getSpecificAuthor.length === 0) {
return res.json({error:`No Author found for the book of ${req.params.isbn}`,
});
}
Router.get("/book/:isbn", async (req, res) => {
try {
const getSpecificAuthor = await AuthorModel.findOne({books: req.params.isbn});

return res.json({authors: getSpecificAuthor});
if (!getSpecificAuthor) {
return res.json({error:`No Author found for the book of ${req.params.isbn}`,
});
}

return res.json({authors: getSpecificAuthor});
} catch (error) {
return res.json({error: error.message});
}
});

/*
Expand All @@ -64,10 +73,14 @@ Parameter isbn
Method POST
*/
Router.post("/add", (req, res) => {
const {newAuthor} = req.body;
AuthorModel.create(newAuthor);

return res.json({ message: "author was added" });
try {
const {newAuthor} = req.body;
AuthorModel.create(newAuthor);

return res.json({ message: "author was added" });
} catch (error) {
return res.json({error: error.message});
}
});

module.exports = Router;
113 changes: 71 additions & 42 deletions API/Book/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,12 @@ Parameter None
Method GET
*/
Router.get("/", async (req, res) => {
try {
const getAllBooks = await BookModel.find();
return res.json(getAllBooks);
} catch (error) {
return res.json({error: error.message});
}
});


Expand All @@ -28,15 +32,19 @@ Parameter isbn
Method GET
*/
Router.get("/is/:isbn", async (req, res) => {
const getSpecificBook = await BookModel.findOne({ ISBN: req.params.isbn });
try {
const getSpecificBook = await BookModel.findOne({ ISBN: req.params.isbn });

if (!getSpecificBook) {
return res.json(
{error:`No book found for the ISBN of ${req.params.isbn}`,
});
if (!getSpecificBook) {
return res.json(
{error:`No book found for the ISBN of ${req.params.isbn}`,
});
}

return res.json({book: getSpecificBook});
} catch (error) {
return res.json({error: error.message});
}

return res.json({book: getSpecificBook});
});


Expand All @@ -48,13 +56,17 @@ Parameter category
Method GET
*/
Router.get("/c/:category", async (req, res) => {
const getSpecificBook = await BookModel.findOne({ category: req.params.category });
try {
const getSpecificBook = await BookModel.findOne({ category: req.params.category });

if (!getSpecificBook) {
return res.json({error:`No book found for the category of ${req.params.category}`,});
if (!getSpecificBook) {
return res.json({error:`No book found for the category of ${req.params.category}`,});
}

return res.json({book: getSpecificBook});
} catch (error) {
return res.json({error: error.message})
}

return res.json({book: getSpecificBook});
});


Expand All @@ -66,14 +78,18 @@ Parameter lan
Method GET
*/
Router.get("/lan/:language", async (req, res) => {

const getSpecificBook = await BookModel.findOne({ language: req.params.language });
try {
const getSpecificBook = await BookModel.findOne({ language: req.params.language });

if (!getSpecificBook) {
return res.json({error:`No book found for the language of ${req.params.language}`,});
if (!getSpecificBook) {
return res.json({error:`No book found for the language of ${req.params.language}`,});
}

return res.json({book: getSpecificBook});
} catch (error) {
return res.json({error: error.message})
}

return res.json({book: getSpecificBook});
});


Expand All @@ -85,11 +101,15 @@ Parameter isbn
Method POST
*/
Router.post("/add", async (req, res) => {
try {
const {newBook} = req.body; // const newBook = req.body.newBook; -> const {newBook} = req.body; --> destructuring

const addNewBook = BookModel.create(newBook);
await BookModel.create(newBook);

return res.json({ books: addNewBook, message: "new book added" });
return res.json({ newBook, message: "new book added" });
} catch (error) {
return res.json({error: error.message});
}
});


Expand All @@ -102,18 +122,22 @@ Method PUT
*/
Router.put("/update/title/:isbn", async (req, res) => {

const updatedBook = await BookModel.findOneAndUpdate({
ISBN: req.params.isbn,
},
{
title: req.body.bookTitle,
},
{
new: true,
try {
const updatedBook = await BookModel.findOneAndUpdate({
ISBN: req.params.isbn,
},
{
title: req.body.bookTitle,
},
{
new: true,
}
);

return res.json({ book: updatedBook});
} catch (error) {
return res.json({error: error.message});
}
);

return res.json({ book: updatedBook});
});


Expand All @@ -125,7 +149,8 @@ Parameter isbn
Method PUT
*/
Router.put("/update/author/:isbn", async (req, res) => {
//update book database
try {
//update book database

const updatedBook = await BookModel.findOneAndUpdate({
ISBN: req.params.isbn
Expand Down Expand Up @@ -171,6 +196,9 @@ Router.put("/update/author/:isbn", async (req, res) => {
author: updatedAuthor,
message: "New author was added"
});
} catch (error) {
return res.json({error: error.message});
}
});

/*
Expand All @@ -181,18 +209,19 @@ Parameter isbn
Method DELETE
*/
Router.delete("/delete/:isbn", async (req, res) => {

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 });
try {
const updatedBookDatabase = await BookModel.findOneAndDelete(
{
ISBN: req.params.isbn
}
);

return res.json({ books: updatedBookDatabase });
} catch (error) {
return res.json({error: error.message});
}

});


Expand Down
Loading

0 comments on commit 7d62ecb

Please sign in to comment.