|
| 1 | +import { NextRequest, NextResponse } from "next/server"; |
| 2 | +import { auth } from "@/lib/auth"; |
| 3 | +import { db } from "@/lib/db"; |
| 4 | +import { z } from "zod"; |
| 5 | + |
| 6 | +const addExampleSchema = z.object({ |
| 7 | + mediaUrl: z.string().url(), |
| 8 | + comment: z.string().max(500).optional(), |
| 9 | +}); |
| 10 | + |
| 11 | +export async function GET( |
| 12 | + req: NextRequest, |
| 13 | + { params }: { params: Promise<{ id: string }> } |
| 14 | +) { |
| 15 | + const { id: promptId } = await params; |
| 16 | + |
| 17 | + const prompt = await db.prompt.findUnique({ |
| 18 | + where: { id: promptId }, |
| 19 | + select: { id: true, type: true }, |
| 20 | + }); |
| 21 | + |
| 22 | + if (!prompt) { |
| 23 | + return NextResponse.json({ error: "Prompt not found" }, { status: 404 }); |
| 24 | + } |
| 25 | + |
| 26 | + // Only allow examples for IMAGE and VIDEO prompts |
| 27 | + if (prompt.type !== "IMAGE" && prompt.type !== "VIDEO") { |
| 28 | + return NextResponse.json({ error: "Examples not supported for this prompt type" }, { status: 400 }); |
| 29 | + } |
| 30 | + |
| 31 | + const examples = await db.userPromptExample.findMany({ |
| 32 | + where: { promptId }, |
| 33 | + orderBy: { createdAt: "desc" }, |
| 34 | + include: { |
| 35 | + user: { |
| 36 | + select: { |
| 37 | + id: true, |
| 38 | + username: true, |
| 39 | + name: true, |
| 40 | + avatar: true, |
| 41 | + }, |
| 42 | + }, |
| 43 | + }, |
| 44 | + }); |
| 45 | + |
| 46 | + return NextResponse.json({ examples }); |
| 47 | +} |
| 48 | + |
| 49 | +export async function POST( |
| 50 | + req: NextRequest, |
| 51 | + { params }: { params: Promise<{ id: string }> } |
| 52 | +) { |
| 53 | + const session = await auth(); |
| 54 | + |
| 55 | + if (!session?.user) { |
| 56 | + return NextResponse.json({ error: "Unauthorized" }, { status: 401 }); |
| 57 | + } |
| 58 | + |
| 59 | + const { id: promptId } = await params; |
| 60 | + |
| 61 | + try { |
| 62 | + const body = await req.json(); |
| 63 | + const { mediaUrl, comment } = addExampleSchema.parse(body); |
| 64 | + |
| 65 | + const prompt = await db.prompt.findUnique({ |
| 66 | + where: { id: promptId }, |
| 67 | + select: { id: true, type: true, isPrivate: true, authorId: true }, |
| 68 | + }); |
| 69 | + |
| 70 | + if (!prompt) { |
| 71 | + return NextResponse.json({ error: "Prompt not found" }, { status: 404 }); |
| 72 | + } |
| 73 | + |
| 74 | + // Only allow examples for IMAGE and VIDEO prompts |
| 75 | + if (prompt.type !== "IMAGE" && prompt.type !== "VIDEO") { |
| 76 | + return NextResponse.json({ error: "Examples not supported for this prompt type" }, { status: 400 }); |
| 77 | + } |
| 78 | + |
| 79 | + // Don't allow adding examples to private prompts (unless owner) |
| 80 | + if (prompt.isPrivate && prompt.authorId !== session.user.id) { |
| 81 | + return NextResponse.json({ error: "Cannot add example to private prompt" }, { status: 403 }); |
| 82 | + } |
| 83 | + |
| 84 | + const example = await db.userPromptExample.create({ |
| 85 | + data: { |
| 86 | + mediaUrl, |
| 87 | + comment: comment || null, |
| 88 | + promptId, |
| 89 | + userId: session.user.id, |
| 90 | + }, |
| 91 | + include: { |
| 92 | + user: { |
| 93 | + select: { |
| 94 | + id: true, |
| 95 | + username: true, |
| 96 | + name: true, |
| 97 | + avatar: true, |
| 98 | + }, |
| 99 | + }, |
| 100 | + }, |
| 101 | + }); |
| 102 | + |
| 103 | + return NextResponse.json({ example }); |
| 104 | + } catch (error) { |
| 105 | + if (error instanceof z.ZodError) { |
| 106 | + return NextResponse.json({ error: "Invalid input", details: error.issues }, { status: 400 }); |
| 107 | + } |
| 108 | + console.error("Failed to add example:", error); |
| 109 | + return NextResponse.json({ error: "Failed to add example" }, { status: 500 }); |
| 110 | + } |
| 111 | +} |
| 112 | + |
| 113 | +export async function DELETE( |
| 114 | + req: NextRequest, |
| 115 | + { params }: { params: Promise<{ id: string }> } |
| 116 | +) { |
| 117 | + const session = await auth(); |
| 118 | + |
| 119 | + if (!session?.user) { |
| 120 | + return NextResponse.json({ error: "Unauthorized" }, { status: 401 }); |
| 121 | + } |
| 122 | + |
| 123 | + const { id: promptId } = await params; |
| 124 | + |
| 125 | + try { |
| 126 | + const { searchParams } = new URL(req.url); |
| 127 | + const exampleId = searchParams.get("exampleId"); |
| 128 | + |
| 129 | + if (!exampleId) { |
| 130 | + return NextResponse.json({ error: "exampleId required" }, { status: 400 }); |
| 131 | + } |
| 132 | + |
| 133 | + const example = await db.userPromptExample.findUnique({ |
| 134 | + where: { id: exampleId }, |
| 135 | + select: { id: true, userId: true, promptId: true }, |
| 136 | + }); |
| 137 | + |
| 138 | + if (!example) { |
| 139 | + return NextResponse.json({ error: "Example not found" }, { status: 404 }); |
| 140 | + } |
| 141 | + |
| 142 | + if (example.promptId !== promptId) { |
| 143 | + return NextResponse.json({ error: "Example does not belong to this prompt" }, { status: 400 }); |
| 144 | + } |
| 145 | + |
| 146 | + // Only allow owner or admin to delete |
| 147 | + const isAdmin = session.user.role === "ADMIN"; |
| 148 | + if (example.userId !== session.user.id && !isAdmin) { |
| 149 | + return NextResponse.json({ error: "Unauthorized" }, { status: 403 }); |
| 150 | + } |
| 151 | + |
| 152 | + await db.userPromptExample.delete({ |
| 153 | + where: { id: exampleId }, |
| 154 | + }); |
| 155 | + |
| 156 | + return NextResponse.json({ deleted: true }); |
| 157 | + } catch (error) { |
| 158 | + console.error("Failed to delete example:", error); |
| 159 | + return NextResponse.json({ error: "Failed to delete example" }, { status: 500 }); |
| 160 | + } |
| 161 | +} |
0 commit comments