from fastapi import APIRouter, Depends, Header, Request, Response from app.api.dependencies import csrf_actor, current_actor from app.schemas.stories import ( StoryCreate, StoryPatch, CandidateCreate, CandidateBatch, SelectionCreate, SelectionClear, RenderCreate, Version, ) from app.services.stories import api as service from app.services.stories.candidates import capabilities router = APIRouter(prefix="/stories", tags=["stories"]) @router.get("/capabilities") async def get_capabilities(request: Request, actor=Depends(current_actor)): return await capabilities(request.app.state) @router.get("") async def listing( request: Request, page: int = 1, page_size: int = 24, folder_id: str = "", actor=Depends(current_actor), ): return await service.listing(request.app.state, actor, page, page_size, folder_id) @router.post("", status_code=201) async def create(body: StoryCreate, request: Request, actor=Depends(csrf_actor)): return await service.create(request.app.state, actor, body) @router.get("/{id}") async def get(id: str, request: Request, actor=Depends(current_actor)): return await service.get(request.app.state, actor, id) @router.patch("/{id}") async def patch( id: str, body: StoryPatch, request: Request, if_match: str | None = Header(None), actor=Depends(csrf_actor), ): return await service.patch(request.app.state, actor, id, body, if_match) @router.delete("/{id}", responses={202: {"description": "故事已隐藏,正在等待执行清理"}}) async def delete_story( id: str, request: Request, response: Response, if_match: str | None = Header(None), actor=Depends(csrf_actor), ): from app.services.stories.deletion import delete_story as remove result = await remove(request.app.state, actor, id, if_match) if result.get("pending"): response.status_code = 202 return result @router.get("/{id}/film") async def latest_film(id: str, request: Request, actor=Depends(current_actor)): return await service.latest_film(request.app.state, actor, id) @router.get("/{id}/preview") async def preview(id: str, request: Request, actor=Depends(current_actor)): return await service.get(request.app.state, actor, id, "preview") @router.get("/{id}/readiness") async def readiness(id: str, request: Request, actor=Depends(current_actor)): return await service.get(request.app.state, actor, id, "readiness") @router.get("/{id}/candidates") async def candidates( id: str, request: Request, shot_id: str | None = None, kind: str | None = None, page: int = 1, page_size: int = 24, actor=Depends(current_actor), ): return await service.candidates_list( request.app.state, actor, id, shot_id, kind, page, page_size ) @router.post("/{id}/candidates", status_code=202) async def create_candidates( id: str, body: CandidateCreate, request: Request, idempotency_key: str | None = Header(None), actor=Depends(csrf_actor), ): return await service.create_candidates(request.app.state, actor, id, body, idempotency_key) @router.post("/{id}/candidate-batches", status_code=202) async def create_batch( id: str, body: CandidateBatch, request: Request, idempotency_key: str | None = Header(None), actor=Depends(csrf_actor), ): return await service.create_candidates( request.app.state, actor, id, body, idempotency_key, batch=True ) @router.post("/{id}/candidates/{candidate_id}/retry", status_code=202) async def retry( id: str, candidate_id: str, body: Version, request: Request, idempotency_key: str | None = Header(None), actor=Depends(csrf_actor), ): return await service.create_candidates( request.app.state, actor, id, body, idempotency_key, retry_id=candidate_id ) @router.delete("/{id}/candidates/{candidate_id}") async def delete_candidate( id: str, candidate_id: str, request: Request, if_match: str | None = Header(None), actor=Depends(csrf_actor), ): from app.services.stories.deletion import delete_candidate as remove return await remove(request.app.state, actor, id, candidate_id, if_match) @router.post("/{id}/selections") async def selection( id: str, body: SelectionCreate, request: Request, idempotency_key: str | None = Header(None), actor=Depends(csrf_actor), ): return await service.select_candidate(request.app.state, actor, id, body, idempotency_key) @router.post("/{id}/selections/clear") async def clear( id: str, body: SelectionClear, request: Request, idempotency_key: str | None = Header(None), actor=Depends(csrf_actor), ): return await service.select_candidate( request.app.state, actor, id, body, idempotency_key, clear=True ) @router.post("/{id}/renders", status_code=202) async def render( id: str, body: RenderCreate, request: Request, idempotency_key: str | None = Header(None), actor=Depends(csrf_actor), ): return await service.render(request.app.state, actor, id, body, idempotency_key) @router.get("/{id}/runs") async def runs( id: str, request: Request, page: int = 1, page_size: int = 24, actor=Depends(current_actor) ): return await service.runs(request.app.state, actor, id, page, page_size)