""" Legacy-compatible content section endpoints. Matches the URL shapes the SvelteKit frontend already calls, so no frontend changes are needed. """ from fastapi import APIRouter, Depends, HTTPException from sqlalchemy.ext.asyncio import AsyncSession from app.database import get_db from app.auth.deps import get_current_user from app.models.user import User from app.services.sections import get_section, upsert_section, list_sections router = APIRouter(tags=["Sections"]) # Slug → content_sections key PAGE_SLUG_MAP = { "home": "pages.home", "pack-walks": "pages.packWalks", "1-1-walks": "pages.oneOnOneWalks", "puppy-visits": "pages.puppyVisits", "pricing": "pages.pricing", "about": "pages.about", "contact": "pages.contact", } # ── Public read endpoints ──────────────────────────────────────────────────── @router.get("/api/site-settings") async def site_settings(db: AsyncSession = Depends(get_db)): data = await get_section(db, "siteSettings") return data or {} @router.get("/api/navigation") async def navigation(db: AsyncSession = Depends(get_db)): data = await get_section(db, "navigation") return data or {"items": []} @router.get("/api/footer") async def footer(db: AsyncSession = Depends(get_db)): data = await get_section(db, "footer") return data or {} @router.get("/api/testimonials") async def testimonials(db: AsyncSession = Depends(get_db)): data = await get_section(db, "testimonials") return data if data is not None else [] @router.get("/api/onboarding") async def onboarding(db: AsyncSession = Depends(get_db)): data = await get_section(db, "onboarding") return data or {} @router.get("/api/pages/{slug}") async def page_by_slug(slug: str, db: AsyncSession = Depends(get_db)): key = PAGE_SLUG_MAP.get(slug) if not key: raise HTTPException(status_code=404, detail=f"Page '{slug}' not found") data = await get_section(db, key) if data is None: raise HTTPException(status_code=404, detail=f"Page '{slug}' not found") return data # ── Protected admin endpoints ──────────────────────────────────────────────── @router.get("/api/admin/sections") async def admin_list_sections( db: AsyncSession = Depends(get_db), _: User = Depends(get_current_user), ): return await list_sections(db) @router.get("/api/admin/sections/{key:path}") async def admin_get_section( key: str, db: AsyncSession = Depends(get_db), _: User = Depends(get_current_user), ): data = await get_section(db, key) if data is None: raise HTTPException(status_code=404, detail="Section not found") return {"key": key, "data": data} @router.put("/api/admin/sections/{key:path}") async def admin_update_section( key: str, body: dict, db: AsyncSession = Depends(get_db), _: User = Depends(get_current_user), ): row = await upsert_section(db, key, body) return {"success": True, "key": row.key}