-
Notifications
You must be signed in to change notification settings - Fork 29
J.Sorge HW2 - Express #26
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 |
|---|---|---|
|
|
@@ -10,11 +10,71 @@ app.use(require('body-parser').json()) | |
| const data = { | ||
| fruits: [], | ||
| vegetables: [] | ||
| //fruits: [{ "id": "14r5y6", "name": "apple", "price": "0.49" }], | ||
| //vegetables: [{ "id": "y7474u", "name": "cilantro", "price": "1.00" }, | ||
| //{ "id": "8ue38u", "name": "onion", "price": "2.00" }] | ||
| } | ||
|
|
||
| // IE compatibility polyfill for string.prototype.includes() | ||
| // Sunho Hong https://stackoverflow.com/a/39744409 | ||
| if (!Array.prototype.includes) { | ||
| Object.defineProperty(Array.prototype, "includes", { | ||
| enumerable: false, | ||
| value: function(obj) { | ||
| var newArr = this.filter(function(el) { | ||
| return el == obj; | ||
| }); | ||
| return newArr.length > 0; | ||
| } | ||
| }); | ||
| } | ||
| // IE compatibility pollyfill for string.prototype.filter | ||
| //http://independent-software.com/extending-the-javascript-array-prototype-with-polyfills.html | ||
| if (!Array.prototype.filter) { | ||
| Array.prototype.filter = function (callbackfn, /*optional*/ thisArg) { | ||
| var k, len, result = []; | ||
|
|
||
| // Method cannot be run on an array that does not exist. | ||
| if (this == null) { | ||
| throw new TypeError('this is null or not defined'); | ||
| } | ||
|
|
||
| // The callback must be a function. | ||
| if (typeof callbackfn !== 'function') { | ||
| throw new TypeError(); | ||
| } | ||
|
|
||
| // Loop through array. | ||
| len = this.length; | ||
| k = 0; | ||
| while (k < len) { | ||
| if (k in this) { | ||
| // For each element, if callback returns truthy, add it to | ||
| // result array. | ||
| if (callbackfn.call(thisArg, this[k], k, this)) { | ||
| result.push(this[k]); | ||
| } | ||
| } | ||
| k = k + 1; | ||
| } | ||
| return result; | ||
| }; | ||
| } | ||
|
|
||
| /* Vegetables */ | ||
| app.get('/vegetables', (req, res, next) => { | ||
| const { vegetables } = data | ||
| res.json(vegetables) | ||
| const { name } = req.query | ||
| res.status(200) | ||
|
|
||
| // if no query, return vegetables' data | ||
| if (!name) { | ||
| res.json(vegetables) | ||
| } else { // query exists | ||
| // Filter data for vegetable name | ||
| const veg = vegetables.filter( el => el.name.includes(name)) | ||
| res.json(veg) | ||
| } | ||
| }) | ||
|
|
||
| app.get('/vegetables/:id', (req, res, next) => { | ||
|
|
@@ -34,10 +94,122 @@ app.post('/vegetables', helpers.validate, (req, res, next) => { | |
| const { vegetables } = data | ||
| const vegetable = { id: generateId(), ...req.body } | ||
|
|
||
| vegetables.push(vegetable) | ||
| vegetables.push(vegetable) | ||
| res.status(201).json(vegetable) | ||
| }) | ||
|
|
||
| app.delete('/vegetables/:id', ( req, res, next) => { | ||
| const { vegetables } = data | ||
| const { id } = req.params | ||
|
|
||
| const veg = vegetables.filter(el => el.id.includes(id))[0] | ||
|
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.
|
||
| if (!veg) { | ||
| const message = `Could not find vegetable with ID of ${id}` | ||
| next({ status: 404, 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. You'll want to add a |
||
| } | ||
| const vegIndex = vegetables.indexOf(veg) | ||
| vegetables.splice(vegIndex,1) | ||
| res.status(200).json(veg) | ||
| }) | ||
|
|
||
| app.put('/vegetables/:id', helpers.validate, (req, res, next) => { | ||
|
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. I think |
||
| const { vegetables } = data | ||
| const { id } = req.params | ||
|
|
||
| const veg = vegetables.filter(el => el.id.includes(id))[0] | ||
| if (!veg) { | ||
| const message = `Could not find vegetable with ID of ${id}` | ||
| next({ status: 404, message }) | ||
| } | ||
| const { name, price } = req.body | ||
| if (!name || !price) { | ||
| const message = `Bad request.` | ||
| next({ status: 400, message }) | ||
| } | ||
| veg.name = name | ||
| veg.price = price | ||
| res.status(200).json(veg) | ||
| }) | ||
|
|
||
|
|
||
| /* Fruits */ | ||
| app.get('/fruits', (req, res, next) => { | ||
| const { fruits } = data | ||
| const { name } = req.query | ||
| res.status(200) | ||
|
|
||
| // if no query, return fruits' data | ||
| if (!name) { | ||
| res.json(fruits) | ||
| } else { // query exists | ||
| // Filter data for fruit name | ||
| const fruit = fruits.filter( el => el.name.includes(name)) | ||
| res.json(fruit) | ||
| } | ||
| }) | ||
|
|
||
| app.get('/fruits/:id', (req, res, next) => { | ||
| const { fruits } = data | ||
| const { id } = req.params | ||
| const fruit = fruits.find(el => el.id === id) | ||
|
|
||
| if (!fruit) { | ||
| const message = `Could not find fruit with ID of ${id}` | ||
| next({ status: 404, message }) | ||
| } | ||
|
|
||
| res.status(200).json(fruit) | ||
| }) | ||
|
|
||
| app.post('/fruits', helpers.validate, (req, res, next) => { | ||
| const { fruits } = data | ||
| const { name, price } = req.body | ||
| if (!name || !price) { | ||
| const message = `Bad request.` | ||
| next({ status: 400, message }) | ||
| } | ||
| 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 fruit = fruits.filter(el => el.id.includes(id))[0] | ||
| console.log(fruit) | ||
|
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. Please remove |
||
| if (!fruit) { | ||
| const message = `Could not find a fruit with ID of ${id}` | ||
| next({ status: 404, message }) | ||
| } | ||
| const fruitIndex = fruits.indexOf(fruit) | ||
| fruits.splice(fruitIndex,1) | ||
| res.status(200).json(fruit) | ||
| }) | ||
|
|
||
| app.put('/fruits/:id', helpers.validate, (req, res, next) => { | ||
| const { fruits } = data | ||
| const { id } = req.params | ||
|
|
||
| const fruit = fruits.filter(el => el.id.includes(id))[0] | ||
| if (!fruit) { | ||
| const message = `Could not find fruit with ID of ${id}` | ||
| next({ status: 404, message }) | ||
| } | ||
| const { name, price } = req.body | ||
| if (!name || !price) { | ||
| const message = `Bad request.` | ||
| next({ status: 400, message }) | ||
| } | ||
| fruit.name = name | ||
| fruit.price = price | ||
| res.status(200).json(fruit) | ||
| }) | ||
|
|
||
|
|
||
| // Default responses | ||
| app.use((req, res, next) => { | ||
| next({ | ||
| status: 404, | ||
|
|
@@ -51,4 +223,4 @@ app.use((err, req, res, next) => { | |
| }) | ||
|
|
||
| const listener = () => console.log(`Listening on Port ${PORT}!`) | ||
| app.listen(PORT, listener) | ||
| app.listen(PORT, listener) | ||
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.
Both of these should not be necessary. Were you having issues calling either of these functions?