gestion des chapitres modification et réoganisation
This commit is contained in:
49
src/app/api/projects/[id]/chapters/reorder/route.ts
Normal file
49
src/app/api/projects/[id]/chapters/reorder/route.ts
Normal file
@@ -0,0 +1,49 @@
|
||||
export const dynamic = 'force-dynamic';
|
||||
|
||||
import { NextRequest, NextResponse } from 'next/server';
|
||||
import { auth } from '@/lib/auth';
|
||||
import { prisma } from '@/lib/prisma';
|
||||
|
||||
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: projectId } = await params;
|
||||
const body = await request.json();
|
||||
const { chapters } = body as { chapters: { id: string, orderIndex: number }[] };
|
||||
|
||||
if (!Array.isArray(chapters)) {
|
||||
return NextResponse.json({ error: 'Format invalide' }, { status: 400 });
|
||||
}
|
||||
|
||||
// Verify ownership
|
||||
const existing = await prisma.project.findFirst({
|
||||
where: { id: projectId, userId: session.user.id },
|
||||
});
|
||||
|
||||
if (!existing) {
|
||||
return NextResponse.json({ error: 'Projet non trouvé' }, { status: 404 });
|
||||
}
|
||||
|
||||
try {
|
||||
// Bulk update chapters' orderIndex within a transaction
|
||||
await prisma.$transaction(
|
||||
chapters.map((chapter) =>
|
||||
prisma.chapter.update({
|
||||
where: { id: chapter.id, projectId },
|
||||
data: { orderIndex: chapter.orderIndex },
|
||||
})
|
||||
)
|
||||
);
|
||||
|
||||
return NextResponse.json({ success: true });
|
||||
} catch (error) {
|
||||
console.error('Erreur lors de la réorganisation des chapitres:', error);
|
||||
return NextResponse.json({ error: 'Erreur interne' }, { status: 500 });
|
||||
}
|
||||
}
|
||||
@@ -33,7 +33,8 @@ export default function ProjectLayout({ children }: { children: React.ReactNode
|
||||
updateProject, updateChapter, addChapter,
|
||||
createEntity, updateEntity, deleteEntity,
|
||||
createIdea, updateIdea, deleteIdea,
|
||||
deleteProject
|
||||
deleteProject,
|
||||
reorderChapters
|
||||
} = useProjects(user);
|
||||
const { chatHistory, isGenerating, sendMessage } = useChat();
|
||||
|
||||
@@ -79,6 +80,7 @@ export default function ProjectLayout({ children }: { children: React.ReactNode
|
||||
);
|
||||
}
|
||||
|
||||
// Handle early returns for non-existent projects
|
||||
if (!project) {
|
||||
return (
|
||||
<div className="h-screen w-full flex flex-col items-center justify-center bg-slate-900 text-white">
|
||||
@@ -129,6 +131,8 @@ export default function ProjectLayout({ children }: { children: React.ReactNode
|
||||
onViewModeChange={handleViewModeChange}
|
||||
onChapterSelect={(id) => { setCurrentChapterId(id); router.push(`/project/${projectId}`); }}
|
||||
onUpdateProject={(updates) => updateProject(projectId, updates)}
|
||||
onUpdateChapter={(chapterId, data) => updateChapter(projectId, chapterId, data)}
|
||||
onReorderChapters={(chapters) => reorderChapters(projectId, chapters)}
|
||||
onAddChapter={async () => {
|
||||
const id = await addChapter(projectId, {});
|
||||
if (id) {
|
||||
|
||||
Reference in New Issue
Block a user