配置更新
This commit is contained in:
parent
05d67751b9
commit
d9e6b10a46
@ -1,6 +1,6 @@
|
||||
from fastapi import APIRouter, Depends
|
||||
from sqlalchemy.orm import Session
|
||||
from app.models.config import ConfigDB, ConfigInfo
|
||||
from app.models.config import ConfigDB, ConfigInfo, ConfigUpdate
|
||||
from app.models.user import UserDB
|
||||
from app.models.database import get_db
|
||||
from app.api.deps import get_admin_user
|
||||
@ -23,6 +23,45 @@ async def get_configs(
|
||||
|
||||
return success_response(data=config_list)
|
||||
|
||||
@router.delete("/{key}", response_model=ResponseModel)
|
||||
async def delete_config(
|
||||
key: str,
|
||||
db: Session = Depends(get_db),
|
||||
admin: UserDB = Depends(get_admin_user)
|
||||
):
|
||||
"""删除配置"""
|
||||
config = db.query(ConfigDB).filter(ConfigDB.key == key).first()
|
||||
if not config:
|
||||
return error_response(code=400, message=f"配置项 {key} 不存在")
|
||||
|
||||
db.delete(config)
|
||||
db.commit()
|
||||
return success_response(message="配置删除成功")
|
||||
|
||||
@router.put("/{key}", response_model=ResponseModel)
|
||||
async def update_config(
|
||||
key: str,
|
||||
config: ConfigUpdate,
|
||||
db: Session = Depends(get_db),
|
||||
admin: UserDB = Depends(get_admin_user)
|
||||
):
|
||||
"""更新配置"""
|
||||
db_config = db.query(ConfigDB).filter(ConfigDB.key == key).first()
|
||||
if not db_config:
|
||||
return error_response(code=400, message=f"配置项 {key} 不存在")
|
||||
|
||||
db_config.value = config.value
|
||||
db_config.description = config.description
|
||||
|
||||
db.commit()
|
||||
db.refresh(db_config)
|
||||
|
||||
return success_response(
|
||||
message="配置更新成功",
|
||||
data=ConfigInfo.model_validate(db_config)
|
||||
)
|
||||
|
||||
|
||||
@router.post("", response_model=ResponseModel)
|
||||
async def create_config(
|
||||
config: ConfigInfo,
|
||||
@ -38,7 +77,8 @@ async def create_config(
|
||||
# 创建新配置
|
||||
db_config = ConfigDB(
|
||||
key=config.key,
|
||||
value=config.value
|
||||
value=config.value,
|
||||
description=config.description
|
||||
)
|
||||
|
||||
try:
|
||||
|
||||
@ -3,19 +3,29 @@ from sqlalchemy.sql import func
|
||||
from pydantic import BaseModel, Field
|
||||
from datetime import datetime
|
||||
from .database import Base
|
||||
|
||||
from typing import Optional
|
||||
class ConfigDB(Base):
|
||||
__tablename__ = "configs"
|
||||
|
||||
id = Column(Integer, primary_key=True, autoincrement=True)
|
||||
key = Column(String(100), unique=True, nullable=False)
|
||||
value = Column(Text, nullable=False)
|
||||
value = Column(String(200), nullable=False)
|
||||
description = Column(String(200), nullable=True)
|
||||
create_time = Column(DateTime(timezone=True), server_default=func.now())
|
||||
update_time = Column(DateTime(timezone=True), onupdate=func.now())
|
||||
|
||||
class ConfigInfo(BaseModel):
|
||||
id: int
|
||||
key: str
|
||||
value: str
|
||||
description: Optional[str] = None
|
||||
|
||||
class Config:
|
||||
from_attributes = True
|
||||
|
||||
class ConfigUpdate(BaseModel):
|
||||
value: str
|
||||
description: Optional[str] = None
|
||||
|
||||
class Config:
|
||||
from_attributes = True
|
||||
Loading…
Reference in New Issue
Block a user