feat: 创作中心改造 - 我的改写/续写Tab展示已发布作品
This commit is contained in:
@@ -121,6 +121,8 @@ class StoryDraft(Base):
|
||||
status = Column(Enum(DraftStatus), default=DraftStatus.pending)
|
||||
error_message = Column(String(500), default="")
|
||||
is_read = Column(Boolean, default=False) # 用户是否已查看
|
||||
published_to_center = Column(Boolean, default=False) # 是否发布到创作中心
|
||||
draft_type = Column(String(20), default="rewrite") # 草稿类型: rewrite/continue/create
|
||||
|
||||
created_at = Column(TIMESTAMP, server_default=func.now())
|
||||
completed_at = Column(TIMESTAMP, default=None)
|
||||
|
||||
@@ -374,7 +374,8 @@ async def create_draft(
|
||||
current_node_key=request.currentNodeKey,
|
||||
current_content=request.currentContent,
|
||||
user_prompt=request.prompt,
|
||||
status=DraftStatus.pending
|
||||
status=DraftStatus.pending,
|
||||
draft_type='rewrite'
|
||||
)
|
||||
|
||||
db.add(draft)
|
||||
@@ -419,7 +420,8 @@ async def create_ending_draft(
|
||||
current_node_key=request.endingName, # 保存结局名称
|
||||
current_content=request.endingContent, # 保存结局内容
|
||||
user_prompt=request.prompt,
|
||||
status=DraftStatus.pending
|
||||
status=DraftStatus.pending,
|
||||
draft_type='rewrite'
|
||||
)
|
||||
|
||||
db.add(draft)
|
||||
@@ -464,7 +466,8 @@ async def create_continue_ending_draft(
|
||||
current_node_key=request.endingName, # 保存结局名称
|
||||
current_content=request.endingContent, # 保存结局内容
|
||||
user_prompt=request.prompt,
|
||||
status=DraftStatus.pending
|
||||
status=DraftStatus.pending,
|
||||
draft_type='continue'
|
||||
)
|
||||
|
||||
db.add(draft)
|
||||
@@ -508,6 +511,8 @@ async def get_drafts(
|
||||
"userPrompt": draft.user_prompt,
|
||||
"status": draft.status.value if draft.status else "pending",
|
||||
"isRead": draft.is_read,
|
||||
"publishedToCenter": draft.published_to_center,
|
||||
"draftType": draft.draft_type or "rewrite",
|
||||
"createdAt": draft.created_at.strftime("%Y-%m-%d %H:%M") if draft.created_at else "",
|
||||
"completedAt": draft.completed_at.strftime("%Y-%m-%d %H:%M") if draft.completed_at else None
|
||||
})
|
||||
@@ -549,6 +554,48 @@ async def check_new_drafts(
|
||||
}
|
||||
|
||||
|
||||
@router.get("/published")
|
||||
async def get_published_drafts(
|
||||
userId: int,
|
||||
draftType: Optional[str] = None,
|
||||
db: AsyncSession = Depends(get_db)
|
||||
):
|
||||
"""获取已发布到创作中心的草稿列表"""
|
||||
query = select(StoryDraft, Story.title.label('story_title')).join(
|
||||
Story, StoryDraft.story_id == Story.id
|
||||
).where(
|
||||
StoryDraft.user_id == userId,
|
||||
StoryDraft.published_to_center == True,
|
||||
StoryDraft.status == DraftStatus.completed
|
||||
)
|
||||
|
||||
# 按类型筛选
|
||||
if draftType:
|
||||
query = query.where(StoryDraft.draft_type == draftType)
|
||||
|
||||
query = query.order_by(StoryDraft.created_at.desc())
|
||||
|
||||
result = await db.execute(query)
|
||||
rows = result.all()
|
||||
|
||||
drafts = []
|
||||
for draft, story_title in rows:
|
||||
drafts.append({
|
||||
"id": draft.id,
|
||||
"storyId": draft.story_id,
|
||||
"storyTitle": story_title or "未知故事",
|
||||
"title": draft.title or "",
|
||||
"userPrompt": draft.user_prompt,
|
||||
"draftType": draft.draft_type or "rewrite",
|
||||
"createdAt": draft.created_at.strftime("%Y-%m-%d %H:%M") if draft.created_at else ""
|
||||
})
|
||||
|
||||
return {
|
||||
"code": 0,
|
||||
"data": drafts
|
||||
}
|
||||
|
||||
|
||||
@router.get("/{draft_id}")
|
||||
async def get_draft_detail(
|
||||
draft_id: int,
|
||||
@@ -652,3 +699,51 @@ async def mark_all_drafts_read(
|
||||
await db.commit()
|
||||
|
||||
return {"code": 0, "message": "已全部标记为已读"}
|
||||
|
||||
|
||||
@router.put("/{draft_id}/publish")
|
||||
async def publish_draft_to_center(
|
||||
draft_id: int,
|
||||
userId: int,
|
||||
db: AsyncSession = Depends(get_db)
|
||||
):
|
||||
"""发布草稿到创作中心"""
|
||||
# 验证草稿存在且属于该用户
|
||||
result = await db.execute(
|
||||
select(StoryDraft).where(
|
||||
StoryDraft.id == draft_id,
|
||||
StoryDraft.user_id == userId,
|
||||
StoryDraft.status == DraftStatus.completed
|
||||
)
|
||||
)
|
||||
draft = result.scalar_one_or_none()
|
||||
|
||||
if not draft:
|
||||
raise HTTPException(status_code=404, detail="草稿不存在或未完成")
|
||||
|
||||
# 更新发布状态
|
||||
draft.published_to_center = True
|
||||
await db.commit()
|
||||
|
||||
return {"code": 0, "message": "已发布到创作中心"}
|
||||
|
||||
|
||||
@router.put("/{draft_id}/unpublish")
|
||||
async def unpublish_draft_from_center(
|
||||
draft_id: int,
|
||||
userId: int,
|
||||
db: AsyncSession = Depends(get_db)
|
||||
):
|
||||
"""从创作中心取消发布"""
|
||||
await db.execute(
|
||||
update(StoryDraft)
|
||||
.where(
|
||||
StoryDraft.id == draft_id,
|
||||
StoryDraft.user_id == userId
|
||||
)
|
||||
.values(published_to_center=False)
|
||||
)
|
||||
await db.commit()
|
||||
|
||||
return {"code": 0, "message": "已从创作中心移除"}
|
||||
|
||||
|
||||
@@ -146,6 +146,8 @@ CREATE TABLE IF NOT EXISTS `story_drafts` (
|
||||
`status` ENUM('pending', 'processing', 'completed', 'failed') DEFAULT 'pending' COMMENT '状态',
|
||||
`error_message` VARCHAR(500) DEFAULT '' COMMENT '失败原因',
|
||||
`is_read` TINYINT(1) DEFAULT 0 COMMENT '用户是否已查看',
|
||||
`published_to_center` TINYINT(1) DEFAULT 0 COMMENT '是否发布到创作中心',
|
||||
`draft_type` VARCHAR(20) DEFAULT 'rewrite' COMMENT '草稿类型: rewrite/continue/create',
|
||||
`created_at` TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
|
||||
`completed_at` TIMESTAMP NULL DEFAULT NULL COMMENT '完成时间',
|
||||
PRIMARY KEY (`id`),
|
||||
|
||||
Reference in New Issue
Block a user