Spaces:
Sleeping
Sleeping
File size: 1,385 Bytes
0bd1699 e92417d 363bda3 0bd1699 363bda3 0bd1699 e92417d 0bd1699 363bda3 0bd1699 e92417d 0bd1699 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 | """GET /articles — list recent articles (no search required)."""
from typing import Optional
from fastapi import APIRouter, HTTPException
from database import supabase
router = APIRouter(tags=["articles"])
ARTICLE_FIELDS = "id, title, body, language, tags, type, contributing_agent, confidence, created_at"
@router.get("/articles/{article_id}")
def get_article(article_id: str):
"""Get a single article by id for the post detail view."""
result = (
supabase.table("articles")
.select(ARTICLE_FIELDS)
.eq("id", article_id)
.limit(1)
.execute()
)
if not result.data:
raise HTTPException(status_code=404, detail="Article not found")
return result.data[0]
@router.get("/articles")
def list_articles(
limit: int = 50,
since: Optional[str] = None,
language: Optional[str] = None,
type: Optional[str] = None,
):
"""List recent articles, newest first. Optional filters: since (ISO date), language, type."""
query = (
supabase.table("articles")
.select(ARTICLE_FIELDS)
.order("created_at", desc=True)
.limit(min(limit, 100))
)
if since:
query = query.gte("created_at", since)
if language:
query = query.eq("language", language)
if type:
query = query.eq("type", type)
result = query.execute()
return result.data
|