29e3047792
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
104 lines
3.1 KiB
Python
104 lines
3.1 KiB
Python
import uuid
|
|
|
|
from fastapi import APIRouter, Depends, HTTPException
|
|
from sqlalchemy import func, select
|
|
from sqlalchemy.ext.asyncio import AsyncSession
|
|
from sqlalchemy.orm import selectinload
|
|
|
|
from app.api.deps import get_current_user
|
|
from app.database import get_db
|
|
from app.models.protocol import Protocol
|
|
from app.models.user import User
|
|
from app.schemas.protocol import ProtocolList, ProtocolRegenerate, ProtocolResponse
|
|
from app.tasks.protocol import generate_protocol
|
|
|
|
router = APIRouter(prefix="/api/protocols", tags=["protocols"])
|
|
|
|
|
|
@router.get("", response_model=ProtocolList)
|
|
async def list_protocols(
|
|
skip: int = 0,
|
|
limit: int = 50,
|
|
db: AsyncSession = Depends(get_db),
|
|
user: User = Depends(get_current_user),
|
|
):
|
|
total = (await db.execute(select(func.count(Protocol.id)))).scalar()
|
|
result = await db.execute(
|
|
select(Protocol)
|
|
.order_by(Protocol.created_at.desc())
|
|
.offset(skip)
|
|
.limit(limit)
|
|
)
|
|
items = list(result.scalars().all())
|
|
return ProtocolList(items=items, total=total)
|
|
|
|
|
|
@router.get("/{protocol_id}", response_model=ProtocolResponse)
|
|
async def get_protocol(
|
|
protocol_id: uuid.UUID,
|
|
db: AsyncSession = Depends(get_db),
|
|
user: User = Depends(get_current_user),
|
|
):
|
|
result = await db.execute(
|
|
select(Protocol).where(Protocol.id == protocol_id)
|
|
)
|
|
protocol = result.scalar_one_or_none()
|
|
if not protocol:
|
|
raise HTTPException(404, "Protocol not found")
|
|
return protocol
|
|
|
|
|
|
@router.post("/{protocol_id}/regenerate", status_code=202)
|
|
async def regenerate_protocol(
|
|
protocol_id: uuid.UUID,
|
|
data: ProtocolRegenerate,
|
|
db: AsyncSession = Depends(get_db),
|
|
user: User = Depends(get_current_user),
|
|
):
|
|
result = await db.execute(
|
|
select(Protocol).where(Protocol.id == protocol_id)
|
|
)
|
|
protocol = result.scalar_one_or_none()
|
|
if not protocol:
|
|
raise HTTPException(404, "Protocol not found")
|
|
|
|
template_id = str(data.template_id) if data.template_id else None
|
|
generate_protocol.delay(str(protocol.transcription_id), template_id)
|
|
return {"message": "Regeneration started"}
|
|
|
|
|
|
@router.put("/{protocol_id}", response_model=ProtocolResponse)
|
|
async def update_protocol_content(
|
|
protocol_id: uuid.UUID,
|
|
content: dict,
|
|
db: AsyncSession = Depends(get_db),
|
|
user: User = Depends(get_current_user),
|
|
):
|
|
result = await db.execute(
|
|
select(Protocol).where(Protocol.id == protocol_id)
|
|
)
|
|
protocol = result.scalar_one_or_none()
|
|
if not protocol:
|
|
raise HTTPException(404, "Protocol not found")
|
|
|
|
protocol.content = content
|
|
await db.commit()
|
|
await db.refresh(protocol)
|
|
return protocol
|
|
|
|
|
|
@router.delete("/{protocol_id}", status_code=204)
|
|
async def delete_protocol(
|
|
protocol_id: uuid.UUID,
|
|
db: AsyncSession = Depends(get_db),
|
|
user: User = Depends(get_current_user),
|
|
):
|
|
result = await db.execute(
|
|
select(Protocol).where(Protocol.id == protocol_id)
|
|
)
|
|
protocol = result.scalar_one_or_none()
|
|
if not protocol:
|
|
raise HTTPException(404, "Protocol not found")
|
|
await db.delete(protocol)
|
|
await db.commit()
|