Skip to content
Open
Show file tree
Hide file tree
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
68 changes: 45 additions & 23 deletions app.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,47 +8,69 @@ if (NODE_ENV === 'development') app.use(require('morgan')('dev'))
app.use(require('body-parser').json())

const data = {
fruits: [],
vegetables: []
fruits: [],
vegetables: []
}

app.get('/vegetables', (req, res, next) => {
const { vegetables } = data
res.json(vegetables)
const { vegetables } = data
if (req.query) {
let name = req.query.name
const vegetable = vegetables.find(veggie => veggie.name === name)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

In this case, we want to be able to find the query string value inside of a name. For example, searching for ?name=pepper should return 'Red Peppers' and 'Green Peppers'. This will only do a direct comparison.

res.json(vegetable)
} else {
res.json(vegetables)
}
})




app.get('/vegetables/:id', (req, res, next) => {
const { vegetables } = data
const { id } = req.params
const vegetable = vegetables.find(veggie => veggie.id === id)
const { vegetables } = data
const { id } = req.params
const vegetable = vegetables.find(veggie => veggie.id === id)

if (!vegetable) {
const message = `Could not find vegetable with ID of ${id}`
next({ status: 404, message })
}

res.json(vegetable)
})

if (!vegetable) {
const message = `Could not find vegetable with ID of ${id}`
next({ status: 404, message })
}
app.delete('/vegetables/:id', (req, res, next) => {
const { vegetables } = data
const { id } = req.params
const vegetable = vegetables.indexOf(veggie => veggie.id === id)
if (vegetable === -1) {
const message = `Could not find vegetable with ID of ${id}`
next({ status: 404, message })
}
data.splice(vegetable, 1)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

It doesn't look like .splice() is working here. I'm getting the error:

TypeError: data.splice is not a function

res.status(200).json(vegetable)

res.json(vegetable)
})

app.post('/vegetables', helpers.validate, (req, res, next) => {
const { vegetables } = data
const vegetable = { id: generateId(), ...req.body }
const { vegetables } = data
const vegetable = { id: generateId(), ...req.body }

vegetables.push(vegetable)
res.status(201).json(vegetable)
vegetables.push(vegetable)
res.status(201).json(vegetable)
})

app.use((req, res, next) => {
next({
status: 404,
message: `Could not ${req.method} ${req.path}`
})
next({
status: 404,
message: `Could not ${req.method} ${req.path}`
})
})

app.use((err, req, res, next) => {
const { message, status } = err
res.status(status).json({ message })
const { message, status } = err
res.status(status).json({ message })
})

const listener = () => console.log(`Listening on Port ${PORT}!`)
app.listen(PORT, listener)
app.listen(PORT, listener)
18 changes: 9 additions & 9 deletions src/helpers.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
const REQUIRED_KEYS = [ 'name', 'price' ]
const REQUIRED_KEYS = ['name', 'price']
const validate = (req, res, next) => {
const error = { status: 400, message: 'Bad request' }
if (!req.body) next(error)
const error = { status: 400, message: 'Bad request' }
if (!req.body) next(error)

const hasAllKeys = REQUIRED_KEYS.every(key => req.body[key])
if (!hasAllKeys) next(error)
const hasAllKeys = REQUIRED_KEYS.every(key => req.body[key])
if (!hasAllKeys) next(error)

const noExtraKeys = Object.keys(req.body).every(key => REQUIRED_KEYS.includes(key))
if (!noExtraKeys) next(error)
const noExtraKeys = Object.keys(req.body).every(key => REQUIRED_KEYS.includes(key))
if (!noExtraKeys) next(error)

next()
next()
}

module.exports = { validate }
module.exports = { validate }