Endpoint: GET /vendingMachine/:id/item/:item_id
```json
{
"id": 1 // id of the item
"quantity": 1 // quantity of the item
"price": // price of the item in double (currency is USD)
}
```
Endpoint: GET /vendingMachine/:id/items
```json
[{
"id": 1 // id of the item
"quantity": 1 // quantity of the item
"price": // price of the item in double (currency is USD)
},{
"id": 2 // id of the item
"quantity": 1 // quantity of the item
"price": // price of the item in double (currency is USD)
}]
```
Endpoint: POST /vendingMachine/:id/item/:item_id
In the body of the request, the user will specify their payment info information as well:
{
"paymentType": CARD / CASH/ MOBILE_PAY,
"paymentInfo": Optional<{
// card or mobile_pay details here
}>
}
This endpoint will do a couple things:
The response for the endpoint will be info about the item:
```
{
"id":
"name": name_of_item,
"description": optional_description
}
```
Because we need the system to be consistent, we can have a relational db.
vending_machines table
items
This design allows us to have different types of items per vending machine. It also allows us to track the quantity of the items independently from each vending machine.
You should identify enough components that are needed to solve the actual problem from end to end. Also remember to draw a block diagram using the diagramming tool to augment your design. If you are unfamiliar with the tool, you can simply describe your design to the chat bot and ask it to generate a starter diagram for you to modify...
There will be an API gateway for the client to interact with.
Idempotent requests such as get vending machine or vending machine items will be handled by the vending machine service. Because get requests will likely be more popular than non idempotent requests such as purchase, we can assume this will be a read heavy application. We can have a write-through cache for the vending machine service. This means when the user purchases the item, the cache for the vending machine and item is updated accordingly.
Non-idempotent requests such as purchasing the items will be handled by the purchase service, which will also interact with the payment validation service. When a purchase occurs, the item for the purchase will be locked in the items database, and then a request will be made to the payment validation service. If the payment validation service returns a successful response, the purchase service will allow the purchase of the item be successful. If the payment validation returns a failure response, the purchase service will not allow the purchase of the item to be successful.
The lock on the item will be released after a terminal response is received from the payment validation service.
We can go over a flow for a user deciding to check out what items are available and then deciding to purchase an item from the vending machine:
What are some future improvements you would make? How would you mitigate the failure scenario(s) you described above?