This exercise will assess your ability to build upon an already existing Express API.
- Fork & Clone this repository
npm install
npm run dev
Improve upon and/or build the following routes as specified.
- GET /vegetables?name=[partial-query]
- DELETE /vegetables/[id]
- PUT /vegetables/[id]
- GET /fruits
- GET /fruits?name=[partial-query]
- GET /fruits/[id]
- POST /fruits
- DELETE /fruits/[id]
- PUT /fruits/[id]
Note: This is not a new route!
- e.g.
GET http://localhost:5000/vegetables?name="pepper"
Return a list of all vegetables where the query is included somewhere in the name.
Correct Response Body Example
[
{ "id": "AEdvQm9t_", "name": "green pepper", "price": "0.99" },
{ "id": "JA_dIbXLW", "name": "red pepper", "price": "0.99" }
]
If there are no vegetables that match, just return an empty array.
Correct Response Body Example
[]
- e.g.
DELETE http://localhost:5000/vegetables/5TsEt-xfI
If there is a record where the ID matches, remove the resource from the array. Return a status code of 200.
Correct Response Body Example
{ "id": "5TsEt-xfI", "name": "turnip", "price": "0.79" }
If there is not a record where the ID matches, return an error with a status of
404
saying that the vegetable could not be found.
Incorrect Response Body Example
{ "message": "Could not find vegetable with ID of 1" }
- e.g.
PUT http://localhost:5000/vegetables/5TsEt-xfI
If there is a record where the ID matches, validate the request body and then update the resource with the new information. Return a status code of 200.
Correct Request Body Example
{ "name": "turnip", "price": "0.79" }
Correct Response Body Example
{ "id": "5TsEt-xfI", "name": "turnip", "price": "0.79" }
If there is not a record where the ID matches, return an error with a status of
404
saying that the vegetable could not be found.
Incorrect Response Body Example
{ "message": "Could not find vegetable with ID of 1" }
If the request body does not include the "name" and "price" keys, or includes other keys, return the validation error.
Incorrect Request Body Example
{ "name": "turnip" }
Incorrect Response Body Example
{ "message": "Bad request" }
- e.g.
GET http://localhost:5000/fruits
Return all fruits with a status code of 200.
Correct Response Body Example
[
{ "id": "p7pzzZxCX", "name": "banana", "price": "0.79" }
]
Note: This is not a new route!
- e.g.
GET http://localhost:5000/fruits?name="berr"
Return a list of all fruits where the query is included somewhere in the name.
Correct Response Body Example
[
{ "id": "H14ykcZcT", "name": "blueberries", "price": "2.99" },
{ "id": "faf84ZZNF", "name": "strawberries", "price": "3.99" }
]
If there are no fruits that match, just return an empty array.
Correct Response Body Example
[]
- e.g.
GET http://localhost:5000/fruits/p7pzzZxCX
If there is a record where the ID matches, return the resource with a status code of 200.
Correct Response Body Example
{ "id": "p7pzzZxCX", "name": "banana", "price": "0.79" }
- e.g.
POST http://localhost:5000/fruits
Validate the request body and then create a new resource with an ID. Return a status code of 201.
Correct Request Body Example
{ "name": "banana", "price": "0.79" }
Correct Response Body Example
{ "id": "p7pzzZxCX", "name": "banana", "price": "0.79" }
- e.g.
DELETE http://localhost:5000/fruits/p7pzzZxCX
If there is a record where the ID matches, remove the resource from the array. Return a status code of 200.
Correct Response Body Example
{ "id": "p7pzzZxCX", "name": "banana", "price": "0.79" }
If there is not a record where the ID matches, return an error with a status of
404
saying that the banana could not be found.
Incorrect Response Body Example
{ "message": "Could not find banana with ID of 1" }
- e.g.
PUT http://localhost:5000/fruits/p7pzzZxCX
If there is a record where the ID matches, validate the request body and then update the resource with the new information. Return a status code of 200.
Correct Request Body Example
{ "name": "banana", "price": "0.79" }
Correct Response Body Example
{ "id": "p7pzzZxCX", "name": "banana", "price": "0.79" }
If there is not a record where the ID matches, return an error with a status of
404
saying that the banana could not be found.
Incorrect Response Body Example
{ "message": "Could not find banana with ID of 1" }
If the request body does not include the "name" and "price" keys, or includes other keys, return the validation error.
Incorrect Request Body Example
{ "name": "banana" }
Incorrect Response Body Example
{ "message": "Bad request" }