Grocery Store
Write a server to implement a basic grocery store. It should keep track of product inventory and allow you to add, update, and delete products through various API endpoints.
It's up to you to decide how to implement this.
Apprentices of all disciplines should build the server, and a client app to work with it. For iOS, Android, and Web apprentices, build a client in your own discipline; Systems apprentices may choose any other discipline in which to build the client.
Some possible languages and frameworks to use:
Python:
-
Django Rest Framework: http://www.django-rest-framework.org
-
using Postgres for data storage: http://www.postgresql.org
-
Falcon: http://falconframework.org/
-
http://falcon.readthedocs.org/en/latest/user/quickstart.html
-
Google App Engine: https://cloud.google.com/appengine/docs
-
using webapp2: https://cloud.google.com/appengine/docs/standard/python/tools/webapp2
-
Flask: http://flask.pocoo.org
Javascript (Node.js):
- Express: http://expressjs.com
- http://expressjs.com/starter/hello-world.html
- http://expressjs.com/starter/basic-routing.html
Go:
- net/http: http://godoc.org/net/http
Ruby:
====================
Testing
- Use
curl
to test individual endpoints from the command line.
Example (assuming your server is running locally on port 5000, the default for Flask):
>$ curl http://localhost:5000/api/inventory
Might output the following:
{
"apples": 113,
"eggs": 10,
"milk": 48,
"oranges": 57
}
-
Use a REST client app, like Postman, to test the endpoints in a more repeatable way.
-
Use the unit testing features of the language or framework you're using.
-
Write a client to interface with your server
API
Get stock of all products
GET:
/api/inventory
Body: None
Notes:
Fetch the stock of all products and return a dictionary mapping product name to quantity. If there is no inventory, return an empty dictionary.
Response:
{
"apples": 123,
"oranges": 62,
"milk": 54,
"eggs": 22,
}
Status Codes:
200
if successful
Get stock of a specific named product
GET:
/api/inventory/:product
Body: None
Notes:
Fetch the stock of a specific product and return a dictionary mapping product name to quantity.
Response:
{
"apples": 123
}
Status Codes:
200
if successful404
if the is no inventory for the given product
Re-stock one or more specific named products
POST:
/api/inventory/:product
Body:
Optional. If included, increase quantity by the specfied amount. Quantity value must be positive. If no body is included in the request, increase the quantity by 1. An empty JSON body (eg "{}") should be considered an error.
{
"quantity": 123
}
Notes:
Increase the quantity of a specific product and return a dictionary mapping product name to updated quantity.
Response:
{
"apples": 123
}
400 Error:
{
"error": "Quantity must be positive.",
}
Status Codes:
201
if successful, and the product was not available before this call200
if successful, and the product was available before this call400
if there is a body of the request that does not contain a positive "quantity" value. An error message should accompany this response code
Remove all stock of a specific named product
DELETE:
/api/inventory/:product
Body: None
Response: None
Status Codes:
200
if successful404
if the was no inventory to remove for the given product
Purchase a specific named product
POST:
/api/purchase/:product/
Body:
Optional. If included, decrease quantity by the specfied amount. Quantity value must be positive. If no body is included in the request, decrease the quantity by 1. An empty JSON body (eg "{}") should be considered an error.
{
"quantity": 12
}
Notes:
Decrease the quantity of a specific product and return a dictionary mapping product name to updated quantity.
Response:
Success:
{
"apples": 123
}
400 Error:
{
"error": "a string describing the error"
}
Status Codes:
200
if successful400
if there is a body of the request that does not contain a positive "quantity" value or the result of the purchase would be negative stock in the given product. An error message should be returned in these cases.404
if the product is not available
Purchase multiple products
POST:
/api/purchase
Body:
Quantity values must be positive.
{
"apples": 6,
"milk": 1,
"eggs": 1
}
Notes:
Decrease the quantity of a the given products and return a dictionary mapping product name to updated quantities.
Response:
Success:
{
"apples": 117,
"milk": 53,
"eggs": 21
}
400 Error:
{
"error": "a string describing the error"
}
404 Error:
{
"error": "yes, we have no bananas"
}
Status Codes:
200
if successful400
if the a body of the request contains an invalid value or the result of the purchase would be negative stock in any of the given products. An error message should be returned in these cases.404
if any product is not available with an error message telling which products are not available.