init
This commit is contained in:
64
backend/app/api/stories.py
Normal file
64
backend/app/api/stories.py
Normal file
@@ -0,0 +1,64 @@
|
||||
from typing import Annotated
|
||||
from fastapi import APIRouter, HTTPException, Depends
|
||||
from sqlalchemy.ext.asyncio import AsyncSession
|
||||
from sqlalchemy import select, update
|
||||
|
||||
from app.database import get_db
|
||||
from app.models.story import Story
|
||||
from app.schemas.schemas import StoryOut, StoryEdit
|
||||
|
||||
router = APIRouter(prefix="/stories", tags=["故事管理"])
|
||||
|
||||
|
||||
@router.get("", response_model=list[StoryOut])
|
||||
async def list_stories(db: Annotated[AsyncSession, Depends(get_db)], status: str = "all"):
|
||||
"""获取故事列表,支持筛选:all/matched/pending"""
|
||||
query = select(Story).where(Story.match_status != "deleted")
|
||||
if status == "matched":
|
||||
query = query.where(Story.match_status == "matched")
|
||||
elif status == "pending":
|
||||
query = query.where(Story.match_status == "pending")
|
||||
query = query.order_by(Story.created_at.desc())
|
||||
result = await db.execute(query)
|
||||
return result.scalars().all()
|
||||
|
||||
|
||||
@router.get("/{story_id}", response_model=StoryOut)
|
||||
async def get_story(story_id: str, db: Annotated[AsyncSession, Depends(get_db)]):
|
||||
"""获取故事详情"""
|
||||
story = await db.get(Story, story_id)
|
||||
if not story or story.match_status == "deleted":
|
||||
raise HTTPException(404, "故事不存在")
|
||||
return story
|
||||
|
||||
|
||||
@router.put("/{story_id}", response_model=StoryOut)
|
||||
async def edit_story(story_id: str, data: StoryEdit, db: Annotated[AsyncSession, Depends(get_db)]):
|
||||
"""编辑故事"""
|
||||
story = await db.get(Story, story_id)
|
||||
if not story or story.match_status == "deleted":
|
||||
raise HTTPException(404, "故事不存在")
|
||||
|
||||
if data.title is not None:
|
||||
story.title = data.title
|
||||
if data.summary is not None:
|
||||
story.summary = data.summary
|
||||
if data.content is not None:
|
||||
story.content = data.content
|
||||
|
||||
await db.commit()
|
||||
await db.refresh(story)
|
||||
return story
|
||||
|
||||
|
||||
@router.delete("/{story_id}")
|
||||
async def delete_story(story_id: str, db: Annotated[AsyncSession, Depends(get_db)]):
|
||||
"""删除故事(标记为 deleted)"""
|
||||
story = await db.get(Story, story_id)
|
||||
if not story or story.match_status == "deleted":
|
||||
raise HTTPException(404, "故事不存在")
|
||||
|
||||
story.match_status = "deleted"
|
||||
story.person_id = None
|
||||
await db.commit()
|
||||
return {"message": "已删除"}
|
||||
Reference in New Issue
Block a user