From 5f57fe5d422e6423832bef3b048e75c7a6077ebd Mon Sep 17 00:00:00 2001 From: slosen Date: Fri, 19 Jul 2019 18:43:39 -0700 Subject: [PATCH 1/4] cant get authors to work still --- api/models/books.js | 20 +++++++++++++++++ api/routes/books.authors.js | 44 +++++++++++++++++++++++++++++++++++++ api/routes/books.js | 44 +++++++++++++++++++------------------ app.js | 3 ++- package.json | 1 + 5 files changed, 90 insertions(+), 22 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..b60e663 --- /dev/null +++ b/api/models/books.js @@ -0,0 +1,20 @@ +const mongoose = require('mongoose'); + +const schema = new mongoose.Schema({ + title: { + type: String, + required: true + }, + published: Number, + authors: [{ + name: { + type: String, + required: true + }, + dob: Number + }] + }, { + timestamps: { createdAt: 'created_at', updatedAt: 'updated_at' } +}); + +module.exports = mongoose.model('Books', schema) \ No newline at end of file diff --git a/api/routes/books.authors.js b/api/routes/books.authors.js new file mode 100644 index 0000000..6a61311 --- /dev/null +++ b/api/routes/books.authors.js @@ -0,0 +1,44 @@ +const router = require('express').Router({mergeParams: true}) +const Books = require('../models/books') + + +router.get('/', async (req, res, next) => { + const status = 200 + const {authors} = await Books.findById(req.params.bookId).select('authors') + + res.json({ status, authors }) +}) + +router.post('/', async (req, res, next) => { + const status = 201 + const books = await Books.findById(req.params.bookId) + + books.authors.push(req.body) + await books.save()//dont undertsand what is happening here + + const author = books.authors[books.authors.length -1] + res.status(status).json({ status, author }) //dont undertsand what is happening here +}) + +router.put('/:id', async (req, res, next) => { + const status = 200 + const books = await Books.finById(req.params.seriesId) + const author = books.authors.id(req.params.id)//dont undertsand what is happening here + Object.assign(author, req.body)//dont undertsand what is happening here + await series.save()//dont undertsand what is happening here + + res.status(status).json({ status, author })//dont undertsand what is happening here +}) + +router.delete('/:id', async (req, res, next) => { + const status = 200 + const books = await Books.findfindById(req.params.bookId) + const author = books.authors.id(req.params.id).remove() + await books.save() // is this like a push? + + res.status(status).json({ status, author })//dont undertsand what is happening here +}) + +module.exports = router + +// I am confused about needing this file, because I was able to update the author name before exicuting this code. \ No newline at end of file diff --git a/api/routes/books.js b/api/routes/books.js index 10a56a9..63a3b10 100644 --- a/api/routes/books.js +++ b/api/routes/books.js @@ -1,5 +1,6 @@ const router = require('express').Router() const { generate: generateId } = require('shortid') +const Books = require('../models/books') const books = [ { @@ -26,46 +27,47 @@ const books = [ } ]; -router.get('/', (req, res, next) => { +router.get('/', async (req, res, next) => { const status = 200 - const response = books + const response = await Books.find(req.query).select('_id title published authors') res.json({ status, response }) }) -router.post('/', (req, res, next) => { +router.post('/', async (req, res, next) => { const status = 201 - - books.push({ id: generateId(), ...req.body }) - const response = books - - res.json({ status, response }) + try { + const response = await Books.create(req.body) + res.json({ status, response }) + + } catch (error) { + error.status = 400 + error.message = 'Invalid data. Please try again.' + + next(error) + } + }) -router.get('/:id', (req, res, next) => { +router.get('/:id', async (req, res, next) => { const status = 200 - const response = books.find(({ id }) => id === req.params.id) + const response = await Books.findById(req.params.id) res.json({ status, response }) }) -router.put('/:id', (req, res, next) => { +router.put('/:id', async (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) + const query = {_id:req.params.id} + const options = {new:true} + const response = await Books.findOneAndUpdate(query, req.body, options) res.json({ status, response }) }) -router.delete('/:id', (req, res, next) => { +router.delete('/:id', async (req, res, next) => { const status = 200 - const response = books.find(({ id }) => id === req.params.id) - const index = books.indexOf(response) - - books.splice(index, 1) + const response = await Books.findOneAndDelete({ _id: req.params.id }) res.json({ status, response }) }) diff --git a/app.js b/app.js index 075ce75..e6cc5a2 100644 --- a/app.js +++ b/app.js @@ -5,7 +5,7 @@ const app = express() // Database Connection if (MONGO_DB_CONNECTION) { - mongoose.connect(MONGO_DB_CONNECTION, { useNewUrlParser: true }) + mongoose.connect(MONGO_DB_CONNECTION, { useNewUrlParser: true, useFindAndModify: false }) console.log('Connected to database...') } else { console.log('Could not connect to database!') @@ -17,6 +17,7 @@ app.use(require('body-parser').json()) // Routes 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) => { diff --git a/package.json b/package.json index af470f9..501b171 100644 --- a/package.json +++ b/package.json @@ -4,6 +4,7 @@ "description": "", "main": "app.js", "scripts": { + "debug": "PORT=5000 node --inspect app.js", "dev": "nodemon app.js" }, "keywords": [], From a99f11847ad9e63324daee6accffe7feaab2fe73 Mon Sep 17 00:00:00 2001 From: slosen Date: Mon, 22 Jul 2019 18:44:46 -0700 Subject: [PATCH 2/4] fixes, yet still having issues with put and delete authors --- api/models/books.js | 5 +---- api/routes/books.authors.js | 34 +++++++++++++++++++++++----------- api/routes/books.js | 25 ++++++++++++++++++++++++- 3 files changed, 48 insertions(+), 16 deletions(-) diff --git a/api/models/books.js b/api/models/books.js index b60e663..9fa3e63 100644 --- a/api/models/books.js +++ b/api/models/books.js @@ -7,10 +7,7 @@ const schema = new mongoose.Schema({ }, published: Number, authors: [{ - name: { - type: String, - required: true - }, + name: String, dob: Number }] }, { diff --git a/api/routes/books.authors.js b/api/routes/books.authors.js index 6a61311..ba68aad 100644 --- a/api/routes/books.authors.js +++ b/api/routes/books.authors.js @@ -14,31 +14,43 @@ router.post('/', async (req, res, next) => { const books = await Books.findById(req.params.bookId) books.authors.push(req.body) - await books.save()//dont undertsand what is happening here + await books.save()// ok, it updates an existing document or inserts a new document, depending on its document parameter. const author = books.authors[books.authors.length -1] - res.status(status).json({ status, author }) //dont undertsand what is happening here + res.json({ status, author }) + // ok, so res.status(status) is declaring that the current status of res is "status", and the .json() parses that data as JSON. +}) + +router.get('/:id', async (req, res, next) => { + const status = 200 + const books = await Books.findById(req.params.bookId) + const author = books.authors.id(req.params.id) + await books.save() + + res.status(status).json({ status, author }); }) router.put('/:id', async (req, res, next) => { const status = 200 - const books = await Books.finById(req.params.seriesId) - const author = books.authors.id(req.params.id)//dont undertsand what is happening here - Object.assign(author, req.body)//dont undertsand what is happening here - await series.save()//dont undertsand what is happening here + const books = await Books.findById(req.params.bookId) + const author = books.authors.id(req.params.id) + //here "id" is the name of the parameter, and we are identifying the id of the author through the param that we specify. Could you break this down a little? It has some layers I am not following. + Object.assign(author, req.body) + // ok, so "author" is the target, and "req.body" is the source. We are forever changing "author" by copying/adding the properties from "req.body" to "author" + await books.save() - res.status(status).json({ status, author })//dont undertsand what is happening here + res.status(status).json({ status, author }); }) router.delete('/:id', async (req, res, next) => { const status = 200 - const books = await Books.findfindById(req.params.bookId) + const books = await Books.findById(req.params.bookId) const author = books.authors.id(req.params.id).remove() - await books.save() // is this like a push? + await books.save() - res.status(status).json({ status, author })//dont undertsand what is happening here + res.status(status).json({ status, author }); }) module.exports = router -// I am confused about needing this file, because I was able to update the author name before exicuting this code. \ No newline at end of file +// I am confused about needing this file, because I was able to update the author name before exicuting this code. UPDATE: Nope, i actually was NOT able to do this before this code. Still confused on why I cannot GET /api/books/:bookId/authors/:authorId \ No newline at end of file diff --git a/api/routes/books.js b/api/routes/books.js index 63a3b10..d3273ff 100644 --- a/api/routes/books.js +++ b/api/routes/books.js @@ -51,7 +51,7 @@ router.post('/', async (req, res, next) => { router.get('/:id', async (req, res, next) => { const status = 200 - const response = await Books.findById(req.params.id) + const response = await Books.findById(req.params.id).select('_id title published authors') res.json({ status, response }) }) @@ -72,4 +72,27 @@ router.delete('/:id', async (req, res, next) => { res.json({ status, response }) }) +router.get('/:bookId/authors/:id', async (req, res, next) => { + const status = 200 + const response = await Books.authors.findById(req.params.id) + + res.json({ status, response }) +}) + +router.put('/:bookId/authors/:id', async (req, res, next) => { + const status = 200 + const query = {_id:req.params.id} + const options = {new:true} + const response = await Books.authors.findOneAndUpdate(query, req.body, options) + + res.json({ status, response }) +}) + +router.delete('/:bookId/authors/:id', async (req, res, next) => { + const status = 200 + const response = await Books.authors.findOneAndDelete({ _id: req.params.id }) + + res.json({ status, response }) +}) + module.exports = router \ No newline at end of file From 25fcd12c68ce7897c44a08f12d09af85bc3f83ee Mon Sep 17 00:00:00 2001 From: slosen Date: Mon, 22 Jul 2019 20:18:06 -0700 Subject: [PATCH 3/4] still cant figure out what I am doing wrong here... --- api/routes/books.authors.js | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/api/routes/books.authors.js b/api/routes/books.authors.js index ba68aad..70d8e5a 100644 --- a/api/routes/books.authors.js +++ b/api/routes/books.authors.js @@ -23,11 +23,10 @@ router.post('/', async (req, res, next) => { router.get('/:id', async (req, res, next) => { const status = 200 - const books = await Books.findById(req.params.bookId) - const author = books.authors.id(req.params.id) - await books.save() + const {authors} = await Books.findById(req.params.bookId).select('authors') + const author = authors.id(req.params.id) - res.status(status).json({ status, author }); + res.json({ status, author }) }) router.put('/:id', async (req, res, next) => { From cc1793cb07dd3866c3e4765465ac4c033ffc68a3 Mon Sep 17 00:00:00 2001 From: slosen Date: Tue, 23 Jul 2019 09:11:32 -0700 Subject: [PATCH 4/4] routing fixes, thanks Este! --- api/routes/books.js | 38 +++++++++++++++++++------------------- 1 file changed, 19 insertions(+), 19 deletions(-) diff --git a/api/routes/books.js b/api/routes/books.js index d3273ff..918de98 100644 --- a/api/routes/books.js +++ b/api/routes/books.js @@ -72,27 +72,27 @@ router.delete('/:id', async (req, res, next) => { res.json({ status, response }) }) -router.get('/:bookId/authors/:id', async (req, res, next) => { - const status = 200 - const response = await Books.authors.findById(req.params.id) - - res.json({ status, response }) -}) - -router.put('/:bookId/authors/:id', async (req, res, next) => { - const status = 200 - const query = {_id:req.params.id} - const options = {new:true} - const response = await Books.authors.findOneAndUpdate(query, req.body, options) +// router.get('/:bookId/authors/:id', async (req, res, next) => { +// const status = 200 +// const response = await Books.authors.findById(req.params.id) + +// res.json({ status, response }) +// }) + +// router.put('/:bookId/authors/:id', async (req, res, next) => { +// const status = 200 +// const query = {_id:req.params.id} +// const options = {new:true} +// const response = await Books.authors.findOneAndUpdate(query, req.body, options) - res.json({ status, response }) -}) +// res.json({ status, response }) +// }) -router.delete('/:bookId/authors/:id', async (req, res, next) => { - const status = 200 - const response = await Books.authors.findOneAndDelete({ _id: req.params.id }) +// router.delete('/:bookId/authors/:id', async (req, res, next) => { +// const status = 200 +// const response = await Books.authors.findOneAndDelete({ _id: req.params.id }) - res.json({ status, response }) -}) +// res.json({ status, response }) +// }) module.exports = router \ No newline at end of file