Files
newz-qwen-27b/backend/api/v1/articles.py
Daniel Samson afa399776f feat(admin): rebuild admin area around the Article domain
The admin area was managing a Hacker News-style Item model that the
public site never consumed, so the homepage always fell back to sample
data. Rebuild the admin area around the real public content model.

Backend:
- Remove the Item model, schema and admin API
- Admin API now manages articles at /api/v1/admin/articles (CRUD) with
  /api/v1/admin/authors and /api/v1/admin/categories reference data
- Add public read-only article endpoints (/api/v1/articles,
  /api/v1/articles/{id}): no auth, published-only, newest-first
- Rewrite Article schemas; ArticleRead embeds author and category
- Add idempotent seed_content.py dev seeder (3 authors, 4 categories,
  6 published articles)

Frontend:
- Admin UI rewritten around articles: AdminDashboard list, new
  EditArticlePage (create/edit, author/category selects, published
  toggle, Lexical editor, debounced auto-save)
- Replace AdminItem types and api client with the article domain
- Update App routes, AdminLayout sidebar, DeleteConfirmation, Lexical
  editor labels and Home/client comments to article terminology

Tests:
- Rewrite admin unit tests and the Playwright admin e2e spec for the
  article domain; add test_articles.py for the public endpoints and
  seeder
2026-10-31 18:31:05 +00:00

56 lines
1.8 KiB
Python

"""Public read API for articles.
No authentication required — these endpoints power the site front end:
* ``GET /api/v1/articles`` — the home page feed. Returns *published* articles
newest-first, each with its author and category embedded.
* ``GET /api/v1/articles/{id}`` — a single published article (404 when missing
or still a draft).
Drafts (``published=False``) are never exposed here; admins manage them via
the admin area (``api.v1.admin``).
"""
from fastapi import APIRouter, Depends, HTTPException, status
from sqlalchemy.orm import Session, joinedload
from db.session import get_db
from models import Article
from schemas import ArticleRead
router = APIRouter(prefix="/articles", tags=["articles"])
def _load_published_article(db: Session, article_id: int) -> Article:
article = (
db.query(Article)
.options(joinedload(Article.author), joinedload(Article.category))
.filter(Article.id == article_id, Article.published.is_(True))
.first()
)
if article is None:
raise HTTPException(status_code=404, detail="Article not found")
return article
@router.get("", response_model=list[ArticleRead], summary="List published articles")
def list_published_articles(
db: Session = Depends(get_db),
) -> list[Article]:
"""Published articles, newest first, with author and category embedded."""
return (
db.query(Article)
.options(joinedload(Article.author), joinedload(Article.category))
.filter(Article.published.is_(True))
.order_by(Article.created_at.desc())
.all()
)
@router.get("/{article_id}", response_model=ArticleRead, summary="Get a published article")
def get_published_article(
article_id: int,
db: Session = Depends(get_db),
) -> Article:
return _load_published_article(db, article_id)