Refactor models and routes for item management
Reorganized model structure by replacing 'object' and 'items' with a unified 'item' model using UUIDs, and moved base model logic into separate files. Updated routes to use the new item model and improved request/response handling. Enhanced user and setting models, added utility functions, and improved error handling throughout the codebase. Also added initial .idea project files and minor admin API improvements. Co-Authored-By: 砂糖橘 <54745033+Foxerine@users.noreply.github.com>
This commit is contained in:
@@ -1,9 +1,12 @@
|
||||
from fastapi import APIRouter
|
||||
from typing import Annotated
|
||||
|
||||
from fastapi import APIRouter, HTTPException
|
||||
from fastapi import Depends
|
||||
from model.response import DefaultResponse
|
||||
from sqlalchemy.ext.asyncio import AsyncSession
|
||||
|
||||
from middleware.admin import is_admin
|
||||
|
||||
from model import database, Setting, SettingResponse
|
||||
from model.response import DefaultResponse
|
||||
|
||||
Router = APIRouter(
|
||||
prefix='/api/admin',
|
||||
@@ -25,4 +28,52 @@ async def verity_admin() -> DefaultResponse:
|
||||
- 若为管理员,返回 `True`
|
||||
- 若不是管理员,抛出 `401` 错误
|
||||
'''
|
||||
return DefaultResponse(data=True)
|
||||
|
||||
@Router.get(
|
||||
path='api/admin/settings',
|
||||
summary='获取设置项',
|
||||
description='获取设置项, 留空则获取所有',
|
||||
response_model=DefaultResponse,
|
||||
response_description='设置项列表'
|
||||
)
|
||||
async def get_settings(
|
||||
session: Annotated[AsyncSession, Depends(database.Database.get_session)],
|
||||
name: str | None = None
|
||||
) -> DefaultResponse:
|
||||
data = []
|
||||
|
||||
if name:
|
||||
setting = await Setting.get(session, Setting.name == name)
|
||||
if setting:
|
||||
data.append(SettingResponse.model_validate(setting))
|
||||
else:
|
||||
raise HTTPException(404, detail="Setting not found")
|
||||
else:
|
||||
settings = await Setting.get(session, fetch_mode="all")
|
||||
if settings:
|
||||
data = [SettingResponse.model_validate(s) for s in settings]
|
||||
|
||||
return DefaultResponse(data=data)
|
||||
|
||||
|
||||
@Router.put(
|
||||
path='api/admin/settings',
|
||||
summary='更新设置项',
|
||||
description='更新设置项',
|
||||
response_model=DefaultResponse,
|
||||
response_description='更新结果'
|
||||
)
|
||||
async def update_settings(
|
||||
session: Annotated[AsyncSession, Depends(database.Database.get_session)],
|
||||
name: str,
|
||||
value: str
|
||||
) -> DefaultResponse:
|
||||
setting = await Setting.get(session, Setting.name == name)
|
||||
if not setting:
|
||||
raise HTTPException(404, detail="Setting not found")
|
||||
|
||||
setting.value = value
|
||||
await Setting.save(session)
|
||||
|
||||
return DefaultResponse(data=True)
|
||||
Reference in New Issue
Block a user