feat: 盲盒想法支持编辑和删除,展示用户已投入的想法列表
This commit is contained in:
@@ -55,10 +55,15 @@ export async function GET(req: NextRequest) {
|
||||
}
|
||||
|
||||
try {
|
||||
const [poolCount, drawn] = await Promise.all([
|
||||
const [poolCount, myIdeas, drawn] = await Promise.all([
|
||||
prisma.blindBoxIdea.count({
|
||||
where: { roomId, status: "in_pool" },
|
||||
}),
|
||||
prisma.blindBoxIdea.findMany({
|
||||
where: { roomId, userId, status: "in_pool" },
|
||||
orderBy: { createdAt: "desc" },
|
||||
select: { id: true, content: true, createdAt: true },
|
||||
}),
|
||||
prisma.blindBoxIdea.findMany({
|
||||
where: { roomId, status: "drawn" },
|
||||
orderBy: { createdAt: "desc" },
|
||||
@@ -69,8 +74,57 @@ export async function GET(req: NextRequest) {
|
||||
}),
|
||||
]);
|
||||
|
||||
return NextResponse.json({ poolCount, drawn });
|
||||
return NextResponse.json({ poolCount, myIdeas, drawn });
|
||||
} catch {
|
||||
return errorResponse("查询失败", 500);
|
||||
}
|
||||
}
|
||||
|
||||
export async function PUT(req: NextRequest) {
|
||||
try {
|
||||
const { ideaId, userId, content } = await req.json();
|
||||
|
||||
if (!userId) return errorResponse("请先登录", 401);
|
||||
if (!ideaId) return errorResponse("缺少 ideaId", 400);
|
||||
if (!content || typeof content !== "string" || content.trim().length === 0) {
|
||||
return errorResponse("内容不能为空", 400);
|
||||
}
|
||||
if (content.trim().length > 200) {
|
||||
return errorResponse("内容不能超过 200 字", 400);
|
||||
}
|
||||
|
||||
const idea = await prisma.blindBoxIdea.findUnique({ where: { id: ideaId } });
|
||||
if (!idea) return errorResponse("想法不存在", 404);
|
||||
if (idea.userId !== userId) return errorResponse("只能编辑自己的想法", 403);
|
||||
if (idea.status !== "in_pool") return errorResponse("已抽中的想法不能编辑", 400);
|
||||
|
||||
const updated = await prisma.blindBoxIdea.update({
|
||||
where: { id: ideaId },
|
||||
data: { content: content.trim() },
|
||||
});
|
||||
|
||||
return NextResponse.json({ id: updated.id, content: updated.content });
|
||||
} catch {
|
||||
return errorResponse("编辑失败", 500);
|
||||
}
|
||||
}
|
||||
|
||||
export async function DELETE(req: NextRequest) {
|
||||
try {
|
||||
const { ideaId, userId } = await req.json();
|
||||
|
||||
if (!userId) return errorResponse("请先登录", 401);
|
||||
if (!ideaId) return errorResponse("缺少 ideaId", 400);
|
||||
|
||||
const idea = await prisma.blindBoxIdea.findUnique({ where: { id: ideaId } });
|
||||
if (!idea) return errorResponse("想法不存在", 404);
|
||||
if (idea.userId !== userId) return errorResponse("只能删除自己的想法", 403);
|
||||
if (idea.status !== "in_pool") return errorResponse("已抽中的想法不能删除", 400);
|
||||
|
||||
await prisma.blindBoxIdea.delete({ where: { id: ideaId } });
|
||||
|
||||
return NextResponse.json({ deleted: true });
|
||||
} catch {
|
||||
return errorResponse("删除失败", 500);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user