From 36767931946562a812c44ed3f1042b28125e244b Mon Sep 17 00:00:00 2001 From: morgane Date: Thu, 18 Jun 2026 12:53:49 +0000 Subject: [PATCH] Actualiser api/index.php --- api/index.php | 66 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 66 insertions(+) diff --git a/api/index.php b/api/index.php index 6a1a658..23bfb5b 100644 --- a/api/index.php +++ b/api/index.php @@ -12338,6 +12338,72 @@ function categoriesUpdate(PDO $db): void { echo json_encode(['success' => true]); } +function recipeLibraryList(PDO $db): void { + $rows = $db->query("SELECT id, title, recipe_json, is_favorite, created_at FROM recipe_library ORDER BY is_favorite DESC, created_at DESC")->fetchAll(); + $recipes = []; + foreach ($rows as $row) { + $recipes[] = [ + 'id' => (int)$row['id'], + 'title' => $row['title'], + 'recipe' => json_decode($row['recipe_json'], true), + 'is_favorite' => (bool)$row['is_favorite'], + 'created_at' => $row['created_at'], + ]; + } + echo json_encode(['success' => true, 'recipes' => $recipes]); +} + +function recipeLibrarySave(PDO $db): void { + $input = json_decode(file_get_contents('php://input'), true) ?? []; + $id = (int)($input['id'] ?? 0); + $recipe = $input['recipe'] ?? null; + $title = trim($recipe['title'] ?? ''); + + if (!$recipe || $title === '') { + echo json_encode(['success' => false, 'error' => 'title and recipe required']); + return; + } + + $json = json_encode($recipe, JSON_UNESCAPED_UNICODE); + + if ($id > 0) { + $stmt = $db->prepare("SELECT id FROM recipe_library WHERE id = ?"); + $stmt->execute([$id]); + if (!$stmt->fetch()) { + echo json_encode(['success' => false, 'error' => 'recipe not found']); + return; + } + $db->prepare("UPDATE recipe_library SET title = ?, recipe_json = ?, updated_at = CURRENT_TIMESTAMP WHERE id = ?") + ->execute([$title, $json, $id]); + echo json_encode(['success' => true, 'id' => $id]); + return; + } + + $db->prepare("INSERT INTO recipe_library (title, recipe_json) VALUES (?, ?)")->execute([$title, $json]); + echo json_encode(['success' => true, 'id' => (int)$db->lastInsertId()]); +} + +function recipeLibraryDelete(PDO $db): void { + $input = json_decode(file_get_contents('php://input'), true) ?? []; + $id = (int)($input['id'] ?? 0); + if ($id > 0) { + $db->prepare("DELETE FROM recipe_library WHERE id = ?")->execute([$id]); + } + echo json_encode(['success' => true]); +} + +function recipeLibraryToggleFavorite(PDO $db): void { + $input = json_decode(file_get_contents('php://input'), true) ?? []; + $id = (int)($input['id'] ?? 0); + if ($id <= 0) { + echo json_encode(['success' => false, 'error' => 'id required']); + return; + } + $db->prepare("UPDATE recipe_library SET is_favorite = 1 - is_favorite WHERE id = ?")->execute([$id]); + $fav = (int)$db->query("SELECT is_favorite FROM recipe_library WHERE id = {$id}")->fetchColumn(); + echo json_encode(['success' => true, 'is_favorite' => (bool)$fav]); +} + // ===== SHARED APP DATA FUNCTIONS ===== function appSettingsGet(PDO $db): void {