api/
ved/
admin/
modules/
gyar/
admin/
cron/
api/ved/bookingData.php1 lines
<?php
# bookingData.php
# API endpoint
# Fil för att visa eller ändra restauranginställningar,
# d.v.s vanliga öppettider, antal platser, bokningsintervall och bokningslängd.
require_once "ved/modules/utility.php";
# Hämtar bokningsinställningarna.
$functionGET = function () {
require_once "ved/modules/vedDatabase.php";
$settings = $db->query("SELECT seatCount, bookingInterval, bookingLength FROM restaurantSettings");
$openingHours = $db->queryAll("SELECT w.weekdayId weekday, DATE_FORMAT(w.openingTime, '%H:%i') openingTime, DATE_FORMAT(w.closingTime, '%H:%i') closingTime FROM weekdayOpenHours w");
$data = $settings;
$data["openingHours"] = $openingHours;
return $data;
};
# Ändrar bokningsinställningarna.
$functionPUT = function () {
# Parametrar kontrolleras och ser till att de här positiva heltal.
if (!is_int($_POST["seatCount"]) || $_POST["seatCount"] < 0)
throw new APIParameterError(400, "seatCount", "Parameter must be a positive integer.");
if (!is_int($_POST["bookingLength"]) || $_POST["bookingLength"] < 0)
throw new APIParameterError(400, "bookingLength", "Parameter must be a positive integer.");
if (!is_int($_POST["bookingInterval"]) || $_POST["bookingInterval"] < 0)
throw new APIParameterError(400, "bookingInterval", "Parameter must be a positive integer.");
# Ser till att bokningsintervallet passar in i bokningslängden.
if ($_POST["bookingLength"] % $_POST["bookingInterval"] != 0)
throw new APIParameterError(400, ["bookingLength", "bookingInterval"], "Booking interval does not fit into booking length.");
# Kontrollerar parametern för de nya öppettiderna.
if (gettype($_POST["openingHours"]) != "array")
throw new APIParameterError(400, "openingHours");
foreach ($_POST["openingHours"] as $id => $openingHours) {
# Index måste vara mellan 0 och 6 (0=måndag, 6=söndag).
if (!is_int($id) || $id < 0 || $id > 6)
throw new APIParameterError(400, "openingHours", "Invalid array key {$id}.");
# Om öppet- eller stängtiderna är null kommer det vara stängt den dagen.
if (!isset($openingHours["openingTime"]) || !isset($openingHours["closingTime"]))
continue;
$opening = time_to_min($openingHours["openingTime"]); # Öppettid i minuter efter 00:00
$closing = time_to_min($openingHours["closingTime"]); # Stängtid i minuter efter 00:00
$dayLength = $closing - $opening;
if ($dayLength <= 0)
throw new APIParameterError(400, "openingHours", "Invalid times for weekday {$id}.");
if ($dayLength % $_POST["bookingInterval"] != 0)
throw new APIParameterError(400, "openingHours", "Booking interval does not fit into weekday {$id}.");
}
# Ändrar tiderna. Använder transaction eftersom flera förändringar görs.
require "ved/modules/vedDatabase.php";
$db->pdo->beginTransaction();
try {
$db->query("UPDATE restaurantSettings SET seatCount=:seatCount,bookingInterval=:bookingInterval,bookingLength=:bookingLength", array(
"seatCount" => $_POST["seatCount"],
"bookingInterval" => $_POST["bookingInterval"],
"bookingLength" => $_POST["bookingLength"]
));
foreach ($_POST["openingHours"] as $id => $openingHours) {
$db->query("UPDATE weekdayOpenHours SET openingTime=:openingTime,closingTime=:closingTime WHERE weekdayId=:id", array(
"openingTime" => $openingHours["openingTime"],
"closingTime" => $openingHours["closingTime"],
"id" => $id
));
}
} catch (Exception $ex) {
$db->pdo->rollBack();
throw $ex;
}
$db->pdo->commit();
$data = array("message" => "Booking data was updated.");
return $data;
};
$api = new APIEndpoint();
$auth = require_once "ved/admin/authorization.php";
$httpGET = new APIMethod("GET", $functionGET);
$api->addMethod($httpGET);
$httpPUT = new APIMethod("PUT", $functionPUT);
$httpPUT->setAuthorization($auth);
$httpPUT->setRequiredBodyParameters(array("seatCount", "bookingInterval", "bookingLength", "openingHours"));
$api->addMethod($httpPUT);
return $api;
?>