From 48b91fb60d022541e77c18aa123e211056b5220e Mon Sep 17 00:00:00 2001 From: bbarth86 <46633689+bbarth86@users.noreply.github.com> Date: Tue, 16 Jul 2019 22:06:36 -0700 Subject: [PATCH 1/3] updated routes - authors & books --- api/routes/authors.js | 50 +++++++++++++++++++ api/routes/books.js | 111 +++++++++++++++--------------------------- 2 files changed, 89 insertions(+), 72 deletions(-) create mode 100644 api/routes/authors.js diff --git a/api/routes/authors.js b/api/routes/authors.js new file mode 100644 index 0000000..bf55616 --- /dev/null +++ b/api/routes/authors.js @@ -0,0 +1,50 @@ +const router = require('express').Router({ mergeParams: true }); +const Books = require('../models/books.js') + +// GET /api/books/:bookId/authors +router.get('/', async (req, res, next) => { + const status = 200 + const authors = await Books.findById(req.params.bookId).select('authors') + res.json({ status, authors }) +}) + +// GET /api/books/:bookId/authors/:authorId +router.get('/:authorId', async (req, res, next) => { + const status = 200 + const book = await Books.findById(req.params.bookId) + const authorId = await book.authors.id(req.params.authorId) + res.json({ status, authorId }) + }) + +// POST /api/books/:bookId/authors +router.post('/', async (req, res, next) => { + const status = 201 + const book = await Books.findById(req.params.bookId) + book.authors.push(req.body) + await book.save() + const authors = await Books.findById(req.params.bookId).select('authors') + res.json({ status, authors }) +}) + + +// PUT /api/books/:bookId/authors/:authorId +router.put('/:authorId', async (req, res, next) => { + const status = 200 + const book = await Books.findById(req.params.bookId) + const authorId = await book.authors.id(req.params.authorId) + Object.assign(authorId, req.body) + await book.save() + const authors = await Books.findById(req.params.bookId).select('authors') + res.json({ status, authors }) +}) + +// DELETE /api/books/:bookId/authors/:authorId +router.delete('/:authorId', async (req, res, next) => { + const status = 200 + const book = await Books.findById(req.params.bookId) + const authorRemoved = await book.authors.id(req.params.authorId).remove() + await book.save() + res.json({ status, authorRemoved }) +}) + +module.exports = router \ No newline at end of file diff --git a/api/routes/books.js b/api/routes/books.js index 10a56a9..5ad1a15 100644 --- a/api/routes/books.js +++ b/api/routes/books.js @@ -1,73 +1,40 @@ -const router = require('express').Router() -const { generate: generateId } = require('shortid') - -const books = [ - { - id: 'j9U3iNIQi', - title: 'The Colour of Magic', - published: 1983, - authors: [ - { - name: 'Sir Terry Pratchett', - dob: '04-28-1948' - } - ] - }, - { - id: 'ubQnXOfJV', - title: 'Stardust', - published: 1997, - authors: [ - { - name: 'Neil Gaiman', - dob: '11-10-1960' - } - ] - } -]; - -router.get('/', (req, res, next) => { - const status = 200 - const response = books - - 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 }) -}) - -router.delete('/:id', (req, res, next) => { - const status = 200 - const response = books.find(({ id }) => id === req.params.id) - const index = books.indexOf(response) - - books.splice(index, 1) - - res.json({ status, response }) -}) - +const router = require('express').Router() +const Books = require('../models/books.js') + +router.get('/', async (req, res, next) => { + const status = 200 + const response = await Books.find().select('_id title published authors') + res.json({ status, response }) +}) + +router.post('/', async (req, res, next) => { + const status = 201 + const response = await Books.create(req.body) + res.json({ status, response }) +}) + +router.get('/:id', async (req, res, next) => { + const status = 200 + const response = await Books.findById(req.params.id).select('_id title published authors') + res.json({ status, response }) +}) + +router.put('/:id', async (req, res, next) => { + const status = 200 + const response = await Books.findOneAndUpdate({ + _id: req.params.id + }, { + ...req.body + }, { + new: true + }).select('_id title published authors') + res.json({ status, response }) +}) + +router.delete('/:id', async (req, res, next) => { + const status = 200 + const response = await Books.findOneAndDelete({_id: req.params.id}).select('_id title published authors') + res.json({ status, response }) +}) + module.exports = router \ No newline at end of file From 763a63caecf0494ad9b37f14125a92fc71b85011 Mon Sep 17 00:00:00 2001 From: bbarth86 <46633689+bbarth86@users.noreply.github.com> Date: Tue, 16 Jul 2019 22:07:59 -0700 Subject: [PATCH 2/3] Initial commit - Models - Books.js --- api/models/books.js | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 api/models/books.js diff --git a/api/models/books.js b/api/models/books.js new file mode 100644 index 0000000..34f8b5c --- /dev/null +++ b/api/models/books.js @@ -0,0 +1,28 @@ +const mongoose = require('mongoose') +const Schema = mongoose.Schema + +const schema = new Schema({ +// _id: Schema.Types.ObjectId, +title: { + type: String, + required: true + }, + published: { + type: Number, + required: true + }, + authors: [{ + name: { + type: String, + required: true + }, + dob: { + type: String, + required: true + } + }] +}, { + timestamps: { createdAt: 'created_at', updatedAt: 'updated_at' } +}) + + module.exports = mongoose.model('Books', schema); From e2ac7bcf1a8b8f1481c8036a82967163ddd62dad Mon Sep 17 00:00:00 2001 From: bbarth86 <46633689+bbarth86@users.noreply.github.com> Date: Tue, 16 Jul 2019 22:09:31 -0700 Subject: [PATCH 3/3] Updated app.js --- app.js | 75 +++++++++++++++++++++++++++++----------------------------- 1 file changed, 38 insertions(+), 37 deletions(-) diff --git a/app.js b/app.js index 075ce75..889c605 100644 --- a/app.js +++ b/app.js @@ -1,37 +1,38 @@ -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...') -} else { - 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()) - -// Routes -app.use('/api/books', require('./api/routes/books')) - -// Not Found Handler -app.use((req, res, next) => { - 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 }) -}) - -// Open Connection -const listener = () => console.log(`Listening on Port ${PORT}!`) -app.listen(PORT, listener) +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, useFindAndModify: false }) + console.log('Connected to database...') +} else { + 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()) + +// Routes +app.use('/api/books', require('./api/routes/books')) +app.use('/api/books/:bookId/authors', require('./api/routes/authors')) + +// Not Found Handler +app.use((req, res, next) => { + 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 }) +}) + +// Open Connection +const listener = () => console.log(`Listening on Port ${PORT}!`) +app.listen(PORT, listener)