From 4c77d56e736435d081c7b2822bc9f18e105a0735 Mon Sep 17 00:00:00 2001 From: Laura_MS Date: Tue, 16 Jul 2019 22:02:11 -0700 Subject: [PATCH 1/2] adding and updating routes --- api/routes/books.js | 74 +++++++++++++++++++++++++++++++-------------- 1 file changed, 52 insertions(+), 22 deletions(-) diff --git a/api/routes/books.js b/api/routes/books.js index 10a56a9..b133900 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('../model/books') const books = [ { @@ -26,47 +27,76 @@ const books = [ } ]; -router.get('/', (req, res, next) => { +//works +router.get('/', async (req, res, next) => { const status = 200 - const response = books - + const response = await Books.find() res.json({ status, response }) }) -router.post('/', (req, res, next) => { - const status = 201 - - books.push({ id: generateId(), ...req.body }) - const response = books - +//works +router.get('/:bookId', async (req, res, next) => { + const status = 200 + const response = await Books.findById(req.params.bookId) res.json({ status, response }) }) -router.get('/:id', (req, res, next) => { +//works +router.get('/:bookId/authors', async (req,res,next) => { const status = 200 - const response = books.find(({ id }) => id === req.params.id) - + const response = await Books.findById(req.params.bookId).select('authors') res.json({ status, response }) }) -router.put('/:id', (req, res, next) => { +//doesn't work +router.get('/:bookId/authors/:authorId', 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) + const response = await Books.findOne({_id:req.params.bookId}) + res.json({ status, doc }) +}) - books.splice(index, 1, response) - +//works +router.post('/', (req, res, next) => { + const status = 201 + const response = Books.create(req.body) + res.json({ status, response }) +}) + +//doesn't work +router.post('/:bookId/authors', async (req, res, next) => { + const status = 201 + const response = await Books.findByIdAndUpdate(req.params.bookId,req.body) res.json({ status, response }) }) -router.delete('/:id', (req, res, next) => { +//works +router.put('/:bookId', async (req, res, next) => { const status = 200 - const response = books.find(({ id }) => id === req.params.id) - const index = books.indexOf(response) + const response = await Books.findOneAndUpdate({ _id: req.params.bookId }, { title: req.body.title, published:req.body.published, authors:req.body.authors }, + { new:true, omitUndefined:true }) + res.json({ status, response }) +}) + +//update an author of a certain book +router.put('/:bookId/authors/:authorId', async (req, res, next) => { + const status = 200 + const response = await Books.findOneAndUpdate({ _id:req.params.bookId }, + { title: req.body.title, published:req.body.published, authors:req.body.authors }, + { new:true, omitUndefined:true }) + res.json({ status, response }) +}) - books.splice(index, 1) +//works +router.delete('/:bookId', async (req, res, next) => { + const status = 200 + const response = await Books.findOneAndDelete(req.params.bookId) + res.json({ status, response }) +}) +//delete an author of a certain book +router.delete('/:bookId/authors/:authorId', (req, res, next) => { + const status = 200 + const response = Books.findOneAndDelete({ authorId:req.params.authorId}) res.json({ status, response }) }) From 4cafa3f4f56a3edfec5e044fea523e03431cdcea Mon Sep 17 00:00:00 2001 From: Laura_MS Date: Tue, 16 Jul 2019 23:01:47 -0700 Subject: [PATCH 2/2] fixed a couple more --- api/routes/books.js | 41 +++++++++-------------------------------- 1 file changed, 9 insertions(+), 32 deletions(-) diff --git a/api/routes/books.js b/api/routes/books.js index b133900..a74f76b 100644 --- a/api/routes/books.js +++ b/api/routes/books.js @@ -2,31 +2,6 @@ const router = require('express').Router() const { generate: generateId } = require('shortid') const Books = require('../model/books') -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' - } - ] - } -]; - //works router.get('/', async (req, res, next) => { const status = 200 @@ -51,7 +26,7 @@ router.get('/:bookId/authors', async (req,res,next) => { //doesn't work router.get('/:bookId/authors/:authorId', async (req, res, next) => { const status = 200 - const response = await Books.findOne({_id:req.params.bookId}) + const response = await Books.findOne({authorId:req.params.authorId}) res.json({ status, doc }) }) @@ -62,17 +37,18 @@ router.post('/', (req, res, next) => { res.json({ status, response }) }) -//doesn't work +//works router.post('/:bookId/authors', async (req, res, next) => { const status = 201 - const response = await Books.findByIdAndUpdate(req.params.bookId,req.body) + console.log(req.body.name, req.body.dob); + const response = await Books.findByIdAndUpdate(req.params.bookId, { authors: { name:req.body.name, dob:req.body.dob } } ) res.json({ status, response }) }) //works router.put('/:bookId', async (req, res, next) => { const status = 200 - const response = await Books.findOneAndUpdate({ _id: req.params.bookId }, { title: req.body.title, published:req.body.published, authors:req.body.authors }, + const response = await Books.findOnAndUpdate({ _id: req.params.bookId }, { title: req.body.title, published:req.body.published, authors:req.body.authors }, { new:true, omitUndefined:true }) res.json({ status, response }) }) @@ -80,8 +56,8 @@ router.put('/:bookId', async (req, res, next) => { //update an author of a certain book router.put('/:bookId/authors/:authorId', async (req, res, next) => { const status = 200 - const response = await Books.findOneAndUpdate({ _id:req.params.bookId }, - { title: req.body.title, published:req.body.published, authors:req.body.authors }, + const response = await Books.findOneAndUpdate({ authorId:req.params.authorId }, + { name: req.body.name, dob:req.body.dob }, { new:true, omitUndefined:true }) res.json({ status, response }) }) @@ -96,7 +72,8 @@ router.delete('/:bookId', async (req, res, next) => { //delete an author of a certain book router.delete('/:bookId/authors/:authorId', (req, res, next) => { const status = 200 - const response = Books.findOneAndDelete({ authorId:req.params.authorId}) + console.log(req.params.authorId); + const response = Books.findOneAndDelete({authorId:req.params.authorId}) res.json({ status, response }) })