66 lines
2.0 KiB
TypeScript
66 lines
2.0 KiB
TypeScript
export const dynamic = 'force-dynamic';
|
|
|
|
import { NextRequest, NextResponse } from 'next/server';
|
|
import { auth } from '@/lib/auth';
|
|
import getDB from '@/lib/prisma';
|
|
|
|
// PUT /api/chapters/[id] — Update a chapter
|
|
export async function PUT(
|
|
request: NextRequest,
|
|
{ params }: { params: Promise<{ id: string }> }
|
|
) {
|
|
const session = await auth();
|
|
if (!session?.user?.id) {
|
|
return NextResponse.json({ error: 'Non autorisé' }, { status: 401 });
|
|
}
|
|
|
|
const { id } = await params;
|
|
const body = await request.json();
|
|
|
|
// Verify ownership via project
|
|
const chapter = await getDB().chapter.findUnique({
|
|
where: { id },
|
|
include: { project: { select: { userId: true } } },
|
|
});
|
|
if (!chapter || chapter.project.userId !== session.user.id) {
|
|
return NextResponse.json({ error: 'Non trouvé' }, { status: 404 });
|
|
}
|
|
|
|
const updated = await getDB().chapter.update({
|
|
where: { id },
|
|
data: {
|
|
...(body.title !== undefined && { title: body.title }),
|
|
...(body.content !== undefined && { content: body.content }),
|
|
...(body.summary !== undefined && { summary: body.summary }),
|
|
...(body.orderIndex !== undefined && { orderIndex: body.orderIndex }),
|
|
},
|
|
});
|
|
|
|
return NextResponse.json(updated);
|
|
}
|
|
|
|
// DELETE /api/chapters/[id]
|
|
export async function DELETE(
|
|
_request: NextRequest,
|
|
{ params }: { params: Promise<{ id: string }> }
|
|
) {
|
|
const session = await auth();
|
|
if (!session?.user?.id) {
|
|
return NextResponse.json({ error: 'Non autorisé' }, { status: 401 });
|
|
}
|
|
|
|
const { id } = await params;
|
|
|
|
const chapter = await getDB().chapter.findUnique({
|
|
where: { id },
|
|
include: { project: { select: { userId: true } } },
|
|
});
|
|
if (!chapter || chapter.project.userId !== session.user.id) {
|
|
return NextResponse.json({ error: 'Non trouvé' }, { status: 404 });
|
|
}
|
|
|
|
await getDB().chapter.delete({ where: { id } });
|
|
|
|
return NextResponse.json({ success: true });
|
|
}
|