-
Notifications
You must be signed in to change notification settings - Fork 29
homework complete #14
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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 }) | ||
| } | ||
|
|
||
|
|
@@ -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 } | ||
| 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' | ||
| }) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Having two I think this is also causing some problems later on to make the |
||
| }) | ||
|
|
||
| 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) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Remove |
||
|
|
||
| res.json(fruit) | ||
|
|
||
| res.status(200).json({ | ||
| message: 'Deleted message' | ||
| }) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) => { | ||
|
|
||
There was a problem hiding this comment.
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 ONEroute. Right now, this route is only returning the ID because of it.