| 123456789101112131415161718192021222324252627282930313233343536373839404142434445 |
- """场景位置的增删改查。"""
- from typing import Any
- from fastapi import APIRouter, HTTPException
- from pydantic import BaseModel, Field
- from services import scene_service
- router = APIRouter()
- class SceneCreate(BaseModel):
- name: str = Field(min_length=1, max_length=50)
- camera: list[float] = Field(min_length=3, max_length=3)
- target: list[float] = Field(min_length=3, max_length=3)
- class SceneRename(BaseModel):
- name: str = Field(min_length=1, max_length=50)
- @router.get("/api/scene-positions")
- def list_scenes() -> list[dict[str, Any]]:
- return scene_service.list_scenes()
- @router.post("/api/scene-positions")
- def add_scene(payload: SceneCreate) -> dict[str, Any]:
- return scene_service.add_scene(payload.name.strip(), payload.camera, payload.target)
- @router.put("/api/scene-positions/{scene_id}")
- def rename_scene(scene_id: int, payload: SceneRename) -> dict[str, Any]:
- scene = scene_service.rename_scene(scene_id, payload.name.strip())
- if scene is None:
- raise HTTPException(status_code=404, detail="场景位置不存在")
- return scene
- @router.delete("/api/scene-positions/{scene_id}")
- def delete_scene(scene_id: int) -> dict[str, Any]:
- if not scene_service.delete_scene(scene_id):
- raise HTTPException(status_code=404, detail="场景位置不存在")
- return {"ok": True}
|