Add PUT endpoint and in-place title editing for stories

This commit is contained in:
Riccardo Giorato
2025-12-27 20:10:26 +01:00
parent 708205f3f0
commit 849aec93dd
3 changed files with 152 additions and 4 deletions
+56 -1
View File
@@ -1,6 +1,6 @@
import { type NextRequest, NextResponse } from "next/server";
import { auth } from "@clerk/nextjs/server";
import { getStoryWithPagesBySlug } from "@/lib/db-actions";
import { getStoryWithPagesBySlug, updateStory } from "@/lib/db-actions";
import { db } from "@/lib/db";
import { stories } from "@/lib/schema";
import { eq } from "drizzle-orm";
@@ -67,3 +67,58 @@ export async function GET(
);
}
}
export async function PUT(
request: NextRequest,
{ params }: { params: Promise<{ storySlug: string }> }
) {
try {
const { userId } = await auth();
if (!userId) {
return NextResponse.json(
{ error: "Authentication required" },
{ status: 401 }
);
}
const { storySlug: slug } = await params;
if (!slug) {
return NextResponse.json(
{ error: "Story slug is required" },
{ status: 400 }
);
}
const result = await getStoryWithPagesBySlug(slug);
if (!result) {
return NextResponse.json({ error: "Story not found" }, { status: 404 });
}
// Check if the story belongs to the authenticated user
if (result.story.userId !== userId) {
return NextResponse.json({ error: "Unauthorized" }, { status: 403 });
}
const { title } = await request.json();
if (!title || typeof title !== "string" || title.trim().length === 0) {
return NextResponse.json(
{ error: "Title is required and must be a non-empty string" },
{ status: 400 }
);
}
await updateStory(result.story.id, { title: title.trim() });
return NextResponse.json({ success: true });
} catch (error) {
console.error("Error updating story:", error);
return NextResponse.json(
{ error: "Failed to update story" },
{ status: 500 }
);
}
}