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:

Javascript (Node.js):

Go:

Ruby:

Testing

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
}

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:

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:

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:

Remove all stock of a specific named product

DELETE:

/api/inventory/:product

Body: None

Response: None

Status Codes:

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:

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: