Files
fastapi/app/api/evaluations.py
T
2026-06-04 10:55:23 +08:00

40 lines
1.6 KiB
Python

from fastapi import APIRouter, Depends
from sqlalchemy.orm import Session
from app.core.response import ApiResponse, ok
from app.core.user_context import UserContext, get_user_context
from app.db.session import get_db
from app.schemas.evaluation import EvaluationDetailResponse, EvaluationListResponse, ExportPdfResponse
from app.services.evaluation_service import EvaluationService
from app.services.pdf_export_service import PdfExportService
router = APIRouter()
@router.get("", response_model=ApiResponse[EvaluationListResponse])
def list_evaluations(ctx: UserContext = Depends(get_user_context), db: Session = Depends(get_db)):
"""历史评价:基于 user_id 查询完整训练后的评价记录。"""
return ok(EvaluationService(db).list_history(ctx.user_id))
@router.get("/{evaluation_id}", response_model=ApiResponse[EvaluationDetailResponse])
def get_evaluation_detail(
evaluation_id: int,
ctx: UserContext = Depends(get_user_context),
db: Session = Depends(get_db),
):
"""评价详情:校验 user_id 后返回完整评价报告。"""
return ok(EvaluationService(db).get_detail(evaluation_id, ctx.user_id))
@router.post("/{evaluation_id}/export-pdf", response_model=ApiResponse[ExportPdfResponse])
def export_pdf(
evaluation_id: int,
ctx: UserContext = Depends(get_user_context),
db: Session = Depends(get_db),
):
"""PDF 导出:生成评价报告 PDF 并保存导出记录。"""
export = PdfExportService(db).export(evaluation_id, ctx.user_id)
db.commit()
return ok(ExportPdfResponse(export_id=export.id, file_path=export.file_path))