false, 'error' => 'Non autorisé'])); } $input = json_decode(file_get_contents('php://input'), true); $storyId = $input['storyId'] ?? null; $chapterId = $input['chapterId'] ?? null; $title = $input['title'] ?? ''; $content = $input['content'] ?? ''; $draft = $input['draft'] ?? false; try { // Ajout de logs pour déboguer error_log('Données reçues: ' . print_r($input, true)); $story = Stories::get($storyId); if (!$story) { throw new Exception('Roman non trouvé'); } if ($chapterId) { // Mise à jour d'un chapitre existant $chapterUpdated = false; foreach ($story['chapters'] as &$chapter) { if ($chapter['id'] === $chapterId) { $chapter['title'] = $title; $chapter['content'] = $content; $chapter['draft'] = $draft; $chapter['updated'] = date('Y-m-d'); $chapterUpdated = true; break; } } if (!$chapterUpdated) { throw new Exception('Chapitre non trouvé'); } } else { // Nouveau chapitre $story['chapters'][] = [ 'id' => uniqid(), 'title' => $title, 'content' => $content, 'draft' => $draft, 'created' => date('Y-m-d'), 'updated' => date('Y-m-d') ]; } if (!Stories::save($story)) { throw new Exception('Erreur lors de la sauvegarde du roman'); } echo json_encode(['success' => true]); } catch (Exception $e) { error_log('Erreur dans save-chapter.php: ' . $e->getMessage()); http_response_code(500); echo json_encode([ 'success' => false, 'error' => $e->getMessage() ]); }