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:
1 parent
357a72114b
commit
9a6f75ebd8
32 files changed
+1465
-1
No files matched your search
@@ -0,0 +1,8 @@
|
||||
"""SQLAlchemy ORM models for newzy."""
|
||||
|
||||
from models.article import Article
|
||||
from models.author import Author
|
||||
from models.base import Base
|
||||
from models.category import Category, slugify
|
||||
|
||||
__all__ = ["Article", "Author", "Base", "Category", "slugify"]
|
||||
@@ -0,0 +1,40 @@
|
||||
"""Article model."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from datetime import datetime, timezone
|
||||
|
||||
from sqlalchemy import Boolean, DateTime, ForeignKey, String, Text
|
||||
from sqlalchemy.orm import Mapped, mapped_column, relationship
|
||||
|
||||
from models.base import Base
|
||||
|
||||
|
||||
def _utcnow() -> datetime:
|
||||
return datetime.now(timezone.utc)
|
||||
|
||||
|
||||
class Article(Base):
|
||||
"""A news article written by an author and filed under a category."""
|
||||
|
||||
__tablename__ = "articles"
|
||||
|
||||
id: Mapped[int] = mapped_column(primary_key=True)
|
||||
title: Mapped[str] = mapped_column(String(200), unique=True, nullable=False, index=True)
|
||||
content: Mapped[str] = mapped_column(Text, nullable=False)
|
||||
author_id: Mapped[int] = mapped_column(
|
||||
ForeignKey("authors.id"), nullable=False, index=True
|
||||
)
|
||||
category_id: Mapped[int] = mapped_column(
|
||||
ForeignKey("categories.id"), nullable=False, index=True
|
||||
)
|
||||
created_at: Mapped[datetime] = mapped_column(
|
||||
DateTime(timezone=True), default=_utcnow, nullable=False
|
||||
)
|
||||
published: Mapped[bool] = mapped_column(Boolean, default=False, nullable=False)
|
||||
|
||||
author: Mapped[Author] = relationship(back_populates="articles")
|
||||
category: Mapped[Category] = relationship(back_populates="articles")
|
||||
|
||||
def __repr__(self) -> str: # pragma: no cover
|
||||
return f"<Article id={self.id} title={self.title!r}>"
|
||||
@@ -0,0 +1,24 @@
|
||||
"""Author model."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from sqlalchemy import String, Text
|
||||
from sqlalchemy.orm import Mapped, mapped_column, relationship
|
||||
|
||||
from models.base import Base
|
||||
|
||||
|
||||
class Author(Base):
|
||||
"""A person who writes articles."""
|
||||
|
||||
__tablename__ = "authors"
|
||||
|
||||
id: Mapped[int] = mapped_column(primary_key=True)
|
||||
name: Mapped[str] = mapped_column(String(100), unique=True, nullable=False)
|
||||
email: Mapped[str] = mapped_column(String(255), unique=True, nullable=False)
|
||||
bio: Mapped[str | None] = mapped_column(Text, nullable=True)
|
||||
|
||||
articles: Mapped[list[Article]] = relationship(back_populates="author")
|
||||
|
||||
def __repr__(self) -> str: # pragma: no cover
|
||||
return f"<Author id={self.id} name={self.name!r}>"
|
||||
@@ -0,0 +1,7 @@
|
||||
"""Declarative base shared by all ORM models."""
|
||||
|
||||
from sqlalchemy.orm import DeclarativeBase
|
||||
|
||||
|
||||
class Base(DeclarativeBase):
|
||||
"""Base class for all newzy ORM models."""
|
||||
@@ -0,0 +1,41 @@
|
||||
"""Category model."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import re
|
||||
import unicodedata
|
||||
|
||||
from sqlalchemy import String
|
||||
from sqlalchemy.orm import Mapped, mapped_column, relationship
|
||||
|
||||
from models.base import Base
|
||||
|
||||
_SLUG_RE = re.compile(r"[^a-z0-9]+")
|
||||
|
||||
|
||||
def slugify(text: str) -> str:
|
||||
"""Convert an arbitrary string into a URL-friendly slug.
|
||||
|
||||
>>> slugify("Technology & Science")
|
||||
'technology-science'
|
||||
>>> slugify(" Sports ")
|
||||
'sports'
|
||||
"""
|
||||
normalized = unicodedata.normalize("NFKD", text)
|
||||
normalized = normalized.encode("ascii", "ignore").decode("ascii")
|
||||
return _SLUG_RE.sub("-", normalized.lower()).strip("-")
|
||||
|
||||
|
||||
class Category(Base):
|
||||
"""A topic used to group articles."""
|
||||
|
||||
__tablename__ = "categories"
|
||||
|
||||
id: Mapped[int] = mapped_column(primary_key=True)
|
||||
name: Mapped[str] = mapped_column(String(100), unique=True, nullable=False)
|
||||
slug: Mapped[str] = mapped_column(String(100), unique=True, nullable=False)
|
||||
|
||||
articles: Mapped[list[Article]] = relationship(back_populates="category")
|
||||
|
||||
def __repr__(self) -> str: # pragma: no cover
|
||||
return f"<Category id={self.id} slug={self.slug!r}>"
|
||||
Reference in new issue
Block a user