-
Notifications
You must be signed in to change notification settings - Fork 29
Week 2 exercise completed #25
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,216 @@ | ||
| 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: "I6d-k6a60", | ||
| name: "banana", | ||
| price: "0.79" | ||
| }, | ||
| { | ||
| id: "p-Yp63hCY", | ||
| name: "apple", | ||
| price: "2.00" | ||
| }, | ||
| { | ||
| id: "vtrDZT-Rf", | ||
| name: "pear", | ||
| price: "1.00" | ||
| } | ||
| ], | ||
| vegetables: [ | ||
| { | ||
| id: "OIDlPI3hm", | ||
| name: "onions", | ||
| price: 0.99 | ||
| }, | ||
| { | ||
| id: "oD73STQ06", | ||
| name: "tomato", | ||
| price: 2.99 | ||
| }, | ||
| { | ||
| id: "IfxG9SHKZ", | ||
| name: "cucumber", | ||
| price: 1.99 | ||
| } | ||
| ] | ||
| }; | ||
|
|
||
| app.get('/vegetables', (req, res, next) => { | ||
| const { vegetables } = data | ||
| res.json(vegetables) | ||
| }) | ||
| // 01 - GET /vegetables?name=[partial-query] | ||
| // =============================================== | ||
| app.get("/vegetables", (req, res, next) => { | ||
| if (req.query) { | ||
|
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. This won't work, unfortunately. |
||
| const { vegetables } = data; | ||
| const { name } = req.query; | ||
| const result = vegetables.filter(veggie => veggie.name.includes(name)); | ||
| res.json(result); | ||
| } else { | ||
| const { vegetables } = data; | ||
| res.json(vegetables); | ||
| } | ||
| }); | ||
|
|
||
| // 2 - DELETE/vegetables/[id] | ||
| // ======================== | ||
| app.delete("/vegetables/:id", (req, res, next) => { | ||
| const { vegetables } = data; | ||
| const { id } = req.params; | ||
| const index = vegetables.findIndex(veggie => veggie.id === id); | ||
| vegetable = vegetables.splice(index, 1); | ||
| if (index == -1) { | ||
| const message = `Could not find vegetable with ID of ${id}`; | ||
| next({ status: 404, message }); | ||
| } | ||
| res.json(vegetable); | ||
| }); | ||
|
|
||
| // 3 - PUT/vegetables/[id] | ||
| //========================= | ||
|
|
||
| app.put("/vegetables/:id", (req, res, next) => { | ||
| const { vegetables } = data; | ||
| const { id } = req.params; | ||
| const vegetable = vegetables.find(veggie => veggie.id === id); | ||
| if (vegetable) { | ||
| vegetable.name = req.body.name; | ||
| vegetable.price = req.body.price; | ||
| } else { | ||
| const message = `Could not find vegetable with ID of ${id}`; | ||
| next({ status: 404, message }); | ||
| } | ||
|
|
||
| res.json(vegetable); | ||
| }); | ||
|
|
||
| // 4 - GET/fruits | ||
| //================ | ||
|
|
||
| // app.get("/fruits", (req, res, next) => { | ||
| // const { fruits } = data; | ||
| // res.json(fruits); | ||
| // }); | ||
|
|
||
| // 5 - GET /fruits?name=[partial-query] | ||
| // =============================================== | ||
| app.get("/fruits", (req, res, next) => { | ||
| if (req.query) { | ||
|
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. Same problem here. |
||
| const { fruits } = data; | ||
| const { name } = req.query; | ||
| const result = fruits.filter(myFruit => myFruit.name.includes(name)); | ||
| res.json(result); | ||
| } else { | ||
| const { fruits } = data; | ||
| res.json(fruits); | ||
| } | ||
| }); | ||
|
|
||
| // 6 - GET/fruits/[id] | ||
| //===================== | ||
|
|
||
| app.get("/fruits/:id", (req, res, next) => { | ||
| const { fruits } = data; | ||
| const { id } = req.params; | ||
| const fruit = fruits.find(myFruit => myFruit.id === id); | ||
|
|
||
| if (!fruit) { | ||
| const message = `Could not find fruit with ID of ${id}`; | ||
| next({ status: 404, message }); | ||
| } | ||
|
|
||
| res.json(fruit); | ||
| }); | ||
|
|
||
| // 7 - POST/fruits | ||
| //================= | ||
|
|
||
| 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); | ||
| }); | ||
|
|
||
| // 8 - DELETE/fruits/[id] | ||
| // ======================== | ||
| app.delete("/fruits/:id", (req, res, next) => { | ||
| const { fruits } = data; | ||
| const { id } = req.params; | ||
| const index = fruits.findIndex(myFruit => myFruit.id === id); | ||
| fruit = fruits.splice(index, 1); | ||
| if (index == -1) { | ||
| const message = `Could not find fruit with ID of ${id}`; | ||
| next({ status: 404, message }); | ||
| } | ||
| res.json(fruit); | ||
| }); | ||
|
|
||
| // 9 - PUT/fruits/[id] | ||
| //========================= | ||
|
|
||
| app.put("/fruits/:id", (req, res, next) => { | ||
| const { fruits } = data; | ||
| const { id } = req.params; | ||
| const fruit = fruits.find(myFruit => myFruit.id === id); | ||
| if (fruit) { | ||
| fruit.name = req.body.name; | ||
| fruit.price = req.body.price; | ||
| } else { | ||
| const message = `Could not find fruit with ID of ${id}`; | ||
| next({ status: 404, message }); | ||
| } | ||
|
|
||
| res.json(fruit); | ||
| }); | ||
|
|
||
| //get all vegetables | ||
| app.get("/vegetables", (req, res, next) => { | ||
| const { vegetables } = data; | ||
| res.json(vegetables); | ||
| }); | ||
|
|
||
| app.get('/vegetables/:id', (req, res, next) => { | ||
| const { vegetables } = data | ||
| const { id } = req.params | ||
| const vegetable = vegetables.find(veggie => veggie.id === id) | ||
| //get vegetables with id | ||
| app.get("/vegetables/:id", (req, res, next) => { | ||
| const { vegetables } = data; | ||
| const { id } = req.params; | ||
| 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 = `Could not find vegetable with ID of ${id}`; | ||
| next({ status: 404, message }); | ||
| } | ||
|
|
||
| res.json(vegetable) | ||
| }) | ||
| res.json(vegetable); | ||
| }); | ||
|
|
||
| app.post('/vegetables', helpers.validate, (req, res, next) => { | ||
| const { vegetables } = data | ||
| const vegetable = { id: generateId(), ...req.body } | ||
| //post a new vegetable | ||
| 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) | ||
| }) | ||
| vegetables.push(vegetable); | ||
| res.status(201).json(vegetable); | ||
| }); | ||
|
|
||
| //Error loggers | ||
| app.use((req, res, next) => { | ||
| next({ | ||
| status: 404, | ||
| 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); | ||
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.
It looks like in one case you're using strings and in the other using a number. I'd be consistent here.