-
Notifications
You must be signed in to change notification settings - Fork 29
Shahab-exercise-express #13
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 |
|---|---|---|
| @@ -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)) | ||
| ); | ||
|
|
||
| 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); | ||
|
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. Remove |
||
|
|
||
| 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; | ||
|
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'm getting an error here of |
||
| vegetable.price = price; | ||
| res.status(201).json(vegetable); | ||
|
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. Per the instructions, this should be a |
||
| }); | ||
|
|
||
| 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) => { | ||
|
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. What is this |
||
| return value.id !== id; | ||
| }) | ||
| ); | ||
| //console.log(data.vegetables); | ||
| //console.log("==========================="); | ||
| data.fruits = filtered; | ||
| //console.log(data.vegetables); | ||
|
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. 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); | ||
| 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 }; |
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.
Interesting. I assume this was to get around the quotations. This will work but also return possibly more results than expected.