9d9dfb46c6
Cooking mode step-by-step view. OpenAPI 3.1 spec auto-generated from Zod schemas. Email lib (resend). Redis client. S3 storage helper. Photo upload endpoint. Landing page. Short recipe URL redirect /r/[id]. API docs page.
40 lines
1.6 KiB
TypeScript
40 lines
1.6 KiB
TypeScript
import { NextRequest, NextResponse } from "next/server";
|
|
import { z } from "zod";
|
|
import { db, comments, eq, and } from "@epicure/db";
|
|
import { requireSession } from "@/lib/api-auth";
|
|
|
|
type Params = { params: Promise<{ id: string }> };
|
|
|
|
export async function PUT(req: NextRequest, { params }: Params) {
|
|
const { session, response } = await requireSession();
|
|
if (response) return response;
|
|
const { id } = await params;
|
|
|
|
const comment = await db.query.comments.findFirst({ where: eq(comments.id, id) });
|
|
if (!comment || comment.userId !== session!.user.id) {
|
|
return NextResponse.json({ error: "Not found" }, { status: 404 });
|
|
}
|
|
|
|
const body = await req.json() as unknown;
|
|
const parsed = z.object({ content: z.string().min(1).max(5000) }).safeParse(body);
|
|
if (!parsed.success) return NextResponse.json({ error: "Validation error" }, { status: 400 });
|
|
|
|
await db.update(comments).set({ content: parsed.data.content, updatedAt: new Date() }).where(eq(comments.id, id));
|
|
return NextResponse.json({ updated: true });
|
|
}
|
|
|
|
export async function DELETE(_req: NextRequest, { params }: Params) {
|
|
const { session, response } = await requireSession();
|
|
if (response) return response;
|
|
const { id } = await params;
|
|
|
|
const comment = await db.query.comments.findFirst({ where: eq(comments.id, id) });
|
|
if (!comment) return NextResponse.json({ error: "Not found" }, { status: 404 });
|
|
if (comment.userId !== session!.user.id && session!.user.role === "user") {
|
|
return NextResponse.json({ error: "Forbidden" }, { status: 403 });
|
|
}
|
|
|
|
await db.delete(comments).where(eq(comments.id, id));
|
|
return new NextResponse(null, { status: 204 });
|
|
}
|