Files
yaoyaoai/app/api/v1/oem_desktop_configs.py
fengchuanhn@gmail.com 91fb42bf91 11
2026-05-21 16:00:04 +08:00

96 lines
2.7 KiB
Python

"""管理员 desktop_configs 键值 CRUD。"""
from fastapi import APIRouter, Query
from app.dependencies import OemUser, DbSession
from app.schemas.admin_desktop_config import (
DesktopConfigCreate,
DesktopConfigOut,
DesktopConfigUpdate,
)
from app.schemas.common import ApiResponse, PaginatedData
from app.services import admin_desktop_config as config_service
router = APIRouter(prefix="/oem/desktop-configs", tags=["管理-桌面配置"])
@router.get("", response_model=ApiResponse[PaginatedData[DesktopConfigOut]])
async def list_configs(
_admin: OemUser,
db: DbSession,
page: int = Query(1, ge=1),
page_size: int = Query(
config_service.DEFAULT_PAGE_SIZE,
ge=1,
le=config_service.MAX_PAGE_SIZE,
),
keyword: str | None = Query(default=None, max_length=64, description="按键名模糊搜索"),
) -> ApiResponse[PaginatedData[DesktopConfigOut]]:
rows, total = await config_service.list_configs(
db,
page=page,
page_size=page_size,
keyword=keyword,
)
return ApiResponse(
ok=True,
message="",
data=PaginatedData.build(
[DesktopConfigOut.model_validate(r) for r in rows],
total=total,
page=page,
page_size=page_size,
),
)
@router.post("", response_model=ApiResponse[DesktopConfigOut])
async def create_config(
body: DesktopConfigCreate,
_admin: OemUser,
db: DbSession,
) -> ApiResponse[DesktopConfigOut]:
try:
row = await config_service.create_config(db, body)
except config_service.AdminDesktopConfigError as exc:
return ApiResponse(ok=False, message=exc.message)
return ApiResponse(
ok=True,
message="配置已创建",
data=DesktopConfigOut.model_validate(row),
)
@router.patch("/{config_id}", response_model=ApiResponse[DesktopConfigOut])
async def update_config(
config_id: int,
body: DesktopConfigUpdate,
_admin: OemUser,
db: DbSession,
) -> ApiResponse[DesktopConfigOut]:
try:
row = await config_service.update_config(db, config_id, body)
except config_service.AdminDesktopConfigError as exc:
return ApiResponse(ok=False, message=exc.message)
return ApiResponse(
ok=True,
message="配置已更新",
data=DesktopConfigOut.model_validate(row),
)
@router.delete("/{config_id}", response_model=ApiResponse[None])
async def delete_config(
config_id: int,
_admin: OemUser,
db: DbSession,
) -> ApiResponse[None]:
try:
await config_service.delete_config(db, config_id)
except config_service.AdminDesktopConfigError as exc:
return ApiResponse(ok=False, message=exc.message)
return ApiResponse(ok=True, message="配置已删除")