From 9c82aec3c5e6817392921d1d9cf4afd70d36eec1 Mon Sep 17 00:00:00 2001 From: Samuel Wolff Date: Mon, 15 Jan 2024 08:58:24 +0100 Subject: [PATCH] nextId --- Controller/BenutzerController.php | 10 +++ Model/BenutzerModel.php | 21 +++++ restAPI.php | 134 +++++++++++++++--------------- 3 files changed, 100 insertions(+), 65 deletions(-) diff --git a/Controller/BenutzerController.php b/Controller/BenutzerController.php index 1d6e02b..1393870 100644 --- a/Controller/BenutzerController.php +++ b/Controller/BenutzerController.php @@ -36,6 +36,16 @@ class BenutzerController return json_encode($data); } + + public function nextId() + { + + $result = $this->db->nextId(); + + return $result; + + } + } ?> \ No newline at end of file diff --git a/Model/BenutzerModel.php b/Model/BenutzerModel.php index 3431c4e..dfdaf3f 100644 --- a/Model/BenutzerModel.php +++ b/Model/BenutzerModel.php @@ -95,6 +95,27 @@ class BenutzerModel extends Database } + public function nextId() + { + + $pdo = $this->linkDB(); + + $sql = "SELECT `auto_increment` FROM INFORMATION_SCHEMA.TABLES WHERE table_name = 'Benutzerkonto'; "; + + try { + $stmt = $pdo->prepare($sql); + $stmt->execute(); + } catch (\PDOException $e) { + return false; + } + + $result = $stmt->fetchALL(\PDO::FETCH_ASSOC); + + return $result; + + } + + } ?> \ No newline at end of file diff --git a/restAPI.php b/restAPI.php index c0cb206..03f0e36 100644 --- a/restAPI.php +++ b/restAPI.php @@ -1,4 +1,4 @@ -$methodName($id); - - } else - //POST - if ($_SERVER['REQUEST_METHOD'] == "POST"){ - echo $controller->$methodName($data); - } else - //DELETE - if ($_SERVER['REQUEST_METHOD'] == "DELETE"){ - echo $controller->$methodName($id); - } else - //PUT - { - echo $controller->$methodName($id, $data); - } +}); + +$endpoint = explode('/', trim($_SERVER['PATH_INFO'], '/')); +$data = json_decode(file_get_contents('php://input'), true); + +$controllerName = $endpoint[0]; +$endpoint2 = isset($endpoint[1]) ? $endpoint[1] : false; +$id = false; +$alias = false; + +if ($endpoint2) { + if (preg_match('/^[0-9]+$/', $endpoint2)) { + $id = $endpoint2; } else { - //http_response_code(404); - new \ppb\Library\Msg(true, 'Page not found: '.$controllerClassName.'::'.$methodName); - + $alias = $endpoint2; } +} + +$controllerClassName = 'ppb\\Controller\\' . ucfirst($controllerName) . 'Controller'; + +if ($_SERVER['REQUEST_METHOD'] == "DELETE") { + $methodName = "delete" . ucfirst($controllerName); +} else if ($_SERVER['REQUEST_METHOD'] == "PUT") { + $methodName = "update" . ucfirst($controllerName); +} else if ($_SERVER['REQUEST_METHOD'] == "POST") { + $methodName = "write" . ucfirst($controllerName); +} else if ($_SERVER['REQUEST_METHOD'] == "GET") { + if ($alias) { + $methodName = $alias; + } else { + $methodName = "get" . ucfirst($controllerName); + } +} + +if (method_exists($controllerClassName, $methodName)) { + + header('Content-Type: application/json'); + + $controller = new $controllerClassName(); + //GET + if ($_SERVER['REQUEST_METHOD'] == "GET") { + + echo $controller->$methodName($id); + + } else + //POST + if ($_SERVER['REQUEST_METHOD'] == "POST") { + echo $controller->$methodName($data); + } else + //DELETE + if ($_SERVER['REQUEST_METHOD'] == "DELETE") { + echo $controller->$methodName($id); + } else + //PUT + { + echo $controller->$methodName($id, $data); + } +} else { + //http_response_code(404); + new \ppb\Library\Msg(true, 'Page not found: ' . $controllerClassName . '::' . $methodName); + +} ?> \ No newline at end of file