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
132 changes: 126 additions & 6 deletions app.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,22 +8,31 @@ if (NODE_ENV === 'development') app.use(require('morgan')('dev'))
app.use(require('body-parser').json())

const data = {
fruits: [],
vegetables: []
fruits: [{ "id": "p7pzzZxCX", "name": "banana", "price": "0.79" }],
vegetables: [ { "id": "AEdvQm9t_", "name": "green pepper", "price": "0.99" },
{ "id": "JA_dIbXLW", "name": "red pepper", "price": "0.99" }]
}

app.get('/vegetables', (req, res, next) => {
const { vegetables } = data
res.json(vegetables)
const { vegetables } = data;
const { name } = req.query;
const foundVegetables = vegetables.filter(veggie => veggie.name.includes(name));

if(!name) {
res.json(vegetables)
}
else {
res.json(foundVegetables);
}
})

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

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

Expand All @@ -36,6 +45,117 @@ app.post('/vegetables', helpers.validate, (req, res, next) => {

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

res.status(200).json({
message: 'Handling POST requests to /vegetables'
})
})

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

const vegetableIndex = vegetables.findIndex(veggie => veggie.id == id)
if (vegetableIndex >= 0) {
res.json(vegetables.splice(vegetableIndex, 1)[0])
} else {
const message = `Could not find vegetable with ID of ${id}`
next({ status: 404, message})
}
})

app.put('/vegetables/:id', helpers.validate, (req, res, next) => {
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 })
}
else {
const vegIndex = vegetables.indexOf(vegetable);
vegetables[vegIndex] = { id: id, ...req.body }
res.status(200).json(vegetables[vegIndex]);
}

})

//Fruits
app.get('/fruits', (req, res, next) => {
const { fruits } = data;
const { name } = req.query;
const foundFruits = fruits.filter(fruit => fruit.name.includes(name));

if(!name) {
res.json(fruits)
}
else {
res.json(foundFruits);
}
})

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

const fruit = fruits.find(fruit => fruit.id === id);

if (!fruit) {
const message = `Could not find fruit with ID of ${id}`
next({ status: 404, message })
}
else {
const fruitIndex = fruits.indexOf(fruit);
fruits[fruitIndex] = { id: id, ...req.body }
Copy link
Contributor

Choose a reason for hiding this comment

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

You shouldn't need to construct a new object inside a GET ONE route. Right now, this route is only returning the ID because of it.

res.status(200).json(fruits[fruitIndex]);
}
})

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

fruits.push(fruit)
res.status(201).json(fruit)

res.status(200).json({
message: 'Handling POST requests to /fruits'
})
Copy link
Contributor

Choose a reason for hiding this comment

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

Having two res.json()s will cause the following error to surface:

Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client

I think this is also causing some problems later on to make the GET ALL /fruits request.

})

app.delete('/fruits/:id', (req, res, next) => {
const { fruits } = data
const { id } = req.params

const fruitIndex = fruits.findIndex(fruit => fruit.id == id)
if (fruitIndex >= 0) {
res.json(fruits.splice(fruitIndex, 1)[0])
} else {
const message = `Could not find fruit with ID of ${id}`
next({ status: 404, message})
}
})

app.put('/fruits/:id', helpers.validate, (req, res, next) => {
const { fruits } = data
const { id } = req.params
const fruit = fruits.find(fruit => fruit.id === id)

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

const updatedFruits = fruits.filter(fruits => fruits.id !== id)

console.log(updatedFruits)
Copy link
Contributor

Choose a reason for hiding this comment

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

Remove console.log() statements before committing, please!


res.json(fruit)

res.status(200).json({
message: 'Deleted message'
})
Copy link
Contributor

Choose a reason for hiding this comment

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

Same thing here, you should only be responding once per route (unless you have some kind of if/else)

})

app.use((req, res, next) => {
Expand Down