root 67d8c6bf30 feat: add FastAPI example project with CRUD API
- Add FastAPI application with health check and item CRUD endpoints
- Add Pydantic models for request/response validation
- Add pytest test suite with 5 passing tests
- Add project documentation and run script

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
2026-05-07 08:25:34 +08:00

35 lines
866 B
Python

from fastapi import FastAPI, HTTPException
from typing import Dict
from app.models import Item, ItemCreate
app = FastAPI(title="FastAPI Example")
# 内存存储
_items: Dict[int, Item] = {}
_item_id_counter = 0
@app.get("/health")
async def health_check():
return {"status": "ok"}
@app.post("/items", response_model=Item)
async def create_item(item: ItemCreate):
global _item_id_counter
_item_id_counter += 1
new_item = Item(id=_item_id_counter, **item.model_dump())
_items[_item_id_counter] = new_item
return new_item
@app.get("/items", response_model=list[Item])
async def list_items():
return list(_items.values())
@app.get("/items/{item_id}", response_model=Item)
async def get_item(item_id: int):
if item_id not in _items:
raise HTTPException(status_code=404, detail="Item not found")
return _items[item_id]