scenes.py 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. """场景位置的增删改查。"""
  2. from typing import Any
  3. from fastapi import APIRouter, HTTPException
  4. from pydantic import BaseModel, Field
  5. from services import scene_service
  6. router = APIRouter()
  7. class SceneCreate(BaseModel):
  8. name: str = Field(min_length=1, max_length=50)
  9. camera: list[float] = Field(min_length=3, max_length=3)
  10. target: list[float] = Field(min_length=3, max_length=3)
  11. class SceneRename(BaseModel):
  12. name: str = Field(min_length=1, max_length=50)
  13. @router.get("/api/scene-positions")
  14. def list_scenes() -> list[dict[str, Any]]:
  15. return scene_service.list_scenes()
  16. @router.post("/api/scene-positions")
  17. def add_scene(payload: SceneCreate) -> dict[str, Any]:
  18. return scene_service.add_scene(payload.name.strip(), payload.camera, payload.target)
  19. @router.put("/api/scene-positions/{scene_id}")
  20. def rename_scene(scene_id: int, payload: SceneRename) -> dict[str, Any]:
  21. scene = scene_service.rename_scene(scene_id, payload.name.strip())
  22. if scene is None:
  23. raise HTTPException(status_code=404, detail="场景位置不存在")
  24. return scene
  25. @router.delete("/api/scene-positions/{scene_id}")
  26. def delete_scene(scene_id: int) -> dict[str, Any]:
  27. if not scene_service.delete_scene(scene_id):
  28. raise HTTPException(status_code=404, detail="场景位置不存在")
  29. return {"ok": True}