From abd7c29d650b399c5ef8509d6dc8f1125c334d26 Mon Sep 17 00:00:00 2001 From: "[jiansorge]" <[ji@nsorge.com]> Date: Sun, 4 Aug 2019 18:36:37 -0700 Subject: [PATCH 1/3] finshed homework 2 --- app.js | 191 +++++++++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 186 insertions(+), 5 deletions(-) diff --git a/app.js b/app.js index d2e92a4..9982c5a 100644 --- a/app.js +++ b/app.js @@ -8,13 +8,76 @@ if (NODE_ENV === 'development') app.use(require('morgan')('dev')) app.use(require('body-parser').json()) const data = { - fruits: [], - vegetables: [] + // 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" }] } + //{ "id": "i86y3r", "name": "berries ", "price": 0.79 }] + + // 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 */ +// [GET /vegetables?name=[partial-query]](#get-vegetablesnamepartial-query) 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 +97,128 @@ 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] + if (!veg) { + const message = `Could not find vegetable with ID of ${id}` + next({ status: 404, message }) + } + const vegIndex = vegetables.indexOf(veg) + vegetables.splice(vegIndex,1) + res.status(200).json(veg) +}) + +app.put('/vegetables/:id', helpers.validate, (req, res, next) => { + 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) +}) + + + + + // //Make vegetable name lowercase + // const veg = id.toLowerCase() + +/* Fruits */ +app.get('/fruits', (req, res, next) => { + const { fruits } = data + const { name } = req.query + res.status(200) + + // if no query, return vegetables' data + if (!name) { + res.json(fruits) + } else { // query exists + // Filter data for vegetable 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 vegetable 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) + 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 +232,4 @@ app.use((err, req, res, next) => { }) const listener = () => console.log(`Listening on Port ${PORT}!`) -app.listen(PORT, listener) +app.listen(PORT, listener) \ No newline at end of file From a41fec306ac63ee3b5eae1f8d897f0fed21996fc Mon Sep 17 00:00:00 2001 From: "[jiansorge]" <[ji@nsorge.com]> Date: Sun, 4 Aug 2019 18:41:35 -0700 Subject: [PATCH 2/3] cleaned comments --- app.js | 105 ++++++++++++++++++++++++++------------------------------- 1 file changed, 48 insertions(+), 57 deletions(-) diff --git a/app.js b/app.js index 9982c5a..ec42f55 100644 --- a/app.js +++ b/app.js @@ -8,63 +8,60 @@ if (NODE_ENV === 'development') app.use(require('morgan')('dev')) 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" }] + 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" }] } - //{ "id": "i86y3r", "name": "berries ", "price": 0.79 }] - - - // 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(); + +// 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; } - - // 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]); - } + }); +} +// 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; - }; - } + k = k + 1; + } + return result; + }; +} /* Vegetables */ -// [GET /vegetables?name=[partial-query]](#get-vegetablesnamepartial-query) app.get('/vegetables', (req, res, next) => { const { vegetables } = data const { name } = req.query @@ -135,11 +132,6 @@ app.put('/vegetables/:id', helpers.validate, (req, res, next) => { }) - - - // //Make vegetable name lowercase - // const veg = id.toLowerCase() - /* Fruits */ app.get('/fruits', (req, res, next) => { const { fruits } = data @@ -169,7 +161,6 @@ app.get('/fruits/:id', (req, res, next) => { res.status(200).json(fruit) }) - app.post('/fruits', helpers.validate, (req, res, next) => { const { fruits } = data const { name, price } = req.body From c3507c01c2306e24719b11cd0b00eacd6ebc9935 Mon Sep 17 00:00:00 2001 From: "[jiansorge]" <[ji@nsorge.com]> Date: Sun, 4 Aug 2019 18:43:19 -0700 Subject: [PATCH 3/3] cleaned comments --- app.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/app.js b/app.js index ec42f55..15cfd62 100644 --- a/app.js +++ b/app.js @@ -138,11 +138,11 @@ app.get('/fruits', (req, res, next) => { const { name } = req.query res.status(200) - // if no query, return vegetables' data + // if no query, return fruits' data if (!name) { res.json(fruits) } else { // query exists - // Filter data for vegetable name + // Filter data for fruit name const fruit = fruits.filter( el => el.name.includes(name)) res.json(fruit) } @@ -154,7 +154,7 @@ app.get('/fruits/:id', (req, res, next) => { const fruit = fruits.find(el => el.id === id) if (!fruit) { - const message = `Could not find vegetable with ID of ${id}` + const message = `Could not find fruit with ID of ${id}` next({ status: 404, message }) }