Erster Push
This commit is contained in:
commit
351d4ce0c9
63
GetData.php
Normal file
63
GetData.php
Normal file
@ -0,0 +1,63 @@
|
|||||||
|
<?php
|
||||||
|
// Die Konfigurationsdatei einbinden
|
||||||
|
require_once('config.php');
|
||||||
|
|
||||||
|
// API-Schlüssel aus dem Header abrufen
|
||||||
|
$apiKey = isset($_SERVER['HTTP_X_API_KEY']) ? $_SERVER['HTTP_X_API_KEY'] : '';
|
||||||
|
|
||||||
|
// Überprüfen, ob der API-Schlüssel korrekt ist
|
||||||
|
if ($apiKey !== API_KEY) {
|
||||||
|
// Wenn der API-Schlüssel falsch ist oder fehlt, eine Fehlermeldung senden
|
||||||
|
header("HTTP/1.1 403 Forbidden");
|
||||||
|
echo json_encode(array("message" => "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();
|
||||||
|
?>
|
||||||
7
Neu/.htaccess
Normal file
7
Neu/.htaccess
Normal file
@ -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]
|
||||||
78
Neu/api.php
Normal file
78
Neu/api.php
Normal file
@ -0,0 +1,78 @@
|
|||||||
|
<?php
|
||||||
|
header("Content-Type: application/json");
|
||||||
|
include('config.php');
|
||||||
|
include('modules/Recipes.php');
|
||||||
|
include('modules/Comments.php');
|
||||||
|
include('modules/Ingredients.php');
|
||||||
|
include('modules/Users.php');
|
||||||
|
include('modules/MasterData.php');
|
||||||
|
|
||||||
|
// Funktion zur API-Key-Validierung
|
||||||
|
function validate_api_key() {
|
||||||
|
$headers = getallheaders();
|
||||||
|
$api_key = $headers['X-API-KEY'] ?? null; // Prüfe den `X-API-KEY` Header
|
||||||
|
|
||||||
|
if ($api_key !== API_KEY) {
|
||||||
|
http_response_code(403); // Forbidden
|
||||||
|
echo json_encode(["error" => "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;
|
||||||
|
}
|
||||||
|
?>
|
||||||
BIN
Neu/assets/Chicken_Wings.webp
Normal file
BIN
Neu/assets/Chicken_Wings.webp
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 13 KiB |
BIN
Neu/assets/gemuesechip.webp
Normal file
BIN
Neu/assets/gemuesechip.webp
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 48 KiB |
BIN
Neu/assets/pommes-frites-aus-der-heissluftfritteuse.webp
Normal file
BIN
Neu/assets/pommes-frites-aus-der-heissluftfritteuse.webp
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 33 KiB |
BIN
Neu/assets/spaghetti-carbonara.webp
Normal file
BIN
Neu/assets/spaghetti-carbonara.webp
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 100 KiB |
29
Neu/modules/Comments.php
Normal file
29
Neu/modules/Comments.php
Normal file
@ -0,0 +1,29 @@
|
|||||||
|
<?php
|
||||||
|
require_once __DIR__ . '/../config.php';
|
||||||
|
|
||||||
|
function get_comments_by_recipe($recipe_id) {
|
||||||
|
$conn = db_connect();
|
||||||
|
$sql = "SELECT * FROM Comments WHERE RecipeId = ?";
|
||||||
|
$stmt = $conn->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
|
||||||
|
}
|
||||||
|
?>
|
||||||
21
Neu/modules/Ingredients.php
Normal file
21
Neu/modules/Ingredients.php
Normal file
@ -0,0 +1,21 @@
|
|||||||
|
<?php
|
||||||
|
require_once __DIR__ . '/../config.php';
|
||||||
|
|
||||||
|
function get_ingredients_by_recipe($recipe_id) {
|
||||||
|
$conn = db_connect();
|
||||||
|
$sql = "SELECT * FROM Ingredients WHERE RecipeId = ?";
|
||||||
|
$stmt = $conn->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);
|
||||||
|
}
|
||||||
|
?>
|
||||||
17
Neu/modules/MasterData.php
Normal file
17
Neu/modules/MasterData.php
Normal file
@ -0,0 +1,17 @@
|
|||||||
|
<?php
|
||||||
|
require_once __DIR__ . '/../config.php';
|
||||||
|
|
||||||
|
function get_master_data() {
|
||||||
|
$conn = db_connect();
|
||||||
|
$sql = "SELECT * FROM MasterData";
|
||||||
|
$result = $conn->query($sql);
|
||||||
|
|
||||||
|
$data = [];
|
||||||
|
while ($row = $result->fetch_assoc()) {
|
||||||
|
$data[] = $row;
|
||||||
|
}
|
||||||
|
|
||||||
|
$conn->close();
|
||||||
|
return json_encode($data);
|
||||||
|
}
|
||||||
|
?>
|
||||||
103
Neu/modules/Recipes.php
Normal file
103
Neu/modules/Recipes.php
Normal file
@ -0,0 +1,103 @@
|
|||||||
|
<?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
|
||||||
|
}
|
||||||
|
?>
|
||||||
24
Neu/modules/Users.php
Normal file
24
Neu/modules/Users.php
Normal file
@ -0,0 +1,24 @@
|
|||||||
|
<?php
|
||||||
|
require_once __DIR__ . '/../config.php';
|
||||||
|
|
||||||
|
function get_user_by_id($id) {
|
||||||
|
$conn = db_connect();
|
||||||
|
$sql = "SELECT * FROM Users WHERE Id = ?";
|
||||||
|
$stmt = $conn->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);
|
||||||
|
}
|
||||||
|
?>
|
||||||
9
Neu/router.php
Normal file
9
Neu/router.php
Normal file
@ -0,0 +1,9 @@
|
|||||||
|
<?php
|
||||||
|
// Prüfe, ob die Datei oder das Verzeichnis existiert
|
||||||
|
if (file_exists(__DIR__ . parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH))) {
|
||||||
|
return false; // Liefere die Datei direkt aus
|
||||||
|
}
|
||||||
|
|
||||||
|
// Alle anderen Anfragen an api.php weiterleiten
|
||||||
|
$_GET['endpoint'] = trim(parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH), '/');
|
||||||
|
require_once __DIR__ . '/api.php';
|
||||||
10
config.php
Normal file
10
config.php
Normal file
@ -0,0 +1,10 @@
|
|||||||
|
<?php
|
||||||
|
// Datenbank-Verbindungsinformationen (kann auch aus Umgebungsvariablen geladen werden)
|
||||||
|
define('DB_SERVER', '155.133.215.203'); // Oder IP-Adresse des MySQL-Servers
|
||||||
|
define('DB_USERNAME', 'drkddl'); // Dein MySQL-Benutzername
|
||||||
|
define('DB_PASSWORD', 'fI.Y-QfFoOtJNrRV'); // Dein MySQL-Passwort
|
||||||
|
define('DB_NAME', 'drkddl'); // Deine Datenbank
|
||||||
|
|
||||||
|
// Definiere hier deinen geheimen API-Schlüssel
|
||||||
|
define('API_KEY', '7caef100-7440-49eb-a195-60c5ee02dcad');
|
||||||
|
?>
|
||||||
Loading…
Reference in New Issue
Block a user