first commit
This commit is contained in:
@@ -0,0 +1,61 @@
|
||||
"""API 路由:匹配绑定"""
|
||||
|
||||
from typing import List
|
||||
from fastapi import APIRouter, Depends, HTTPException
|
||||
from sqlalchemy.orm import Session
|
||||
|
||||
from app.database import get_db
|
||||
from app import crud, schemas
|
||||
|
||||
router = APIRouter(prefix="/api/matches", tags=["匹配配置"])
|
||||
|
||||
|
||||
def _enrich_match(m) -> schemas.RegimeMatchOut:
|
||||
"""填充关联名称"""
|
||||
return schemas.RegimeMatchOut(
|
||||
id=m.id,
|
||||
market_regime_id=m.market_regime_id,
|
||||
market_cycle=m.market_cycle,
|
||||
trend_strength=m.trend_strength,
|
||||
account_id=m.account_id,
|
||||
strategy_id=m.strategy_id,
|
||||
force_direction=m.force_direction or "",
|
||||
regime_name=m.regime.name if m.regime else None,
|
||||
account_name=m.account.account_name if m.account else None,
|
||||
strategy_name=m.strategy.strategy_name if m.strategy else None,
|
||||
)
|
||||
|
||||
|
||||
@router.get("", response_model=List[schemas.RegimeMatchOut])
|
||||
def list_matches(db: Session = Depends(get_db)):
|
||||
return [_enrich_match(m) for m in crud.get_matches(db)]
|
||||
|
||||
|
||||
@router.post("", response_model=schemas.RegimeMatchOut, status_code=201)
|
||||
def create_match(data: schemas.RegimeMatchCreate, db: Session = Depends(get_db)):
|
||||
obj = crud.create_match(db, data)
|
||||
# 重新加载关联
|
||||
matches = crud.get_matches(db)
|
||||
for m in matches:
|
||||
if m.id == obj.id:
|
||||
return _enrich_match(m)
|
||||
return schemas.RegimeMatchOut(id=obj.id, **data.model_dump())
|
||||
|
||||
|
||||
@router.put("/{match_id}", response_model=schemas.RegimeMatchOut)
|
||||
def update_match(match_id: int, data: schemas.RegimeMatchUpdate, db: Session = Depends(get_db)):
|
||||
obj = crud.update_match(db, match_id, data)
|
||||
if not obj:
|
||||
raise HTTPException(404, "匹配规则不存在")
|
||||
matches = crud.get_matches(db)
|
||||
for m in matches:
|
||||
if m.id == match_id:
|
||||
return _enrich_match(m)
|
||||
raise HTTPException(404, "匹配规则不存在")
|
||||
|
||||
|
||||
@router.delete("/{match_id}")
|
||||
def delete_match(match_id: int, db: Session = Depends(get_db)):
|
||||
if not crud.delete_match(db, match_id):
|
||||
raise HTTPException(404, "匹配规则不存在")
|
||||
return {"ok": True}
|
||||
Reference in New Issue
Block a user