Skip to content
Open
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
178 changes: 175 additions & 3 deletions app.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
};
}
Copy link
Copy Markdown
Contributor

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?


/* 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) => {
Expand All @@ -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]
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

.find() is a good substitute here.

if (!veg) {
const message = `Could not find vegetable with ID of ${id}`
next({ status: 404, message })
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

You'll want to add a return statement here as well, otherwise the next few lines will also get executed.

}
const vegIndex = vegetables.indexOf(veg)
vegetables.splice(vegIndex,1)
res.status(200).json(veg)
})

app.put('/vegetables/:id', helpers.validate, (req, res, next) => {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I think helpers.validate should perform the same function as the lines you have below.

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

Choose a reason for hiding this comment

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

Please remove console.log() statements before committing!

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,
Expand All @@ -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)