59 lines
1.9 KiB
Python
59 lines
1.9 KiB
Python
from uuid import UUID
|
|
|
|
from fastapi import APIRouter, Depends, Query
|
|
from sqlalchemy.ext.asyncio import AsyncSession
|
|
|
|
from app.core.database import get_db
|
|
from app.models.case import Case, CaseStatus
|
|
from app.repositories.case_repo import CaseRepository
|
|
from app.schemas.case import CaseCreate, CaseUpdate, CaseOut, CaseListOut
|
|
|
|
router = APIRouter()
|
|
|
|
|
|
@router.post("", response_model=CaseOut, status_code=201)
|
|
async def create_case(body: CaseCreate, db: AsyncSession = Depends(get_db)):
|
|
repo = CaseRepository(db)
|
|
case = Case(
|
|
case_no=body.case_no,
|
|
title=body.title,
|
|
victim_name=body.victim_name,
|
|
handler=body.handler,
|
|
)
|
|
case = await repo.create(case)
|
|
return case
|
|
|
|
|
|
@router.get("", response_model=CaseListOut)
|
|
async def list_cases(
|
|
offset: int = Query(0, ge=0),
|
|
limit: int = Query(50, ge=1, le=200),
|
|
status: CaseStatus | None = None,
|
|
search: str | None = None,
|
|
db: AsyncSession = Depends(get_db),
|
|
):
|
|
repo = CaseRepository(db)
|
|
items, total = await repo.list_cases(offset=offset, limit=limit, status=status, search=search)
|
|
return CaseListOut(items=items, total=total)
|
|
|
|
|
|
@router.get("/{case_id}", response_model=CaseOut)
|
|
async def get_case(case_id: UUID, db: AsyncSession = Depends(get_db)):
|
|
repo = CaseRepository(db)
|
|
case = await repo.get(case_id)
|
|
if not case:
|
|
from fastapi import HTTPException
|
|
raise HTTPException(status_code=404, detail="案件不存在")
|
|
return case
|
|
|
|
|
|
@router.patch("/{case_id}", response_model=CaseOut)
|
|
async def update_case(case_id: UUID, body: CaseUpdate, db: AsyncSession = Depends(get_db)):
|
|
repo = CaseRepository(db)
|
|
case = await repo.get(case_id)
|
|
if not case:
|
|
from fastapi import HTTPException
|
|
raise HTTPException(status_code=404, detail="案件不存在")
|
|
case = await repo.update(case, body.model_dump(exclude_unset=True))
|
|
return case
|