rms/backend/menu.php
2025-05-08 21:05:06 +06:00

119 lines
3.6 KiB
PHP

<?php
header("Content-Type: application/json");
header("Access-Control-Allow-Origin: *");
header("Access-Control-Allow-Methods: GET, POST, PUT, DELETE, OPTIONS");
header("Access-Control-Allow-Headers: Content-Type");
// Handle preflight OPTIONS request
if ($_SERVER['REQUEST_METHOD'] === 'OPTIONS') {
http_response_code(200);
exit();
}
require_once __DIR__ . '/config.php';
try {
$pdo = new PDO(
"mysql:host=" . DB_HOST . ";dbname=" . DB_NAME . ";charset=utf8mb4",
DB_USER,
DB_PASS
);
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch (PDOException $e) {
// Handle connection errors
http_response_code(500);
echo json_encode(['error' => 'Database connection failed: ' . $e->getMessage()]);
exit();
}
// Get the HTTP method and request URI
$method = $_SERVER['REQUEST_METHOD'];
$request = explode('/', trim($_SERVER['PATH_INFO'] ?? '', '/'));
$resource = array_shift($request);
$id = array_shift($request);
// Ensure the resource is 'menu'
if ($resource !== 'menu') {
http_response_code(404);
echo json_encode(['error' => 'Resource not found']);
exit();
}
// Read the input JSON
$input = json_decode(file_get_contents('php://input'), true);
// Define the SQL queries
switch ($method) {
case 'GET':
if ($id) {
// Retrieve a single menu item
$stmt = $pdo->prepare("SELECT * FROM menu_items WHERE id = ?");
$stmt->execute([$id]);
$item = $stmt->fetch(PDO::FETCH_ASSOC);
if ($item) {
echo json_encode($item);
} else {
http_response_code(404);
echo json_encode(['error' => 'Menu item not found']);
}
} else {
// Retrieve all menu items
$stmt = $pdo->query("SELECT * FROM menu_items");
$items = $stmt->fetchAll(PDO::FETCH_ASSOC);
echo json_encode($items);
}
break;
case 'POST':
// Create a new menu item
if (!isset($input['name'], $input['price'])) {
http_response_code(400);
echo json_encode(['error' => 'Name and price are required']);
exit();
}
$stmt = $pdo->prepare("INSERT INTO menu_items (name, description, price) VALUES (?, ?, ?)");
$stmt->execute([
$input['name'],
$input['description'] ?? null,
$input['price']
]);
$id = $pdo->lastInsertId();
http_response_code(201);
echo json_encode(['message' => 'Menu item created', 'id' => $id]);
break;
case 'PUT':
// Update an existing menu item
if (!$id) {
http_response_code(400);
echo json_encode(['error' => 'ID is required']);
exit();
}
$stmt = $pdo->prepare("UPDATE menu_items SET name = ?, description = ?, price = ? WHERE id = ?");
$stmt->execute([
$input['name'] ?? null,
$input['description'] ?? null,
$input['price'] ?? null,
$id
]);
echo json_encode(['message' => 'Menu item updated']);
break;
case 'DELETE':
// Delete a menu item
if (!$id) {
http_response_code(400);
echo json_encode(['error' => 'ID is required']);
exit();
}
$stmt = $pdo->prepare("DELETE FROM menu_items WHERE id = ?");
$stmt->execute([$id]);
echo json_encode(['message' => 'Menu item deleted']);
break;
default:
http_response_code(405);
echo json_encode(['error' => 'Method not allowed']);
break;
}