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
249 changes: 213 additions & 36 deletions app.js
Original file line number Diff line number Diff line change
@@ -1,54 +1,231 @@
const { NODE_ENV = 'development', PORT = 5000 } = process.env
const express = require('express')
const { generate: generateId } = require('shortid')
const helpers = require('./src/helpers')
const app = express()
const { NODE_ENV = "development", PORT = 5000 } = process.env;
const express = require("express");
const { generate: generateId } = require("shortid");
const helpers = require("./src/helpers");
const app = express();

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

const data = {
fruits: [],
vegetables: []
}
fruits: [
{
id: "ad_-OwhQA",
name: "apple",
price: "2.99"
},
{
id: "DLU6Rjfon",
name: "banana",
price: "1.99"
},
{
id: "3YVN-Kn_z",
name: "kiwis",
price: "4.13"
},
{
id: "0BtTp4JRn",
name: "melon",
price: "3.33"
}
],
vegetables: [
{
id: "xg7hs7vlV",
name: "acorn squash",
price: "1.33"
},
{
id: "0k8RBDjm0",
name: "artichoke",
price: "1.23"
},
{
id: "Vt2Y8l3IE",
name: "basil",
price: "1.93"
},
{
id: "-iWrxvN8G",
name: "cabbage",
price: "2.98"
}
]
};

app.get('/vegetables', (req, res, next) => {
const { vegetables } = data
res.json(vegetables)
})
app.get("/vegetables", (req, res, next) => {
const { vegetables } = data;

app.get('/vegetables/:id', (req, res, next) => {
const { vegetables } = data
const { id } = req.params
const vegetable = vegetables.find(veggie => veggie.id === id)
const name = req.query.name;

let myList = [];

if (!name) {
return res.json(vegetables);
}
console.log(name);

myList = vegetables.filter(veggie =>
veggie.name.includes(name.substring(1, req.query.name.length - 1))
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.

Interesting. I assume this was to get around the quotations. This will work but also return possibly more results than expected.

);

res.json(myList);
});
/******************************************** */
app.get("/vegetables/:id", (req, res, next) => {
const { vegetables } = data;
const id = req.params.id;
let vegetable = vegetables.find(veggie => veggie.id === id);

console.log(id);
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.

Remove console.log() statements before committing!


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

res.json(vegetable);
});
/******************************************** */
app.delete("/vegetables/:id", (req, res, next) => {
const { vegetables } = data;
const { id } = req.params;
let vegetable = vegetables.find(veggie => veggie.id === id);

//console.log(id);

if (!vegetable) {
const message = `DELETE: Could not find vegetable with ID of ${id}`;
next({ status: 404, message });
}
let filtered = vegetables.filter(
(x = (value, index, arr) => {
return value.id !== id;
})
);
//console.log(data.vegetables);
//console.log("===========================");
data.vegetables = filtered;
//console.log(data.vegetables);
res.status(200).json(vegetable);
});
/******************************************** */
app.put("/vegetables/:id", helpers.validate, (req, res, next) => {
const { vegetables } = data;
const { id } = req.params;
const { name, price } = req.body;
const vegetable = vegetables.find(veggie => veggie.id === id);

if (!vegetable) {
const message = `Could not find vegetable with ID of ${id}`
next({ status: 404, message })
const message = `PUT: Could not find ${name} with ID of ${id}`;
next({ status: 404, message });
}
vegetable.name = name;
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'm getting an error here of TypeError: Cannot set property 'name' of undefined. My guess is you need to return here to stop the rest of the function from executing.

vegetable.price = price;
res.status(201).json(vegetable);
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.

Per the instructions, this should be a 200.

});

res.json(vegetable)
})
/******************************************** */
app.post("/vegetables", helpers.validate, (req, res, next) => {
const { vegetables } = data;
const vegetable = { id: generateId(), ...req.body };

app.post('/vegetables', helpers.validate, (req, res, next) => {
const { vegetables } = data
const vegetable = { id: generateId(), ...req.body }
vegetables.push(vegetable);
res.status(201).json(vegetable);
});
/******************************************** */
app.get("/fruits", (req, res, next) => {
const { fruits } = data;

vegetables.push(vegetable)
res.status(201).json(vegetable)
})
const name = req.query.name;

let myList = [];

if (!name) {
return res.json(fruits);
}
console.log(name);

myList = fruits.filter(fruit =>
fruit.name.includes(name.substring(1, req.query.name.length - 1))
);

res.json(myList);
});
/******************************************** */
app.get("/fruits/:id", (req, res, next) => {
const { fruits } = data;
const id = req.params.id;
let fruit = fruits.find(fruit => fruit.id === id);

console.log(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;
let fruit = fruits.find(fruit => fruit.id === id);

//console.log(id);

if (!fruit) {
const message = `DELETE: Could not find fruit with ID of ${id}`;
next({ status: 404, message });
}
let filtered = fruits.filter(
(x = (value, index, arr) => {
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.

What is this x = bit?

return value.id !== id;
})
);
//console.log(data.vegetables);
//console.log("===========================");
data.fruits = filtered;
//console.log(data.vegetables);
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.

These kind of statements should be removed as well.

res.status(200).json(fruit);
});
/******************************************** */
app.put("/fruits/:id", helpers.validate, (req, res, next) => {
const { fruits } = data;
const { id } = req.params;
const { name, price } = req.body;
const fruit = fruits.find(fruit => fruit.id === id);

if (!fruit) {
const message = `PUT: Could not find ${name} with ID of ${id}`;
next({ status: 404, message });
}
fruit.name = name;
fruit.price = price;
res.status(201).json(fruit);
});
/***************************************************************************************************** */
app.use((req, res, next) => {
next({
status: 404,
message: `Could not ${req.method} ${req.path}`
})
})
message: `Could not ${req.method} ${req.path}`
});
});

app.use((err, req, res, next) => {
const { message, status } = err
res.status(status).json({ message })
})
const { message, status } = err;
res.status(status).json({ message });
});

const listener = () => console.log(`Listening on Port ${PORT}!`)
app.listen(PORT, listener)
const listener = () => console.log(`Listening on Port ${PORT}!`);
app.listen(PORT, listener);
2 changes: 1 addition & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ _Note: This is not a new route!_

**Incorrect Response Body Example**
```json
{ "message": "Could not find banana with ID of 1" }
{ "message": "Could not find fruit with ID of 1" }
```

### PUT /fruits/[id]
Expand Down
23 changes: 13 additions & 10 deletions src/helpers.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
const REQUIRED_KEYS = [ 'name', 'price' ]
const REQUIRED_KEYS = ["name", "price"];
const validate = (req, res, next) => {
const error = { status: 400, message: 'Bad request' }
if (!req.body) next(error)
const error = { status: 400, message: "Bad request" };
//const error1 = { status: 400, message: "xxxxxx" };
if (!req.body) return next(error);

const hasAllKeys = REQUIRED_KEYS.every(key => req.body[key])
if (!hasAllKeys) next(error)
const hasAllKeys = REQUIRED_KEYS.every(key => req.body[key]);
if (!hasAllKeys) return next(error);

const noExtraKeys = Object.keys(req.body).every(key => REQUIRED_KEYS.includes(key))
if (!noExtraKeys) next(error)
const noExtraKeys = Object.keys(req.body).every(key =>
REQUIRED_KEYS.includes(key)
);
if (!noExtraKeys) return next(error);

next()
}
next();
};

module.exports = { validate }
module.exports = { validate };