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
116 changes: 112 additions & 4 deletions app.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,38 @@ const { NODE_ENV = 'development', PORT = 5000 } = process.env
const express = require('express')
const { generate: generateId } = require('shortid')
const helpers = require('./src/helpers')
const _ = require('lodash');
const app = express()


if (NODE_ENV === 'development') app.use(require('morgan')('dev'))
app.use(require('body-parser').json())

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

// Use Mock Data if in development enviro
if (NODE_ENV === 'development') {
const data = require('./mock');
} else {
const data = {
fruits: [],
vegetables: []
}
}

/**
* Vegetable Routes
*/

app.get('/vegetables', (req, res, next) => {
const { vegetables } = data
res.json(vegetables)
if(req.query.name ) {
let matches = _.filter(vegetables, (vegetable) => {
return vegetable.name.indexOf(req.query.name) > -1;
});
res.json(matches)
} else {
res.json(vegetables)
}
})

app.get('/vegetables/:id', (req, res, next) => {
Expand All @@ -38,6 +57,95 @@ app.post('/vegetables', helpers.validate, (req, res, next) => {
res.status(201).json(vegetable)
})

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

app.put('/vegetables/:id', helpers.validate, (req, res, next) => {
const { vegetables } = data
const { id } = req.params
const vegetableIdx = vegetables.findIndex(vegetable => vegetable.id == id)
if (vegetableIdx < 0) {
const message = `Could not find vegetable with ID of ${id}`
next({ status: 404, message })
}
const vegetable = {id: req.params.id, name: req.body.name, price:req.body.price}
vegetables.splice(vegetableIdx,1,vegetable);
res.json(vegetable)
})


/**
* Fruit Routes
*/

app.get('/fruits', (req, res, next) => {
const { fruits } = data
if(req.query.name ) {
let matches = _.filter(fruits, (fruit) => {
return fruit.name.indexOf(req.query.name) > -1;
});
res.json(matches)
} else {
res.json(fruits)
}
})

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 })
Copy link
Contributor

Choose a reason for hiding this comment

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

You should return here, to avoid the error of:

Cannot set headers after they are sent to the client

}

res.json(fruit)
})

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

app.delete('/fruits/:id', (req, res, next) => {
const { fruits } = data
const { id } = req.params
const fruitIdx = fruits.findIndex(fruit => fruit.id == id)
if (fruitIdx < 0) {
const message = `Could not find fruit with ID of ${id}`
next({ status: 404, message })
}

const fruit = fruits.splice(fruitIdx,1);
res.json(fruit)
Copy link
Contributor

Choose a reason for hiding this comment

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

This will return an array as opposed to an object, which is what the instructions ask for.

})

app.put('/fruits/:id', helpers.validate, (req, res, next) => {
const { fruits } = data
const { id } = req.params
const fruitIdx = fruits.findIndex(fruit => fruit.id == id)
if (fruitIdx < 0) {
const message = `Could not find fruit with ID of ${id}`
next({ status: 404, message })
}
const fruit = {id: req.params.id, name: req.body.name, price:req.body.price}
fruits.splice(fruitIdx,1,fruit);
res.json(fruit)
})

app.use((req, res, next) => {
next({
status: 404,
Expand Down
38 changes: 38 additions & 0 deletions mock.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
data = {
fruits: [
{
"id": "m527HRMm5",
"name": "Red Cherry",
"price": ".49"
},
{
"id": "yCpYogn1Z",
"name": "Rainier Cherry",
"price": "1.29"
},
{
"id": "dX0iYFMDu",
"name": "Bing Cherry",
"price": ".99"
}
],
vegetables: [
{
"id": "7HRMm52m5",
"name": "Potato (5lb bag)",
"price": "3.49"
},
{
"id": "yCgn1ZpYo",
"name": "Potato",
"price": ".89"
},
{
"id": "FMDudX0iY",
"name": "Red Potato",
"price": ".89"
}
]
}

module.exports = data;
3 changes: 1 addition & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
"dependencies": {
"body-parser": "^1.19.0",
"express": "^4.17.1",
"lodash": "^4.17.11",
"morgan": "^1.9.1",
"shortid": "^2.2.14"
}
Expand Down
18 changes: 9 additions & 9 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,15 @@ This exercise will assess your ability to build upon an already existing Express

Improve upon and/or build the following routes as specified.

- [ ] [GET /vegetables?name=[partial-query]](#get-vegetablesnamepartial-query)
- [ ] [DELETE /vegetables/[id]](#delete-vegetablesid)
- [ ] [PUT /vegetables/[id]](#put-vegetablesid)
- [ ] [GET /fruits](#get-fruits)
- [ ] [GET /fruits?name=[partial-query]](#get-fruitsnamepartial-query)
- [ ] [GET /fruits/[id]](#get-fruitsid)
- [ ] [POST /fruits](#post-fruitsid)
- [ ] [DELETE /fruits/[id]](#delete-fruitsid)
- [ ] [PUT /fruits/[id]](#put-fruitsid)
- [X] [GET /vegetables?name=[partial-query]](#get-vegetablesnamepartial-query)
- [X] [DELETE /vegetables/[id]](#delete-vegetablesid)
- [X] [PUT /vegetables/[id]](#put-vegetablesid)
- [X] [GET /fruits](#get-fruits)
- [X] [GET /fruits?name=[partial-query]](#get-fruitsnamepartial-query)
- [X] [GET /fruits/[id]](#get-fruitsid)
- [X] [POST /fruits](#post-fruitsid)
- [X] [DELETE /fruits/[id]](#delete-fruitsid)
- [X] [PUT /fruits/[id]](#put-fruitsid)

### GET /vegetables?name=[partial-query]

Expand Down