BMS/backend/api/routes/floor_layout.py
2026-03-19 11:32:17 +00:00

33 lines
1.1 KiB
Python

from fastapi import APIRouter, Depends, HTTPException
from sqlalchemy.ext.asyncio import AsyncSession
from sqlalchemy import text
from core.database import get_session
router = APIRouter()
@router.get("")
async def get_floor_layout(site_id: str, db: AsyncSession = Depends(get_session)):
row = await db.execute(
text("SELECT value FROM site_config WHERE site_id = :site_id AND key = 'floor_layout'"),
{"site_id": site_id},
)
result = row.fetchone()
if result is None:
raise HTTPException(status_code=404, detail="No floor layout saved for this site")
return result[0]
@router.put("")
async def save_floor_layout(site_id: str, layout: dict, db: AsyncSession = Depends(get_session)):
await db.execute(
text("""
INSERT INTO site_config (site_id, key, value, updated_at)
VALUES (:site_id, 'floor_layout', CAST(:value AS jsonb), NOW())
ON CONFLICT (site_id, key)
DO UPDATE SET value = EXCLUDED.value, updated_at = NOW()
"""),
{"site_id": site_id, "value": __import__("json").dumps(layout)},
)
await db.commit()
return {"ok": True}