Provides a complete OpenAPI-first CRUD example with a Pydantic model layer, explicit runtime semantics, and integration tests to support developer onboarding and real-world service structure.
49 lines
1.1 KiB
Python
49 lines
1.1 KiB
Python
"""
|
|
CRUD route handlers bound via OpenAPI operationId.
|
|
"""
|
|
|
|
from fastapi import Response, HTTPException
|
|
|
|
from models import ItemCreate
|
|
from data import (
|
|
list_items as _list_items,
|
|
get_item as _get_item,
|
|
create_item as _create_item,
|
|
update_item as _update_item,
|
|
delete_item as _delete_item,
|
|
)
|
|
|
|
|
|
def list_items():
|
|
return _list_items()
|
|
|
|
|
|
def get_item(item_id: int):
|
|
try:
|
|
return _get_item(item_id)
|
|
except KeyError:
|
|
raise HTTPException(status_code=404, detail="Item not found")
|
|
|
|
|
|
def create_item(payload: ItemCreate, response: Response):
|
|
item = _create_item(payload)
|
|
response.status_code = 201
|
|
return item
|
|
|
|
|
|
def update_item(item_id: int, payload: ItemCreate):
|
|
try:
|
|
return _update_item(item_id, payload)
|
|
except KeyError:
|
|
raise HTTPException(status_code=404, detail="Item not found")
|
|
|
|
|
|
def delete_item(item_id: int, response: Response):
|
|
try:
|
|
_delete_item(item_id)
|
|
except KeyError:
|
|
raise HTTPException(status_code=404, detail="Item not found")
|
|
|
|
response.status_code = 204
|
|
return None
|