feat: add FastAPI backend with SQLite models and Docker setup

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
This commit is contained in:
Daniel Samson committed 2026-10-30 12:44:54 +00:00
1 parent 357a72114b
commit 9a6f75ebd8
32 files changed
+1465 -1

No files matched your search

+24
View File
@@ -0,0 +1,24 @@
"""Pydantic request/response schemas for newzy."""
from schemas.article import ArticleBase, ArticleCreate, ArticleRead, ArticleUpdate
from schemas.author import AuthorBase, AuthorCreate, AuthorRead, AuthorUpdate
from schemas.category import CategoryBase, CategoryCreate, CategoryRead, CategoryUpdate
from schemas.common import ErrorDetail, HealthResponse, VersionResponse
__all__ = [
"ArticleBase",
"ArticleCreate",
"ArticleRead",
"ArticleUpdate",
"AuthorBase",
"AuthorCreate",
"AuthorRead",
"AuthorUpdate",
"CategoryBase",
"CategoryCreate",
"CategoryRead",
"CategoryUpdate",
"ErrorDetail",
"HealthResponse",
"VersionResponse",
]
+44
View File
@@ -0,0 +1,44 @@
"""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
+38
View File
@@ -0,0 +1,38 @@
"""Schemas for the Author model."""
from pydantic import BaseModel, ConfigDict, Field, field_validator
class AuthorBase(BaseModel):
"""Fields shared by create/update/read author schemas."""
name: str = Field(..., min_length=1, max_length=100)
email: str = Field(..., min_length=3, max_length=255)
bio: str | None = None
@field_validator("email")
@classmethod
def _valid_email(cls, value: str) -> str:
if "@" not in value or value.startswith("@") or value.endswith("@"):
raise ValueError("must be a valid email address")
return value
class AuthorCreate(AuthorBase):
"""Payload for creating an author."""
class AuthorUpdate(BaseModel):
"""Payload for updating an author (all fields optional)."""
name: str | None = Field(default=None, min_length=1, max_length=100)
email: str | None = Field(default=None, min_length=3, max_length=255)
bio: str | None = None
class AuthorRead(AuthorBase):
"""Author as returned by the API."""
model_config = ConfigDict(from_attributes=True)
id: int
+51
View File
@@ -0,0 +1,51 @@
"""Schemas for the Category model."""
import re
from pydantic import BaseModel, ConfigDict, Field
from models.category import slugify
_SLUG_PATTERN = r"^[a-z0-9]+(?:-[a-z0-9]+)*$"
class CategoryBase(BaseModel):
"""Fields shared by category schemas."""
name: str = Field(..., min_length=1, max_length=100)
slug: str | None = Field(
default=None,
min_length=1,
max_length=100,
pattern=_SLUG_PATTERN,
description="URL-friendly identifier; derived from `name` when omitted.",
)
class CategoryCreate(CategoryBase):
"""Payload for creating a category.
When ``slug`` is omitted it is generated from ``name``.
"""
class CategoryUpdate(BaseModel):
"""Payload for updating a category (all fields optional)."""
name: str | None = Field(default=None, min_length=1, max_length=100)
slug: str | None = Field(default=None, min_length=1, max_length=100, pattern=_SLUG_PATTERN)
class CategoryRead(BaseModel):
"""Category as returned by the API."""
model_config = ConfigDict(from_attributes=True)
id: int
name: str
slug: str
def prepare_slug(create: CategoryCreate) -> str:
"""Resolve the slug to store: explicit slug or one derived from the name."""
return create.slug if create.slug is not None else slugify(create.name)
+29
View File
@@ -0,0 +1,29 @@
"""Shared response schemas and the standardized error envelope."""
from pydantic import BaseModel
class ErrorDetail(BaseModel):
"""Standard error payload returned for all non-2xx responses.
``errors`` is only present for validation failures (HTTP 422) and
carries the per-field details.
"""
detail: str
code: str
errors: list[dict] | None = None
class HealthResponse(BaseModel):
"""Payload of the health check endpoints."""
status: str = "ok"
version: str
class VersionResponse(BaseModel):
"""Payload of the version endpoint."""
app: str
version: str