Problem statement
Build a bakery pre-order counter that takes and lists pre-orders, and publishes ready-made example orders for callers to copy.
Sample Data
The counter in app/store.py starts with two pre-orders, and the next pre-order created takes the id 3.
API Endpoints to Implement
The two handlers live in app/api/routes/preorders.py, the draft they take, PreorderDraft, in app/models/preorder.py, and their helpers, list_preorders and add_preorder, in app/services/preorders.py.
GET /preorders
Answers 200 with every pre-order in the order the counter holds them.
POST /preorders
- Takes the body as a PreorderDraft and answers 201 with the stored pre-order: the drafted values plus the id it was given. A stored pre-order joins the counter, so a fresh listing reports it after the two the counter started with.
- item is text, 2 to 40 characters, required.
- quantity is a whole number, 1 to 24, required.
- pickup_hour is a whole number, 7 to 18, required.
- note is text of at most 80 characters, optional, default null.
The published examples
PreorderDraft publishes examples, a list of one entry, the whole draft worked out:
[{"item": "Walnut Rye Loaf", "quantity": 3, "pickup_hour": 9, "note": "Sliced, please"}]
Each of its four values publishes an examples list of its own, holding one entry:
- item names Sourdough Loaf
- quantity names 2
- pickup_hour names 8
- note names Sliced, please
These five entries belong to the draft itself, not to the operation that takes it, and the draft still publishes the limits above alongside them. Both the whole-draft entry and a draft built from the four per-value entries are stored when sent.
POST /preorders publishes examples of its own on the JSON body it takes, three entries in this order, each holding a summary and a value:
{
"single_loaf": {"summary": "One loaf for the morning counter", "value": {"item": "Sourdough Loaf", "quantity": 1, "pickup_hour": 8}},
"party_order": {"summary": "A large order with a note", "value": {"item": "Cinnamon Buns", "quantity": 18, "pickup_hour": 15, "note": "Boxed in threes"}},
"past_the_limit": {"summary": "A quantity the counter refuses", "value": {"item": "Cinnamon Buns", "quantity": 30, "pickup_hour": 15}}
}
single_loaf and party_order are stored when sent. past_the_limit is refused, like any other draft over the limit.
Rejected requests
A draft missing one of its three required values, or carrying a value of the wrong kind or outside its stated range, is answered with 422 and one detail entry per broken rule, carrying loc of ["body", "field name"], the input that was received, and the type and msg reported for the rule.
The published document
The service publishes its description at GET /openapi.json. Every example must be declared on the draft or on the operation itself, not written into that document by hand.
There, POST /preorders takes its body as a reference to the shared PreorderDraft shape under components/schemas, with nothing else beside the reference.
Sample Requests & Responses
GET /preorders
Response (200 OK):
[
{"id": 1, "item": "Baguette", "quantity": 4, "pickup_hour": 7, "note": null},
{"id": 2, "item": "Almond Croissant", "quantity": 6, "pickup_hour": 10, "note": "Two boxes"}
]
POST /preorders
Request:
{
"item": "Sourdough Loaf",
"quantity": 1,
"pickup_hour": 8
}
Response (201 Created):
{
"id": 3,
"item": "Sourdough Loaf",
"quantity": 1,
"pickup_hour": 8,
"note": null
}
POST /preorders
Request:
{
"item": "Cinnamon Buns",
"quantity": 30,
"pickup_hour": 15
}
Response (422 Unprocessable Entity):
{
"detail": [
{
"type": "less_than_equal",
"loc": ["body", "quantity"],
"msg": "Input should be less than or equal to 24",
"input": 30,
"ctx": {"le": 24}
}
]
}
