diff --git a/app.js b/app.js index d2e92a4..070a138 100644 --- a/app.js +++ b/app.js @@ -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) => { @@ -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 }) + } + + 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) +}) + +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, diff --git a/mock.js b/mock.js new file mode 100644 index 0000000..719dc28 --- /dev/null +++ b/mock.js @@ -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; \ No newline at end of file diff --git a/package-lock.json b/package-lock.json index 29ea0a1..69bd894 100644 --- a/package-lock.json +++ b/package-lock.json @@ -2658,8 +2658,7 @@ "lodash": { "version": "4.17.11", "resolved": "https://registry.npmjs.org/lodash/-/lodash-4.17.11.tgz", - "integrity": "sha512-cQKh8igo5QUhZ7lg38DYWAxMvjSAKG0A8wGSVimP07SIUEK2UO+arSRKbRZWtelMtN5V0Hkwh5ryOto/SshYIg==", - "dev": true + "integrity": "sha512-cQKh8igo5QUhZ7lg38DYWAxMvjSAKG0A8wGSVimP07SIUEK2UO+arSRKbRZWtelMtN5V0Hkwh5ryOto/SshYIg==" }, "loose-envify": { "version": "1.4.0", diff --git a/package.json b/package.json index e6b4c3e..eb025a3 100644 --- a/package.json +++ b/package.json @@ -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" } diff --git a/readme.md b/readme.md index cc9fe47..9e28c82 100644 --- a/readme.md +++ b/readme.md @@ -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]