Implement milestone M2 (backend setup):
- FastAPI app factory (core/app.py) with CORS, lifespan table
creation, and global error handlers using a standardized
{detail, code, errors?} envelope
- Endpoints: GET /health, GET /api/v1/health, GET /api/v1/version
- SQLAlchemy 2.0 ORM: Article, Author, Category models with
relationships, unique constraints, and FK pragma for SQLite
- Pydantic v2 schemas (create/update/read) for all models
- Env-driven config (DATABASE_URL, CORS_ORIGINS) + .env.example
- pytest suite (19 tests, ~95% coverage) with per-test SQLite
databases via fixtures
- Dockerfile (python:3.11-slim) and docker-compose.yml with a
persistent ./data/db volume and healthcheck
45 lines
1.1 KiB
Python
45 lines
1.1 KiB
Python
"""Schemas for the Article model."""
|
|
|
|
from datetime import datetime
|
|
|
|
from pydantic import BaseModel, ConfigDict, Field
|
|
|
|
|
|
class ArticleBase(BaseModel):
|
|
"""Fields shared by create/update/read article schemas."""
|
|
|
|
title: str = Field(..., min_length=1, max_length=200)
|
|
content: str = Field(..., min_length=1)
|
|
|
|
|
|
class ArticleCreate(ArticleBase):
|
|
"""Payload for creating an article."""
|
|
|
|
author_id: int = Field(..., ge=1)
|
|
category_id: int = Field(..., ge=1)
|
|
published: bool = False
|
|
|
|
|
|
class ArticleUpdate(BaseModel):
|
|
"""Payload for updating an article (all fields optional)."""
|
|
|
|
title: str | None = Field(default=None, min_length=1, max_length=200)
|
|
content: str | None = Field(default=None, min_length=1)
|
|
author_id: int | None = Field(default=None, ge=1)
|
|
category_id: int | None = Field(default=None, ge=1)
|
|
published: bool | None = None
|
|
|
|
|
|
class ArticleRead(BaseModel):
|
|
"""Article as returned by the API."""
|
|
|
|
model_config = ConfigDict(from_attributes=True)
|
|
|
|
id: int
|
|
title: str
|
|
content: str
|
|
author_id: int
|
|
category_id: int
|
|
created_at: datetime
|
|
published: bool
|