commit 351d4ce0c9dc9924c9ab09b205c9c356dd8d05f0 Author: Christopher Meinhold Date: Fri Jan 24 21:08:32 2025 +0100 Erster Push diff --git a/GetData.php b/GetData.php new file mode 100644 index 0000000..18daf84 --- /dev/null +++ b/GetData.php @@ -0,0 +1,63 @@ + "Unbefugter Zugriff - Falscher API-Schlüssel")); + exit; +} + +// Verbindung zur MySQL-Datenbank herstellen +$conn = new mysqli(DB_SERVER, DB_USERNAME, DB_PASSWORD, DB_NAME); + +// Überprüfen, ob die Verbindung erfolgreich war +if ($conn->connect_error) { + die("Verbindung fehlgeschlagen: " . $conn->connect_error); +} + +// Bestimmen des Endpunkts basierend auf der Anfrage +$endpoint = isset($_GET['endpoint']) ? $_GET['endpoint'] : ''; + +// Die Antwort wird standardmäßig als leeres Array gesetzt +$response = array(); + +switch ($endpoint) { + case 'GetFahrzeuge': + $sql = "SELECT id, typ, funkrufname, kennzeichen, bild FROM fahrzeuge"; + break; + + default: + $response = array("message" => "Ungültiger Endpunkt. Verfügbare Endpunkte: GetFahrzeuge."); + echo json_encode($response); + $conn->close(); + exit; +} + +// SQL-Abfrage ausführen und prüfen, ob es Ergebnisse gibt +$result = $conn->query($sql); + +if ($result->num_rows > 0) { + // Ein leeres Array zum Speichern der Ergebnisse + $dataList = array(); + + // Durch alle Datensätze iterieren und in ein Array einfügen + while($row = $result->fetch_assoc()) { + // Je nach Endpunkt den jeweiligen Namen einfügen + $dataList[] = $row; + } + + // Rückgabe der Ergebnisse als JSON + echo json_encode($dataList); +} else { + echo json_encode(array("message" => "Keine Daten gefunden")); +} + +// Verbindung schließen +$conn->close(); +?> diff --git a/Neu/.htaccess b/Neu/.htaccess new file mode 100644 index 0000000..c7282a5 --- /dev/null +++ b/Neu/.htaccess @@ -0,0 +1,7 @@ +RewriteEngine On +RewriteBase / + +# Weiterleitung aller Anfragen an api.php +RewriteCond %{REQUEST_FILENAME} !-f +RewriteCond %{REQUEST_FILENAME} !-d +RewriteRule ^(.*)$ api.php [QSA,L] \ No newline at end of file diff --git a/Neu/api.php b/Neu/api.php new file mode 100644 index 0000000..27321c7 --- /dev/null +++ b/Neu/api.php @@ -0,0 +1,78 @@ + "Ungültiger oder fehlender API-Key"]); + exit; + } +} + +// Prüfe den API-Key +validate_api_key(); + +// Holen des Endpunkts und der Methode +$request_method = $_SERVER['REQUEST_METHOD']; +$uri = explode('/', trim(parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH), '/')); +$endpoint = $uri[0] ?? null; +$id = $uri[1] ?? null; + +switch ($request_method) { + case 'GET': + if ($endpoint === 'recipes') { + if ($id) { + echo get_recipe_by_id($id); + } else { + echo get_all_recipes(); + } + } elseif ($endpoint === 'recipes' && isset($id) && is_numeric($id) && $uri[2] === 'comments') { + echo get_comments_by_recipe($id); + } else { + echo json_encode(["error" => "Ungültiger Endpunkt"]); + } + break; + + case 'POST': + if ($endpoint === 'recipes') { + echo create_recipe(); + } elseif ($endpoint === 'recipes' && isset($id) && is_numeric($id) && $uri[2] === 'comments') { + echo create_comment($id); + } else { + echo json_encode(["error" => "Ungültiger Endpunkt"]); + } + break; + + case 'PUT': + if ($endpoint === 'recipes' && isset($id) && is_numeric($id)) { + echo update_recipe($id); + } else { + echo json_encode(["error" => "Ungültiger Endpunkt"]); + } + break; + + case 'DELETE': + if ($endpoint === 'recipes' && isset($id) && is_numeric($id)) { + echo delete_recipe($id); + } elseif ($endpoint === 'comments' && isset($id) && is_numeric($id)) { + echo delete_comment($id); + } else { + echo json_encode(["error" => "Ungültiger Endpunkt"]); + } + break; + + default: + echo json_encode(["error" => "Ungültige Anforderung"]); + break; +} +?> diff --git a/Neu/assets/Chicken_Wings.webp b/Neu/assets/Chicken_Wings.webp new file mode 100644 index 0000000..e5fa15e Binary files /dev/null and b/Neu/assets/Chicken_Wings.webp differ diff --git a/Neu/assets/gemuesechip.webp b/Neu/assets/gemuesechip.webp new file mode 100644 index 0000000..de008b0 Binary files /dev/null and b/Neu/assets/gemuesechip.webp differ diff --git a/Neu/assets/pommes-frites-aus-der-heissluftfritteuse.webp b/Neu/assets/pommes-frites-aus-der-heissluftfritteuse.webp new file mode 100644 index 0000000..ef748c8 Binary files /dev/null and b/Neu/assets/pommes-frites-aus-der-heissluftfritteuse.webp differ diff --git a/Neu/assets/spaghetti-carbonara.webp b/Neu/assets/spaghetti-carbonara.webp new file mode 100644 index 0000000..b7c5afc Binary files /dev/null and b/Neu/assets/spaghetti-carbonara.webp differ diff --git a/Neu/modules/Comments.php b/Neu/modules/Comments.php new file mode 100644 index 0000000..9720938 --- /dev/null +++ b/Neu/modules/Comments.php @@ -0,0 +1,29 @@ +prepare($sql); + $stmt->bind_param("i", $recipe_id); + $stmt->execute(); + + $result = $stmt->get_result(); + $comments = []; + while ($row = $result->fetch_assoc()) { + $comments[] = $row; + } + + $stmt->close(); + $conn->close(); + return json_encode($comments); +} + +function create_comment($recipe_id) { + // Gleiche Logik wie im vorherigen Beispiel +} + +function delete_comment($id) { + // Gleiche Logik wie im vorherigen Beispiel +} +?> diff --git a/Neu/modules/Ingredients.php b/Neu/modules/Ingredients.php new file mode 100644 index 0000000..9b1b95f --- /dev/null +++ b/Neu/modules/Ingredients.php @@ -0,0 +1,21 @@ +prepare($sql); + $stmt->bind_param("i", $recipe_id); + $stmt->execute(); + + $result = $stmt->get_result(); + $ingredients = []; + while ($row = $result->fetch_assoc()) { + $ingredients[] = $row; + } + + $stmt->close(); + $conn->close(); + return json_encode($ingredients); +} +?> diff --git a/Neu/modules/MasterData.php b/Neu/modules/MasterData.php new file mode 100644 index 0000000..eeaece1 --- /dev/null +++ b/Neu/modules/MasterData.php @@ -0,0 +1,17 @@ +query($sql); + + $data = []; + while ($row = $result->fetch_assoc()) { + $data[] = $row; + } + + $conn->close(); + return json_encode($data); +} +?> diff --git a/Neu/modules/Recipes.php b/Neu/modules/Recipes.php new file mode 100644 index 0000000..dfd97cd --- /dev/null +++ b/Neu/modules/Recipes.php @@ -0,0 +1,103 @@ +connect_error) { + die(json_encode(["error" => "Datenbankverbindung fehlgeschlagen: " . $conn->connect_error])); + } + return $conn; +} + +function get_all_recipes() { + $conn = db_connect(); + $sql = "SELECT Id, Title, Description, ImagePath FROM Recipes"; + $result = $conn->query($sql); + + $recipes = []; + while ($row = $result->fetch_assoc()) { + $recipes[] = [ + 'Id' => (int)$row['Id'], + 'Title' => $row['Title'], + 'Description' => $row['Description'], + 'ImagePath' => $row['ImagePath'] + ]; + } + + $conn->close(); + return json_encode($recipes); +} + +function get_recipe_by_id($recipeId) { + $conn = db_connect(); + $recipeId = (int)$recipeId; + + // Lade die Basisinformationen des Rezepts + $sql = "SELECT * FROM `Recipes` WHERE `Id` = $recipeId"; + $result = $conn->query($sql); + $recipe = $result->fetch_assoc(); + + if (!$recipe) { + $conn->close(); + return json_encode(['error' => 'Rezept nicht gefunden']); + } + + $recipeDetails = [ + 'Id' => (int)$recipe['Id'], + 'Title' => $recipe['Title'], + 'Description' => $recipe['Description'], + 'ImagePath' => $recipe['ImagePath'], + 'Rating' => (float)$recipe['Rating'], + 'UserId' => (int)$recipe['UserId'], + 'Servings' => (int)$recipe['Servings'], + 'CreatedAt' => $recipe['CreatedAt'] + ]; + + // Lade die Anweisungen + $instructionsSql = "SELECT StepNumber, Instruction FROM RecipeInstructions WHERE RecipeId = $recipeId ORDER BY StepNumber"; + $instructionsResult = $conn->query($instructionsSql); + $instructions = []; + while ($instructionRow = $instructionsResult->fetch_assoc()) { + $instructions[] = [ + 'StepNumber' => (int)$instructionRow['StepNumber'], + 'InstructionText' => $instructionRow['Instruction'] + ]; + } + $recipeDetails['Instructions'] = $instructions; + + // Lade die Zutaten + $ingredientsSql = " + SELECT i.Quantity, md.Name AS Unit, i.Name + FROM Ingredients i + LEFT JOIN MasterData md ON i.UnitId = md.Id + WHERE i.RecipeId = $recipeId + "; + $ingredientsResult = $conn->query($ingredientsSql); + $ingredients = []; + while ($ingredientRow = $ingredientsResult->fetch_assoc()) { + $ingredients[] = [ + 'Quantity' => (float)$ingredientRow['Quantity'], + 'Unit' => $ingredientRow['Unit'], // Verknüpfte Einheit + 'Name' => $ingredientRow['Name'] + ]; + } + $recipeDetails['Ingredients'] = $ingredients; + + + $conn->close(); + return json_encode($recipeDetails); +} + + +function create_recipe() { + // Gleiche Logik wie im vorherigen Beispiel +} + +function update_recipe($id) { + // Gleiche Logik wie im vorherigen Beispiel +} + +function delete_recipe($id) { + // Gleiche Logik wie im vorherigen Beispiel +} +?> diff --git a/Neu/modules/Users.php b/Neu/modules/Users.php new file mode 100644 index 0000000..93b6b80 --- /dev/null +++ b/Neu/modules/Users.php @@ -0,0 +1,24 @@ +prepare($sql); + $stmt->bind_param("i", $id); + $stmt->execute(); + + $result = $stmt->get_result(); + $user = $result->fetch_assoc(); + + $stmt->close(); + $conn->close(); + + if (!$user) { + http_response_code(404); + return json_encode(["error" => "Benutzer nicht gefunden"]); + } + + return json_encode($user); +} +?> diff --git a/Neu/router.php b/Neu/router.php new file mode 100644 index 0000000..e7df853 --- /dev/null +++ b/Neu/router.php @@ -0,0 +1,9 @@ +