init
This commit is contained in:
@@ -0,0 +1,93 @@
|
||||
from fastapi import APIRouter, Depends, HTTPException
|
||||
from sqlalchemy import select
|
||||
from sqlalchemy.ext.asyncio import AsyncSession
|
||||
|
||||
from app.models.database import get_session
|
||||
from app.models.orm import Script
|
||||
from app.models.schemas import ScriptUploadRequest, ScriptParsePreview, ScriptImportRequest, ScriptResponse
|
||||
from app.services.script_parser import script_parser
|
||||
|
||||
router = APIRouter()
|
||||
|
||||
|
||||
@router.post("/upload", response_model=ScriptParsePreview)
|
||||
async def upload_script(
|
||||
req: ScriptUploadRequest,
|
||||
session: AsyncSession = Depends(get_session),
|
||||
):
|
||||
parsed = await script_parser.parse(req.content, req.file_type)
|
||||
return ScriptParsePreview(
|
||||
title=parsed["title"],
|
||||
background=parsed["background"],
|
||||
characters=parsed["characters"],
|
||||
clues=parsed["clues"],
|
||||
phases=parsed["phases"],
|
||||
)
|
||||
|
||||
|
||||
@router.post("/import", response_model=ScriptResponse)
|
||||
async def import_script(
|
||||
req: ScriptImportRequest,
|
||||
session: AsyncSession = Depends(get_session),
|
||||
):
|
||||
script = Script(
|
||||
title=req.title,
|
||||
background=req.background,
|
||||
raw_content="",
|
||||
parsed_data={
|
||||
"characters": req.characters,
|
||||
"clues": req.clues,
|
||||
"phases": req.phases,
|
||||
},
|
||||
character_count=len(req.characters),
|
||||
)
|
||||
session.add(script)
|
||||
await session.commit()
|
||||
await session.refresh(script)
|
||||
return script
|
||||
|
||||
|
||||
@router.get("/preview")
|
||||
async def preview_scripts(session: AsyncSession = Depends(get_session)):
|
||||
result = await session.execute(
|
||||
select(Script).order_by(Script.created_at.desc())
|
||||
)
|
||||
scripts = list(result.scalars().all())
|
||||
return [
|
||||
{
|
||||
"id": s.id,
|
||||
"title": s.title,
|
||||
"background": s.background,
|
||||
"character_count": s.character_count,
|
||||
"parsed_data": s.parsed_data,
|
||||
}
|
||||
for s in scripts
|
||||
]
|
||||
|
||||
|
||||
@router.get("", response_model=list[ScriptResponse])
|
||||
async def list_scripts(session: AsyncSession = Depends(get_session)):
|
||||
result = await session.execute(
|
||||
select(Script).order_by(Script.created_at.desc())
|
||||
)
|
||||
return list(result.scalars().all())
|
||||
|
||||
|
||||
@router.get("/{script_id}", response_model=ScriptResponse)
|
||||
async def get_script(script_id: str, session: AsyncSession = Depends(get_session)):
|
||||
result = await session.execute(select(Script).where(Script.id == script_id))
|
||||
script = result.scalars().first()
|
||||
if not script:
|
||||
raise HTTPException(status_code=404, detail="Script not found")
|
||||
return script
|
||||
|
||||
|
||||
@router.delete("/{script_id}")
|
||||
async def delete_script(script_id: str, session: AsyncSession = Depends(get_session)):
|
||||
result = await session.execute(select(Script).where(Script.id == script_id))
|
||||
script = result.scalars().first()
|
||||
if not script:
|
||||
raise HTTPException(status_code=404, detail="Script not found")
|
||||
await session.delete(script)
|
||||
await session.commit()
|
||||
return {"ok": True}
|
||||
Reference in New Issue
Block a user