import uuid from fastapi import APIRouter, File, Response, UploadFile from app.api.deps import CurrentUser, SessionDep from app.schemas.gedcom import ImportReport from app.services import gedcom, tree_service router = APIRouter(prefix="/trees", tags=["gedcom"]) @router.post("/{tree_id}/gedcom/import", response_model=ImportReport) async def import_gedcom( tree_id: uuid.UUID, session: SessionDep, current: CurrentUser, file: UploadFile = File(...), ) -> ImportReport: # NOTE: additive — records are created as new; existing people are not merged. tree = await tree_service.get_tree(session, viewer_id=current.id, tree_id=tree_id) text = (await file.read()).decode("utf-8", errors="replace") report = await gedcom.import_gedcom(session, actor=current, tree=tree, text=text) return ImportReport(**report) @router.get("/{tree_id}/gedcom/export") async def export_gedcom( tree_id: uuid.UUID, session: SessionDep, current: CurrentUser ) -> Response: tree = await tree_service.get_tree(session, viewer_id=current.id, tree_id=tree_id) text = await gedcom.export_gedcom(session, viewer_id=current.id, tree=tree) safe = "".join(c for c in tree.name if c.isalnum() or c in " -_").strip() or "tree" return Response( content=text, media_type="text/plain", headers={"Content-Disposition": f'attachment; filename="{safe}.ged"'}, )