64 lines
1.7 KiB
PHP
64 lines
1.7 KiB
PHP
|
<?php
|
||
|
require_once '../../includes/config.php';
|
||
|
require_once '../../includes/auth.php';
|
||
|
require_once '../../includes/stories.php';
|
||
|
|
||
|
// Vérification de l'authentification
|
||
|
if (!Auth::check()) {
|
||
|
http_response_code(401);
|
||
|
echo json_encode(['success' => false, 'error' => 'Non autorisé']);
|
||
|
exit;
|
||
|
}
|
||
|
|
||
|
// Vérification de la méthode HTTP
|
||
|
if ($_SERVER['REQUEST_METHOD'] !== 'POST') {
|
||
|
http_response_code(405);
|
||
|
echo json_encode(['success' => false, 'error' => 'Méthode non autorisée']);
|
||
|
exit;
|
||
|
}
|
||
|
|
||
|
// Récupération et validation des données
|
||
|
$input = json_decode(file_get_contents('php://input'), true);
|
||
|
$storyId = $input['id'] ?? null;
|
||
|
|
||
|
if (!$storyId) {
|
||
|
http_response_code(400);
|
||
|
echo json_encode(['success' => false, 'error' => 'ID du roman manquant']);
|
||
|
exit;
|
||
|
}
|
||
|
|
||
|
try {
|
||
|
// Récupération du roman
|
||
|
$story = Stories::get($storyId);
|
||
|
if (!$story) {
|
||
|
throw new Exception('Roman non trouvé');
|
||
|
}
|
||
|
|
||
|
// Suppression de l'image de couverture si elle existe
|
||
|
if (!empty($story['cover'])) {
|
||
|
$coverPath = __DIR__ . '/../../' . $story['cover'];
|
||
|
if (file_exists($coverPath)) {
|
||
|
unlink($coverPath);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
// Suppression du fichier JSON du roman
|
||
|
$storyFile = __DIR__ . '/../../stories/' . $storyId . '.json';
|
||
|
if (file_exists($storyFile)) {
|
||
|
unlink($storyFile);
|
||
|
|
||
|
// Réponse de succès
|
||
|
echo json_encode([
|
||
|
'success' => true,
|
||
|
'message' => 'Roman supprimé avec succès'
|
||
|
]);
|
||
|
} else {
|
||
|
throw new Exception('Fichier du roman introuvable');
|
||
|
}
|
||
|
} catch (Exception $e) {
|
||
|
http_response_code(500);
|
||
|
echo json_encode([
|
||
|
'success' => false,
|
||
|
'error' => 'Erreur lors de la suppression : ' . $e->getMessage()
|
||
|
]);
|
||
|
}
|