refactor(stage-4): partition monolithic main.py into modular APIRouters
This commit is contained in:
@@ -0,0 +1,79 @@
|
||||
from typing import Optional, List, Dict, Any
|
||||
from fastapi import APIRouter, Depends, Response
|
||||
|
||||
from app.auth import require_user
|
||||
from app.database import get_db
|
||||
from app.export import export_csv, export_json, export_anki_tsv, export_markdown
|
||||
|
||||
router = APIRouter(tags=["Export"])
|
||||
|
||||
|
||||
def _fetch_all_cards(user_id: int, collection: Optional[str] = None) -> List[Dict[str, Any]]:
|
||||
with get_db() as conn:
|
||||
if collection:
|
||||
rows = conn.execute(
|
||||
"""
|
||||
SELECT id, term, translation, context_sentence, phonetic, audio_url,
|
||||
part_of_speech, definition_en, tags, notes, collection, created_at, updated_at
|
||||
FROM vocabulary_cards
|
||||
WHERE user_id = ? AND collection = ?
|
||||
ORDER BY collection ASC, created_at DESC
|
||||
""",
|
||||
(user_id, collection),
|
||||
).fetchall()
|
||||
else:
|
||||
rows = conn.execute(
|
||||
"""
|
||||
SELECT id, term, translation, context_sentence, phonetic, audio_url,
|
||||
part_of_speech, definition_en, tags, notes, collection, created_at, updated_at
|
||||
FROM vocabulary_cards
|
||||
WHERE user_id = ?
|
||||
ORDER BY collection ASC, created_at DESC
|
||||
""",
|
||||
(user_id,),
|
||||
).fetchall()
|
||||
return [dict(r) for r in rows]
|
||||
|
||||
|
||||
@router.get("/api/export/csv")
|
||||
async def api_export_csv(collection: Optional[str] = None, user: dict = Depends(require_user)):
|
||||
cards = _fetch_all_cards(user["id"], collection)
|
||||
data = export_csv(cards)
|
||||
return Response(
|
||||
content=data,
|
||||
media_type="text/csv; charset=utf-8",
|
||||
headers={"Content-Disposition": 'attachment; filename="vocabulary.csv"'},
|
||||
)
|
||||
|
||||
|
||||
@router.get("/api/export/json")
|
||||
async def api_export_json(collection: Optional[str] = None, user: dict = Depends(require_user)):
|
||||
cards = _fetch_all_cards(user["id"], collection)
|
||||
data = export_json(cards)
|
||||
return Response(
|
||||
content=data,
|
||||
media_type="application/json; charset=utf-8",
|
||||
headers={"Content-Disposition": 'attachment; filename="vocabulary.json"'},
|
||||
)
|
||||
|
||||
|
||||
@router.get("/api/export/anki")
|
||||
async def api_export_anki(collection: Optional[str] = None, user: dict = Depends(require_user)):
|
||||
cards = _fetch_all_cards(user["id"], collection)
|
||||
data = export_anki_tsv(cards)
|
||||
return Response(
|
||||
content=data,
|
||||
media_type="text/plain; charset=utf-8",
|
||||
headers={"Content-Disposition": 'attachment; filename="vocabulary_anki.txt"'},
|
||||
)
|
||||
|
||||
|
||||
@router.get("/api/export/markdown")
|
||||
async def api_export_markdown(collection: Optional[str] = None, user: dict = Depends(require_user)):
|
||||
cards = _fetch_all_cards(user["id"], collection)
|
||||
data = export_markdown(cards)
|
||||
return Response(
|
||||
content=data,
|
||||
media_type="text/markdown; charset=utf-8",
|
||||
headers={"Content-Disposition": 'attachment; filename="vocabulary.md"'},
|
||||
)
|
||||
Reference in New Issue
Block a user