import uuid from fastapi import APIRouter, HTTPException from app.models import SystemCreate, SystemRecord from app.internal import firestore as fstore router = APIRouter(prefix="/systems", tags=["systems"]) @router.get("") async def list_systems(): return await fstore.collection_list("systems") @router.get("/{system_id}") async def get_system(system_id: str): system = await fstore.doc_get("systems", system_id) if not system: raise HTTPException(404, f"System '{system_id}' not found.") return system @router.post("", status_code=201) async def create_system(body: SystemCreate): system_id = str(uuid.uuid4()) doc = SystemRecord(system_id=system_id, **body.model_dump()) await fstore.doc_set("systems", system_id, doc.model_dump(), merge=False) return doc @router.put("/{system_id}") async def update_system(system_id: str, body: SystemCreate): existing = await fstore.doc_get("systems", system_id) if not existing: raise HTTPException(404, f"System '{system_id}' not found.") await fstore.doc_update("systems", system_id, body.model_dump()) return {**existing, **body.model_dump()} @router.delete("/{system_id}", status_code=204) async def delete_system(system_id: str): existing = await fstore.doc_get("systems", system_id) if not existing: raise HTTPException(404, f"System '{system_id}' not found.") await fstore.doc_delete("systems", system_id)