first commit
This commit is contained in:
48
backend/app/api/v1/reports.py
Normal file
48
backend/app/api/v1/reports.py
Normal file
@@ -0,0 +1,48 @@
|
||||
from uuid import UUID
|
||||
|
||||
from fastapi import APIRouter, Depends, HTTPException
|
||||
from fastapi.responses import FileResponse
|
||||
from sqlalchemy import select
|
||||
from sqlalchemy.ext.asyncio import AsyncSession
|
||||
|
||||
from app.core.config import settings
|
||||
from app.core.database import get_db
|
||||
from app.models.report import ExportReport
|
||||
from app.repositories.case_repo import CaseRepository
|
||||
from app.schemas.report import ReportCreate, ReportOut, ReportListOut
|
||||
|
||||
router = APIRouter()
|
||||
|
||||
|
||||
@router.post("/cases/{case_id}/reports", response_model=ReportOut, status_code=201)
|
||||
async def generate_report(case_id: UUID, body: ReportCreate, db: AsyncSession = Depends(get_db)):
|
||||
repo = CaseRepository(db)
|
||||
case = await repo.get(case_id)
|
||||
if not case:
|
||||
raise HTTPException(404, "案件不存在")
|
||||
|
||||
from app.services.report_service import generate_report as gen
|
||||
report = await gen(case_id, body, db)
|
||||
return report
|
||||
|
||||
|
||||
@router.get("/cases/{case_id}/reports", response_model=ReportListOut)
|
||||
async def list_reports(case_id: UUID, db: AsyncSession = Depends(get_db)):
|
||||
result = await db.execute(
|
||||
select(ExportReport)
|
||||
.where(ExportReport.case_id == case_id)
|
||||
.order_by(ExportReport.created_at.desc())
|
||||
)
|
||||
items = list(result.scalars().all())
|
||||
return ReportListOut(items=items, total=len(items))
|
||||
|
||||
|
||||
@router.get("/reports/{report_id}/download")
|
||||
async def download_report(report_id: UUID, db: AsyncSession = Depends(get_db)):
|
||||
report = await db.get(ExportReport, report_id)
|
||||
if not report:
|
||||
raise HTTPException(404, "报告不存在")
|
||||
full_path = settings.upload_path / report.file_path
|
||||
if not full_path.exists():
|
||||
raise HTTPException(404, "报告文件不存在")
|
||||
return FileResponse(full_path, filename=full_path.name)
|
||||
Reference in New Issue
Block a user