|
| 1 | +"""语义层管理 API""" |
| 2 | + |
| 3 | +from uuid import UUID |
| 4 | + |
| 5 | +from fastapi import APIRouter, Depends, HTTPException, status |
| 6 | +from sqlalchemy import select |
| 7 | +from sqlalchemy.ext.asyncio import AsyncSession |
| 8 | + |
| 9 | +from app.api.deps import get_current_user |
| 10 | +from app.db import get_db |
| 11 | +from app.db.tables import SemanticTerm, User |
| 12 | +from app.models import ( |
| 13 | + APIResponse, |
| 14 | + SemanticTermCreate, |
| 15 | + SemanticTermResponse, |
| 16 | + SemanticTermUpdate, |
| 17 | +) |
| 18 | + |
| 19 | +router = APIRouter(prefix="/semantic/terms", tags=["semantic"]) |
| 20 | + |
| 21 | + |
| 22 | +@router.get("", response_model=APIResponse[list[SemanticTermResponse]]) |
| 23 | +async def list_terms( |
| 24 | + connection_id: UUID | None = None, |
| 25 | + current_user: User = Depends(get_current_user), |
| 26 | + db: AsyncSession = Depends(get_db), |
| 27 | +): |
| 28 | + """获取语义术语列表""" |
| 29 | + query = select(SemanticTerm).where( |
| 30 | + SemanticTerm.user_id == current_user.id, |
| 31 | + SemanticTerm.is_active.is_(True), |
| 32 | + ) |
| 33 | + if connection_id: |
| 34 | + query = query.where(SemanticTerm.connection_id == connection_id) |
| 35 | + |
| 36 | + query = query.order_by(SemanticTerm.term) |
| 37 | + result = await db.execute(query) |
| 38 | + terms = result.scalars().all() |
| 39 | + |
| 40 | + return APIResponse.ok(data=[SemanticTermResponse.model_validate(t) for t in terms]) |
| 41 | + |
| 42 | + |
| 43 | +@router.post("", response_model=APIResponse[SemanticTermResponse]) |
| 44 | +async def create_term( |
| 45 | + term_in: SemanticTermCreate, |
| 46 | + current_user: User = Depends(get_current_user), |
| 47 | + db: AsyncSession = Depends(get_db), |
| 48 | +): |
| 49 | + """创建语义术语""" |
| 50 | + # 检查术语是否已存在 |
| 51 | + existing = await db.execute( |
| 52 | + select(SemanticTerm).where( |
| 53 | + SemanticTerm.user_id == current_user.id, |
| 54 | + SemanticTerm.term == term_in.term, |
| 55 | + SemanticTerm.connection_id == term_in.connection_id, |
| 56 | + ) |
| 57 | + ) |
| 58 | + if existing.scalar_one_or_none(): |
| 59 | + raise HTTPException( |
| 60 | + status_code=status.HTTP_400_BAD_REQUEST, |
| 61 | + detail=f"术语 '{term_in.term}' 已存在", |
| 62 | + ) |
| 63 | + |
| 64 | + term = SemanticTerm( |
| 65 | + user_id=current_user.id, |
| 66 | + term=term_in.term, |
| 67 | + expression=term_in.expression, |
| 68 | + term_type=term_in.term_type, |
| 69 | + connection_id=term_in.connection_id, |
| 70 | + description=term_in.description, |
| 71 | + examples=term_in.examples, |
| 72 | + ) |
| 73 | + db.add(term) |
| 74 | + await db.commit() |
| 75 | + await db.refresh(term) |
| 76 | + |
| 77 | + return APIResponse.ok(data=SemanticTermResponse.model_validate(term), message="术语已创建") |
| 78 | + |
| 79 | + |
| 80 | +@router.get("/{term_id}", response_model=APIResponse[SemanticTermResponse]) |
| 81 | +async def get_term( |
| 82 | + term_id: UUID, |
| 83 | + current_user: User = Depends(get_current_user), |
| 84 | + db: AsyncSession = Depends(get_db), |
| 85 | +): |
| 86 | + """获取单个语义术语""" |
| 87 | + result = await db.execute( |
| 88 | + select(SemanticTerm).where( |
| 89 | + SemanticTerm.id == term_id, |
| 90 | + SemanticTerm.user_id == current_user.id, |
| 91 | + ) |
| 92 | + ) |
| 93 | + term = result.scalar_one_or_none() |
| 94 | + |
| 95 | + if not term: |
| 96 | + raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail="术语不存在") |
| 97 | + |
| 98 | + return APIResponse.ok(data=SemanticTermResponse.model_validate(term)) |
| 99 | + |
| 100 | + |
| 101 | +@router.put("/{term_id}", response_model=APIResponse[SemanticTermResponse]) |
| 102 | +async def update_term( |
| 103 | + term_id: UUID, |
| 104 | + term_in: SemanticTermUpdate, |
| 105 | + current_user: User = Depends(get_current_user), |
| 106 | + db: AsyncSession = Depends(get_db), |
| 107 | +): |
| 108 | + """更新语义术语""" |
| 109 | + result = await db.execute( |
| 110 | + select(SemanticTerm).where( |
| 111 | + SemanticTerm.id == term_id, |
| 112 | + SemanticTerm.user_id == current_user.id, |
| 113 | + ) |
| 114 | + ) |
| 115 | + term = result.scalar_one_or_none() |
| 116 | + |
| 117 | + if not term: |
| 118 | + raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail="术语不存在") |
| 119 | + |
| 120 | + # 更新字段 |
| 121 | + update_data = term_in.model_dump(exclude_unset=True) |
| 122 | + for field, value in update_data.items(): |
| 123 | + setattr(term, field, value) |
| 124 | + |
| 125 | + await db.commit() |
| 126 | + await db.refresh(term) |
| 127 | + |
| 128 | + return APIResponse.ok(data=SemanticTermResponse.model_validate(term), message="术语已更新") |
| 129 | + |
| 130 | + |
| 131 | +@router.delete("/{term_id}", response_model=APIResponse[dict]) |
| 132 | +async def delete_term( |
| 133 | + term_id: UUID, |
| 134 | + current_user: User = Depends(get_current_user), |
| 135 | + db: AsyncSession = Depends(get_db), |
| 136 | +): |
| 137 | + """删除语义术语""" |
| 138 | + result = await db.execute( |
| 139 | + select(SemanticTerm).where( |
| 140 | + SemanticTerm.id == term_id, |
| 141 | + SemanticTerm.user_id == current_user.id, |
| 142 | + ) |
| 143 | + ) |
| 144 | + term = result.scalar_one_or_none() |
| 145 | + |
| 146 | + if not term: |
| 147 | + raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail="术语不存在") |
| 148 | + |
| 149 | + await db.delete(term) |
| 150 | + await db.commit() |
| 151 | + |
| 152 | + return APIResponse.ok(message="术语已删除") |
0 commit comments