Files
banban/talkingq-url/api/system_config.py
2026-03-24 15:04:36 +08:00

68 lines
2.5 KiB
Python

from fastapi import APIRouter, HTTPException, Depends, Query
from typing import Optional, List
from pydantic import BaseModel
from services.system_config_manager import system_config_manager
from api.auth import api_auth
router = APIRouter(prefix="/api/system-config")
class SystemConfigResponse(BaseModel):
key: str
value: str
created_at: Optional[float] = None
updated_at: Optional[float] = None
class SystemConfigCreate(BaseModel):
key: str
value: str
class SystemConfigUpdate(BaseModel):
value: str
@router.get("/", response_model=List[SystemConfigResponse])
async def list_configs(authenticated_device_id: str = Depends(api_auth)):
"""列出所有系统配置"""
configs = await system_config_manager.list_configs()
return [SystemConfigResponse(**c.__dict__) for c in configs]
@router.get("/{key}", response_model=SystemConfigResponse)
async def get_config(key: str, authenticated_device_id: str = Depends(api_auth)):
"""获取指定key的系统配置"""
config = await system_config_manager.get_config(key)
if not config:
raise HTTPException(status_code=404, detail="配置不存在")
return SystemConfigResponse(**config.__dict__)
@router.post("/", response_model=SystemConfigResponse)
async def create_config(
config: SystemConfigCreate,
authenticated_device_id: str = Depends(api_auth)
):
"""新增系统配置"""
success = await system_config_manager.create_config(config.key, config.value)
if not success:
raise HTTPException(status_code=400, detail="创建配置失败")
new_config = await system_config_manager.get_config(config.key)
return SystemConfigResponse(**new_config.__dict__)
@router.put("/{key}", response_model=SystemConfigResponse)
async def update_config(
key: str,
config: SystemConfigUpdate,
authenticated_device_id: str = Depends(api_auth)
):
"""更新系统配置"""
success = await system_config_manager.update_config(key, config.value)
if not success:
raise HTTPException(status_code=400, detail="更新配置失败")
updated_config = await system_config_manager.get_config(key)
return SystemConfigResponse(**updated_config.__dict__)
@router.delete("/{key}")
async def delete_config(key: str, authenticated_device_id: str = Depends(api_auth)):
"""删除系统配置"""
success = await system_config_manager.delete_config(key)
if not success:
raise HTTPException(status_code=400, detail="删除配置失败")
return {"detail": "删除成功"}