From f0eb16bf707075dddabd1453be5f8b1f025b50c1 Mon Sep 17 00:00:00 2001 From: rubini124 Date: Tue, 16 Jul 2019 22:17:51 -0700 Subject: [PATCH] Week 3 homework exercise --- api/models/books.js | 19 +++++++ api/routes/books.authors.js | 47 ++++++++++++++++ api/routes/books.js | 103 +++++++++++++++++++----------------- app.js | 44 ++++++++------- 4 files changed, 143 insertions(+), 70 deletions(-) create mode 100644 api/models/books.js create mode 100644 api/routes/books.authors.js diff --git a/api/models/books.js b/api/models/books.js new file mode 100644 index 0000000..c33a48e --- /dev/null +++ b/api/models/books.js @@ -0,0 +1,19 @@ +const mongoose = require("mongoose"); + +const schema = new mongoose.Schema( + { + title: { type: String, required: true }, + published: { type: Number, required: true }, + authors: [ + { + name: { type: String }, + dob: { type: Date } + } + ] + }, + { + timestamps: { createdAt: "created_at", updatedAt: "updated_at" } + } +); + +module.exports = mongoose.model("Books", schema); diff --git a/api/routes/books.authors.js b/api/routes/books.authors.js new file mode 100644 index 0000000..6cdcc7a --- /dev/null +++ b/api/routes/books.authors.js @@ -0,0 +1,47 @@ +const router = require("express").Router({ mergeParams: true }); +const Books = require("../models/books"); + +//gets the authors object for a given book id +router.get("/", async (req, res, next) => { + const status = 200; + const { authors } = await Books.findById(req.params.bookId).select("authors"); + res.json({ status, authors }); +}); + +//gets the author name and dob for a given author id +router.get("/:authorId", async (req, res, next) => { + const status = 200; + const { authors } = await Books.findById(req.params.bookId).select("authors"); + const author = authors.id(req.params.id); + res.json({ status, author }); +}); + +//post (create) authors for a given book id +router.post("/", async (req, res, next) => { + const status = 201; + const book = await Books.findById(req.params.bookId).select( + "title published authors" + ); + book.authors.push(req.body); + await book.save(); + res.json({ status, book }); +}); + +//updates a book document with author object for a given author id +router.put("/:authorId", async (req, res, next) => { + const status = 200; + const book = await Books.findById(req.params.bookId); + const author = book.author.id(req.params.id); + Object.assign(author, req.body); + await book.save(); + res.json({ status, author }); +}); + +//delete author for a given author id +router.delete("/:authorId", async (req, res, next) => { + const status = 200; + const book = await Books.findById(req.params.bookId); + const author = book.authors.id(req.params.id).remove(); + await book.save(); + res.json({ status, author }); +}); diff --git a/api/routes/books.js b/api/routes/books.js index 10a56a9..11ff35f 100644 --- a/api/routes/books.js +++ b/api/routes/books.js @@ -1,73 +1,76 @@ -const router = require('express').Router() -const { generate: generateId } = require('shortid') +const router = require("express").Router(); +const { generate: generateId } = require("shortid"); +const Books = require("../models/books"); const books = [ { - id: 'j9U3iNIQi', - title: 'The Colour of Magic', + id: "j9U3iNIQi", + title: "The Colour of Magic", published: 1983, authors: [ { - name: 'Sir Terry Pratchett', - dob: '04-28-1948' + name: "Sir Terry Pratchett", + dob: "04-28-1948" } ] }, { - id: 'ubQnXOfJV', - title: 'Stardust', + id: "ubQnXOfJV", + title: "Stardust", published: 1997, authors: [ { - name: 'Neil Gaiman', - dob: '11-10-1960' + name: "Neil Gaiman", + dob: "11-10-1960" } ] } ]; -router.get('/', (req, res, next) => { - const status = 200 - const response = books - - res.json({ status, response }) -}) +//gets all the books +router.get("/", async (req, res, next) => { + const status = 200; + const response = await Books.find().select("-__v"); + res.json({ status, response }); +}); -router.post('/', (req, res, next) => { - const status = 201 - - books.push({ id: generateId(), ...req.body }) - const response = books - - res.json({ status, response }) -}) - -router.get('/:id', (req, res, next) => { - const status = 200 - const response = books.find(({ id }) => id === req.params.id) - - res.json({ status, response }) -}) - -router.put('/:id', (req, res, next) => { - const status = 200 - const response = { id: req.params.id, ...req.body } - const single = books.find(({ id }) => id === req.params.id) - const index = books.indexOf(single) - - books.splice(index, 1, response) - - res.json({ status, response }) -}) +//creates book document +router.post("/", async (req, res, next) => { + const status = 201; + try { + const response = await Books.create(req.body); + res.json({ status, response }); + } catch (error) { + console.error(error); + const e = new Error("Something went bad"); + e.status = 400; + next(e); + } +}); -router.delete('/:id', (req, res, next) => { - const status = 200 - const response = books.find(({ id }) => id === req.params.id) - const index = books.indexOf(response) +//gets a book document with given id +router.get("/:id", async (req, res, next) => { + const status = 200; + const response = await Books.findById(req.params.id); + res.json({ status, response }); +}); - books.splice(index, 1) +//updates a book document with given id +router.put("/:id", async (req, res, next) => { + const status = 200; + const response = await Books.findOneAndUpdate( + { _id: req.params.id }, + { title: req.body.title }, + { new: true } + ); + res.json({ status, response }); +}); - res.json({ status, response }) -}) +//deletes a book document with given id +router.delete("/:id", async (req, res, next) => { + const status = 200; + const response = await Books.findOneAndDelete({ _id: req.params.id }); + res.json({ status, response }); +}); -module.exports = router \ No newline at end of file +module.exports = router; diff --git a/app.js b/app.js index 075ce75..d2bfb5d 100644 --- a/app.js +++ b/app.js @@ -1,37 +1,41 @@ -const { MONGO_DB_CONNECTION, NODE_ENV, PORT } = process.env -const express = require('express') -const mongoose = require('mongoose') -const app = express() +const { MONGO_DB_CONNECTION, NODE_ENV, PORT } = process.env; +const express = require("express"); +const mongoose = require("mongoose"); +const app = express(); // Database Connection if (MONGO_DB_CONNECTION) { - mongoose.connect(MONGO_DB_CONNECTION, { useNewUrlParser: true }) - console.log('Connected to database...') + mongoose.connect(MONGO_DB_CONNECTION, { + useNewUrlParser: true, + useFindAndModify: false + }); + console.log("Connected to database..."); } else { - console.log('Could not connect to database!') + console.log("Could not connect to database!"); } // Application-level Middleware -if (NODE_ENV === 'development') app.use(require('morgan')('dev')) -app.use(require('body-parser').json()) +if (NODE_ENV === "development") app.use(require("morgan")("dev")); +app.use(require("body-parser").json()); // Routes -app.use('/api/books', require('./api/routes/books')) +app.use("/api/books", require("./api/routes/books")); +app.use("api/books/bookId/authors", require("./api/routes/books.authors")); // Not Found Handler app.use((req, res, next) => { - const error = new Error(`Could not ${req.method} ${req.path}`) - error.status = 404 - next(error) -}) + const error = new Error(`Could not ${req.method} ${req.path}`); + error.status = 404; + next(error); +}); // Error Handler app.use((err, req, res, next) => { - if (NODE_ENV === 'development') console.error(err) - const { message, status } = err - res.status(status).json({ status, message }) -}) + if (NODE_ENV === "development") console.error(err); + const { message, status } = err; + res.status(status).json({ status, message }); +}); // Open Connection -const listener = () => console.log(`Listening on Port ${PORT}!`) -app.listen(PORT, listener) +const listener = () => console.log(`Listening on Port ${PORT}!`); +app.listen(PORT, listener);