Problem statement
Build the three registration endpoints and replace the framework's validation report with one of the service's own, naming each offending value by its path and keeping the reason and the rule it broke.
Sample Data
The register in app/store.py starts with four registrations under the ids 1_ to _4, three of them for workshop 101 and one for 102. The next registration created takes the id 5.
API Endpoints to Implement
The three handlers live in app/api/routes/registrations.py, and their helpers, list_registrations, registrations_for and add_registration, in app/services/registrations.py.
GET /registrations
Answers 200 with every registration in the order the register holds them, each carrying id, attendee, workshop_id and seats.
GET /workshops/{workshop_id}/registrations
- workshop_id comes from the URL as a whole number.
- Answers 200 with only that workshop's registrations, and with an empty list for a workshop nobody registered for.
POST /registrations
Takes the body as a RegistrationCreate, holding attendee, workshop_id and seats, all required, and answers 201 with the stored registration: the three drafted values plus the id it was given.
Rejected requests
reshape_validation_errors in app/core/errors.py answers every validation failure, on the body and on the URL alike, with 422 and a body holding exactly two keys: error, always the text validation_error, and errors, one entry per broken rule.
Each entry holds exactly three keys:
- field, the path to the offending value with its parts joined by dots, so a body field is body.attendee and the number in the URL is path.workshop_id.
- message, the reason the framework gave.
- code, the name the framework gave the rule.
A draft breaking three rules reports three entries, and nothing is dropped: the original report can be rebuilt from the reshaped one.
The framework must be left to find the failures rather than each handler checking its own, since the service publishes the contract at GET /openapi.json, where the registration requires a JSON body, both validated operations declare 422, and workshop_id is a required whole number in the path.
Sample Requests & Responses
POST /registrations
Request:
{
"attendee": "Noa Lindt",
"workshop_id": 102,
"seats": 2
}
Response (201 Created):
{
"id": 5,
"attendee": "Noa Lindt",
"workshop_id": 102,
"seats": 2
}
GET /workshops/101/registrations
Response (200 OK):
[
{"id": 1, "attendee": "Mira Chen", "workshop_id": 101, "seats": 2},
{"id": 3, "attendee": "Sam Ortiz", "workshop_id": 101, "seats": 3},
{"id": 4, "attendee": "Rae Patel", "workshop_id": 101, "seats": 1}
]
POST /registrations
Request:
{
"attendee": 12,
"seats": "many"
}
Response (422 Unprocessable Entity):
{
"error": "validation_error",
"errors": [
{"field": "body.attendee", "message": "Input should be a valid string", "code": "string_type"},
{"field": "body.workshop_id", "message": "Field required", "code": "missing"},
{"field": "body.seats", "message": "Input should be a valid integer, unable to parse string as an integer", "code": "int_parsing"}
]
}
GET /workshops/abc/registrations
Response (422 Unprocessable Entity):
{
"error": "validation_error",
"errors": [
{"field": "path.workshop_id", "message": "Input should be a valid integer, unable to parse string as an integer", "code": "int_parsing"}
]
}
