From 7d62ecb45a9460fe0dc3902af2364544bb2506e3 Mon Sep 17 00:00:00 2001 From: AdithyakrishnaV Date: Sun, 4 Jul 2021 14:20:15 +0530 Subject: [PATCH] add mogoosee validation --- API/Author/index.js | 57 ++++++++++------- API/Book/index.js | 113 ++++++++++++++++++++------------- API/Publication/index.js | 132 ++++++++++++++++++++++++++------------- author.js | 7 ++- book.js | 28 +++++++-- publication.js | 7 ++- 6 files changed, 229 insertions(+), 115 deletions(-) diff --git a/API/Author/index.js b/API/Author/index.js index 7d57072..2631fca 100644 --- a/API/Author/index.js +++ b/API/Author/index.js @@ -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}); + } }); /* @@ -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}); }); /* @@ -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}); + } }); /* @@ -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; \ No newline at end of file diff --git a/API/Book/index.js b/API/Book/index.js index c74fd42..680ac2e 100644 --- a/API/Book/index.js +++ b/API/Book/index.js @@ -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}); + } }); @@ -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}); }); @@ -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}); }); @@ -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}); }); @@ -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}); + } }); @@ -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}); }); @@ -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 @@ -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}); + } }); /* @@ -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}); + } + }); diff --git a/API/Publication/index.js b/API/Publication/index.js index 313fe92..549dcee 100644 --- a/API/Publication/index.js +++ b/API/Publication/index.js @@ -1,6 +1,7 @@ //Initializing Express Router const Router = require("express").Router(); +const BookModel = require("../../book"); const PublicationModel = require("../../publication"); @@ -12,7 +13,11 @@ Parameter NONE Method GET */ Router.get("/", (req, res) => { - return res.json({publications:database.publication}); + try { + return res.json({publications:database.publication}); + } catch (error) { + return res.json({error: error.message}); + } }); /* @@ -22,15 +27,18 @@ Access Public Parameter publications Method GET */ -Router.get("/:name", (req, res) => { - const getSpecificPublication = database.publication.filter( - (publication) => publication.name === req.params.name - ); - if (getSpecificPublication.length === 0) { +Router.get("/:name", async (req, res) => { + try { + const getSpecificPublication = await PublicationModel.findOne({name: req.params.name}); + + if (!getSpecificPublication) { return res.json({error:`No book found for the publication of ${req.params.name}`,}); } return res.json({publication: getSpecificPublication}); + } catch (error) { + return res.json({error: error.message}); + } }); /* @@ -40,16 +48,19 @@ Access Public Parameter isbn Method GET */ -Router.get("/book/:isbn", (req, res) => { - const getSpecificPublication = database.publication.filter( - (publication) => publication.books.includes(req.params.isbn) - ); +Router.get("/book/:isbn", async (req, res) => { + try { + const getSpecificPublication = await PublicationModel.findOne({books: req.params.isbn}); + - if (getSpecificPublication.length === 0) { + if (!getSpecificPublication) { return res.json({error:`No publication found for the book of ${req.params.isbn}`,}); } return res.json({publication: getSpecificPublication}); + } catch (error) { + return res.json({error: error.message}); + } }); @@ -61,10 +72,14 @@ Parameter isbn Method POST */ Router.post("/add", async (req, res) => { - const {newPublication} = req.body; - PublicationModel.create(newPublication); - - return res.json({ author: database.publication }); + try { + const {newPublication} = req.body; + PublicationModel.create(newPublication); + + return res.json({ author: database.publication }); + } catch (error) { + return res.json|({error: error.message}); + } }); @@ -75,27 +90,47 @@ Access Public Parameter isbn Method PUT */ -Router.put("/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", - }); +Router.put("/update/book/:isbn", async (req, res) => { + try { + //update the publication database + const updatedPublication = await PublicationModel.findOneAndUpdate({ + id: req.body.pubId + }, + { + $addToSet: { + books: req.params.isbn + } + }, + { + new: true, + } + ); + +//update book database +const updatedBooks = await BookModel.findOneAndUpdate( + { + ISBN: req.params.isbn + }, + { + $addToSet: { + publication: req.body.pubId + } + }, + { + new: true + } + ); + + + return res.json({ + books: updatedBooks, + publications: updatedPublication, + message: "Successfully updated the publication", + }); + } catch (error) { + return res.json({error: error.message}); + } + }); @@ -106,8 +141,10 @@ Access Public Parameter isbn, publication id Method DELETE */ -Router.delete("/delete/book/:isbn/:pubId", (req, res) => { - //update publication database +Router.delete("/delete/book/:isbn/:pubId", async (req, res) => { + try { + //update publication database + database.publications.forEach((publication) => { if(publication.id === parseInt(req.params.pubId)) { const newBooksList = publication.books.filter( @@ -120,18 +157,23 @@ Router.delete("/delete/book/:isbn/:pubId", (req, res) => { }); //update book database - database.books.forEach((book) => { + + database.books.forEach((book) => { if(book.ISBN !== req.params.isbn){ book.publication = 0; return; } - }); + }); - return res.json({ - books: database.books, - publications: database.publications, - }); -}); + return res.json({ + books: database.books, + publications: database.publications, + }); + v + } catch (error) { + return res.json({error: error.message}); + } +}); module.exports = Router; diff --git a/author.js b/author.js index 0434861..503d239 100644 --- a/author.js +++ b/author.js @@ -3,7 +3,12 @@ const mongoose = require("mongoose"); //Creating an author schema const AuthorSchema = mongoose.Schema({ id: Number, - name: String, + name: { + type: String, + required: true, + minLength: 4, + maxLength: 12 + }, books:[String] }); diff --git a/book.js b/book.js index f067ef2..999a0ad 100644 --- a/book.js +++ b/book.js @@ -2,10 +2,30 @@ const mongoose = require("mongoose"); //Creating a book schema const BookSchema = mongoose.Schema({ - ISBN: String, - title: String, - pubDate: String, - language: String, + ISBN: { + type: String, + required: true, + minLength: 8, + maxLength: 10 + }, + title: { + type: String, + required: true, + minLength: 8, + maxLength: 10 + }, + pubDate:{ + type: String, + required: true, + minLength: 8, + maxLength: 10 + }, + language: { + type: String, + required: true, + minLength: 2, + maxLength: 10 + }, numPage: Number, authors: [Number], publication: [Number], diff --git a/publication.js b/publication.js index d7e8387..3b2ffe2 100644 --- a/publication.js +++ b/publication.js @@ -3,7 +3,12 @@ const mongoose = require("mongoose"); // Publication schema const PublicationSchema = mongoose.Schema({ id: Number, - name: String, + name: { + type: String, + required: true, + minLength: 4, + maxLength: 10 + }, books:[String] });