ApiDrkDdl/Neu/modules/Recipes.php
2025-01-24 21:08:32 +01:00

104 lines
2.9 KiB
PHP

<?php
require_once __DIR__ . '/../config.php';
function db_connect() {
$conn = new mysqli(DB_HOST, DB_USER, DB_PASS, DB_NAME);
if ($conn->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
}
?>