diff --git a/api/routes/books.js b/api/routes/books.js index 10a56a9..a74f76b 100644 --- a/api/routes/books.js +++ b/api/routes/books.js @@ -1,72 +1,79 @@ 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 + const response = await Books.find() + res.json({ status, response }) +}) -router.get('/', (req, res, next) => { +//works +router.get('/:bookId', async (req, res, next) => { const status = 200 - const response = books - + const response = await Books.findById(req.params.bookId) 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/authors', async (req,res,next) => { + const status = 200 + const response = await Books.findById(req.params.bookId).select('authors') res.json({ status, response }) }) -router.get('/:id', (req, res, next) => { +//doesn't work +router.get('/:bookId/authors/:authorId', async (req, res, next) => { const status = 200 - const response = books.find(({ id }) => id === req.params.id) + const response = await Books.findOne({authorId:req.params.authorId}) + res.json({ status, doc }) +}) +//works +router.post('/', (req, res, next) => { + const status = 201 + const response = Books.create(req.body) 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) +//works +router.post('/:bookId/authors', async (req, res, next) => { + const status = 201 + 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 }) +}) - books.splice(index, 1, response) - +//works +router.put('/:bookId', async (req, res, next) => { + const status = 200 + 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 }) }) -router.delete('/:id', (req, res, next) => { +//update an author of a certain book +router.put('/:bookId/authors/:authorId', 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({ authorId:req.params.authorId }, + { name: req.body.name, dob:req.body.dob }, + { 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 + console.log(req.params.authorId); + const response = Books.findOneAndDelete({authorId:req.params.authorId}) res.json({ status, response }) })