Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
99 changes: 53 additions & 46 deletions api/routes/books.js
Original file line number Diff line number Diff line change
@@ -1,72 +1,79 @@
const router = require('express').Router()
const { generate: generateId } = require('shortid')
const Books = require('../model/books')
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you may have forgotten to add this path to the git repository. Routes look mostly correct but I cannot properly test your code without it. If you update and notify me I will reevaluate your grade.


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 })
})

Expand Down