35 lines
1.1 KiB
Python
35 lines
1.1 KiB
Python
from fastapi import APIRouter, Depends
|
|
from sqlalchemy.ext.asyncio import AsyncSession
|
|
from sqlalchemy import select
|
|
from app.core.database import get_db
|
|
from app.models.system_config import SystemConfig
|
|
|
|
router = APIRouter(prefix="/settings", tags=["系统设置"])
|
|
|
|
|
|
@router.get("")
|
|
async def get_all_settings(db: AsyncSession = Depends(get_db)):
|
|
result = await db.execute(select(SystemConfig))
|
|
configs = result.scalars().all()
|
|
return {c.key: c.value for c in configs}
|
|
|
|
|
|
@router.put("/{key}")
|
|
async def update_setting(key: str, body: dict, db: AsyncSession = Depends(get_db)):
|
|
result = await db.execute(select(SystemConfig).where(SystemConfig.key == key))
|
|
config = result.scalar_one_or_none()
|
|
|
|
value = body.get("value", "")
|
|
description = body.get("description", "")
|
|
|
|
if config:
|
|
config.value = value
|
|
if description:
|
|
config.description = description
|
|
else:
|
|
config = SystemConfig(key=key, value=value, description=description)
|
|
db.add(config)
|
|
|
|
await db.flush()
|
|
return {"key": key, "value": value}
|