from uuid import UUID from fastapi import APIRouter, Depends, Query, HTTPException from sqlalchemy.ext.asyncio import AsyncSession from app.core.database import get_db from app.repositories.transaction_repo import TransactionRepository from app.schemas.transaction import ( TransactionOut, TransactionListOut, FlowGraphOut, TransactionUpdateIn, ) from app.services.flow_service import build_flow_graph router = APIRouter() @router.get("/cases/{case_id}/transactions", response_model=TransactionListOut) async def list_transactions( case_id: UUID, filter_type: str | None = Query(None, description="all / unique / duplicate"), db: AsyncSession = Depends(get_db), ): repo = TransactionRepository(db) items, total = await repo.list_by_case(case_id, filter_type=filter_type) return TransactionListOut(items=items, total=total) @router.get("/transactions/{tx_id}", response_model=TransactionOut) async def get_transaction(tx_id: UUID, db: AsyncSession = Depends(get_db)): repo = TransactionRepository(db) tx = await repo.get(tx_id) if not tx: raise HTTPException(404, "交易不存在") return tx @router.patch("/transactions/{tx_id}", response_model=TransactionOut) async def update_transaction( tx_id: UUID, body: TransactionUpdateIn, db: AsyncSession = Depends(get_db), ): repo = TransactionRepository(db) tx = await repo.get(tx_id) if not tx: raise HTTPException(404, "交易不存在") update_data = body.model_dump(exclude_unset=True) tx = await repo.update(tx, update_data) await db.commit() return tx @router.get("/cases/{case_id}/flows", response_model=FlowGraphOut) async def get_fund_flows(case_id: UUID, db: AsyncSession = Depends(get_db)): return await build_flow_graph(case_id, db)