From 9c82aec3c5e6817392921d1d9cf4afd70d36eec1 Mon Sep 17 00:00:00 2001 From: Samuel Wolff Date: Mon, 15 Jan 2024 08:58:24 +0100 Subject: [PATCH 1/3] 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 From 2b14563643f4c77be8e5a1cf7259587bb91092c7 Mon Sep 17 00:00:00 2001 From: Samuel Wolff Date: Mon, 15 Jan 2024 09:39:19 +0100 Subject: [PATCH 2/3] nextId --- Controller/BenutzerController.php | 8 ++++++++ Model/BenutzerModel.php | 20 ++++++++++++++++++++ 2 files changed, 28 insertions(+) diff --git a/Controller/BenutzerController.php b/Controller/BenutzerController.php index 1d6e02b..61f64e1 100644 --- a/Controller/BenutzerController.php +++ b/Controller/BenutzerController.php @@ -36,6 +36,14 @@ class BenutzerController return json_encode($data); } + + public function nextId() + { + $result = $this->db->nextId(); + + return json_encode($result); + } + } ?> \ No newline at end of file diff --git a/Model/BenutzerModel.php b/Model/BenutzerModel.php index 3431c4e..3356ce7 100644 --- a/Model/BenutzerModel.php +++ b/Model/BenutzerModel.php @@ -94,6 +94,26 @@ class BenutzerModel extends Database return $result; } + 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; + + } + } From 1357e21155471fdcb92b474a4d91435d0f345798 Mon Sep 17 00:00:00 2001 From: Samuel Wolff Date: Wed, 17 Jan 2024 10:21:14 +0100 Subject: [PATCH 3/3] ??? --- Model/BenutzerModel.php | 23 ----------------------- 1 file changed, 23 deletions(-) diff --git a/Model/BenutzerModel.php b/Model/BenutzerModel.php index 6278b8b..2fb70da 100644 --- a/Model/BenutzerModel.php +++ b/Model/BenutzerModel.php @@ -113,29 +113,6 @@ class BenutzerModel extends Database return $result; } - - - 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