133 lines
4.1 KiB
Python
Raw Normal View History

2025-08-02 12:38:52 +08:00
from fastapi import APIRouter, Depends, HTTPException, Query
from sqlalchemy.orm import Session
2025-08-05 11:57:14 +08:00
from typing import List, Optional, Dict, Any
2025-08-02 12:38:52 +08:00
from core.database import get_db
from models.algorithm import Algorithm
router = APIRouter()
2025-08-05 11:57:14 +08:00
@router.post("/", summary="创建算法")
2025-08-02 12:38:52 +08:00
async def create_algorithm(
2025-08-05 11:57:14 +08:00
algorithm_data: Dict[str, Any],
2025-08-02 12:38:52 +08:00
db: Session = Depends(get_db)
):
"""创建新的算法"""
2025-08-05 11:57:14 +08:00
db_algorithm = Algorithm(**algorithm_data)
2025-08-02 12:38:52 +08:00
db.add(db_algorithm)
db.commit()
db.refresh(db_algorithm)
2025-08-05 11:57:14 +08:00
return db_algorithm.to_dict()
2025-08-02 12:38:52 +08:00
2025-08-05 11:57:14 +08:00
@router.get("/", summary="获取算法列表")
2025-08-02 12:38:52 +08:00
async def get_algorithms(
skip: int = Query(0, ge=0, description="跳过记录数"),
limit: int = Query(10, ge=1, le=100, description="返回记录数"),
name: Optional[str] = Query(None, description="算法名称"),
status: Optional[str] = Query(None, description="算法状态"),
is_enabled: Optional[bool] = Query(None, description="是否启用"),
db: Session = Depends(get_db)
):
"""获取算法列表,支持分页和筛选"""
query = db.query(Algorithm)
if name:
query = query.filter(Algorithm.name.contains(name))
if status:
query = query.filter(Algorithm.status == status)
if is_enabled is not None:
query = query.filter(Algorithm.is_enabled == is_enabled)
total = query.count()
algorithms = query.offset(skip).limit(limit).all()
2025-08-05 11:57:14 +08:00
algorithm_list = [algorithm.to_dict() for algorithm in algorithms]
return {
"algorithms": algorithm_list,
"total": total,
"page": skip // limit + 1,
"size": limit
}
2025-08-02 12:38:52 +08:00
2025-08-05 11:57:14 +08:00
@router.get("/{algorithm_id}", summary="获取算法详情")
2025-08-02 12:38:52 +08:00
async def get_algorithm(
algorithm_id: int,
db: Session = Depends(get_db)
):
"""根据ID获取算法详情"""
algorithm = db.query(Algorithm).filter(Algorithm.id == algorithm_id).first()
if not algorithm:
raise HTTPException(status_code=404, detail="算法不存在")
2025-08-05 11:57:14 +08:00
return algorithm.to_dict()
2025-08-02 12:38:52 +08:00
2025-08-05 11:57:14 +08:00
@router.put("/{algorithm_id}", summary="更新算法")
2025-08-02 12:38:52 +08:00
async def update_algorithm(
algorithm_id: int,
2025-08-05 11:57:14 +08:00
algorithm_data: Dict[str, Any],
2025-08-02 12:38:52 +08:00
db: Session = Depends(get_db)
):
"""更新算法信息"""
db_algorithm = db.query(Algorithm).filter(Algorithm.id == algorithm_id).first()
if not db_algorithm:
raise HTTPException(status_code=404, detail="算法不存在")
2025-08-05 11:57:14 +08:00
for field, value in algorithm_data.items():
if hasattr(db_algorithm, field):
setattr(db_algorithm, field, value)
2025-08-02 12:38:52 +08:00
db.commit()
db.refresh(db_algorithm)
2025-08-05 11:57:14 +08:00
return db_algorithm.to_dict()
2025-08-02 12:38:52 +08:00
@router.delete("/{algorithm_id}", summary="删除算法")
async def delete_algorithm(
algorithm_id: int,
db: Session = Depends(get_db)
):
"""删除算法"""
algorithm = db.query(Algorithm).filter(Algorithm.id == algorithm_id).first()
if not algorithm:
raise HTTPException(status_code=404, detail="算法不存在")
db.delete(algorithm)
db.commit()
return {"message": "算法删除成功"}
2025-08-05 11:57:14 +08:00
@router.patch("/{algorithm_id}/status", summary="更新算法状态")
2025-08-02 12:38:52 +08:00
async def update_algorithm_status(
algorithm_id: int,
status: str = Query(..., description="新状态"),
db: Session = Depends(get_db)
):
"""更新算法状态"""
algorithm = db.query(Algorithm).filter(Algorithm.id == algorithm_id).first()
if not algorithm:
raise HTTPException(status_code=404, detail="算法不存在")
algorithm.status = status
db.commit()
db.refresh(algorithm)
2025-08-05 11:57:14 +08:00
return algorithm.to_dict()
2025-08-02 12:38:52 +08:00
2025-08-05 11:57:14 +08:00
@router.patch("/{algorithm_id}/enable", summary="启用/禁用算法")
2025-08-02 12:38:52 +08:00
async def toggle_algorithm_enabled(
algorithm_id: int,
enabled: bool = Query(..., description="是否启用"),
db: Session = Depends(get_db)
):
"""启用或禁用算法"""
algorithm = db.query(Algorithm).filter(Algorithm.id == algorithm_id).first()
if not algorithm:
raise HTTPException(status_code=404, detail="算法不存在")
algorithm.is_enabled = enabled
db.commit()
db.refresh(algorithm)
2025-08-05 11:57:14 +08:00
2025-08-06 14:45:07 +08:00
return algorithm.to_dict()