From 782311304ee8db373b7ffd4fbe481756f8cc0a0e Mon Sep 17 00:00:00 2001 From: Samuel Wolff Date: Wed, 6 Dec 2023 08:33:25 +0100 Subject: [PATCH] Alle Controller und Models --- Controller/BenutzerController.php | 35 ++++++++ Controller/KindController.php | 47 ++++++++++ Library/Msg.php | 31 +++++++ Model/BenutzerModel.php | 80 +++++++++++++++++ Model/Database.php | 36 ++++++++ Model/KindModel.php | 141 ++++++++++++++++++++++++++++++ restAPI.php | 85 ++++++++++++++++++ 7 files changed, 455 insertions(+) create mode 100644 Controller/BenutzerController.php create mode 100644 Controller/KindController.php create mode 100644 Library/Msg.php create mode 100644 Model/BenutzerModel.php create mode 100644 Model/Database.php create mode 100644 Model/KindModel.php create mode 100644 restAPI.php diff --git a/Controller/BenutzerController.php b/Controller/BenutzerController.php new file mode 100644 index 0000000..fbf653d --- /dev/null +++ b/Controller/BenutzerController.php @@ -0,0 +1,35 @@ +db = new BenutzerModel(); + } + + public function updateBenutzer($elternId, $data){ + + $result = $this->db->updateBenutzer($benutzerId, $data); + + return json_encode($result); + + } + + public function insertBenutzer($data){ + $result = $this->db->insertBenutzer($data) + + return json_encode($data); + } + +} + +?> diff --git a/Controller/KindController.php b/Controller/KindController.php new file mode 100644 index 0000000..c2e4f2f --- /dev/null +++ b/Controller/KindController.php @@ -0,0 +1,47 @@ +db = new KindModel(); + } + + // $parentId ist standardmäßig auf false und gibt damit alle Kinder aus, + // das setzen gibt nur Kinder eines bestimmten Benutzerkontos aus. + public function getKind($parentId = false){ + $result = $this->db->getKind($parentId); + + return json_encode($result); + } + + public function updateKind($kindId, $data){ + $result = $this->db->updateKind($kindId, $data); + + return json_encode($result); + } + + public function addKind($data){ + $result = $this->db->addKind($data); + + return json_encode($data); + } + + public function deleteKind($kindId){ + $result = $this->db->deleteKind($kindId); + + return $result; + } + +} + +?> \ No newline at end of file diff --git a/Library/Msg.php b/Library/Msg.php new file mode 100644 index 0000000..446e6f1 --- /dev/null +++ b/Library/Msg.php @@ -0,0 +1,31 @@ + true, + "msg" => is_null($msg) ? 'Ihre Anfrage konnte nicht verarbeitet werden' : $msg, + "ex" => $striped + )); + die; + } else { + echo json_encode(array("isError" => false)); + } + + } +} \ No newline at end of file diff --git a/Model/BenutzerModel.php b/Model/BenutzerModel.php new file mode 100644 index 0000000..0183c29 --- /dev/null +++ b/Model/BenutzerModel.php @@ -0,0 +1,80 @@ +linkDB(); + + $params = array(); + + $sql = "UPDATE Benutzer SET" + + foreach($data as $index=>$value){ + $sql .= " ".$index." = :".$index; + $params[":".$index] = $value; + } + + $sql .= " WHERE id = :benutzerId;"; + + $params[":benutzerId"] = $kindId; + + try{ + $stmt = $pdo->prepare($sql); + $stmt->excute($params); + } + catch(\PDOException $e){ + return false; + } + + $result = $stmt->fetchALL(\PDO::FETCH_ASSOC); + + return $result; + } + + + public function insertBenutzer($data){ + $pdo = $this->linkDB(); + + $params = array(); + + $sql = "INSERT INTO Benutzer ("; + + foreach($data as $index=>$value){ + $sql .= $index.", "; + $params[":"+$index] = $index; + } + + $sql = substr($sql, 0, strlen($sql)-2).") VALUES ("; + + foreach($data as $value){ + $sql .= ":".$value." ,"; + $params[":"+$value] = $value; + } + + $sql = substr($sql, 0, strlen($sql)-2).");"; + + try{ + $stmt = $pdo->prepare($sql); + $stmt->excute($params); + } + catch(\PDOException $e){ + return false; + } + + $result = $stmt->fetchALL(\PDO::FETCH_ASSOC); + + return $result; + } + + +} + +?> diff --git a/Model/Database.php b/Model/Database.php new file mode 100644 index 0000000..107da05 --- /dev/null +++ b/Model/Database.php @@ -0,0 +1,36 @@ +dbName;host=$this->linkName" + , $this->user + , $this->pw + , array(\PDO::ATTR_ERRMODE => \PDO::ERRMODE_EXCEPTION)); + return $pdo; + } catch (\PDOException $e) { + new Msg(true, null, $e); + } + } + + +} \ No newline at end of file diff --git a/Model/KindModel.php b/Model/KindModel.php new file mode 100644 index 0000000..ae25b61 --- /dev/null +++ b/Model/KindModel.php @@ -0,0 +1,141 @@ +linkDB(); + + $params = array(); + $sql = "SELECT * FROM Kind"; + + + // Das mitgeben einer Id erlaubt es die Kinder eines bestimmten Benutzerkontos anzeigen zu lassen, + // während das leerlassen alle Kinder ausgibt. + id($parentId !== false){ + $sql. = " WHERE bid=:id;"; + $params[":id"] = $parentId; + } + + try{ + $stmt = $pdo->prepare($sql); + $stmt->excute($params); + } + catch(\PDOException $e){ + return false; + } + + $result = $stmt->fetchALL(\PDO::FETCH_ASSOC); + + return $result; +} + +/** + * Updated die Daten eines Kindes + * + * @param $kindId Id des Kindes + * @param $data Json encoded Daten mit den neuen Werten + */ +public function updateKind($kindId, $data){ + $pdo = $this->linkDB(); + + $params = array(); + $sql = "UPDATE KIND SET"; + + // Geht die Json-Daten durch und erweitert den SQL-Query + // und setzt die Bindparameter + // $index -> Spalte die geupdated wird + // $value -> neuer Wert + foreach($data as $index=>$value){ + $sql .= " ".$index." = :".$index; + $params[":".$index] = $value; + } + + $sql .= " WHERE id = :kindId;"; + + $params[":kindId"] = $kindId; + + try{ + $stmt = $pdo->prepare($sql); + $stmt->excute($params); + } + catch(\PDOException $e){ + return false; + } + + $result = $stmt->fetchALL(\PDO::FETCH_ASSOC); + + return $result; +} + +/** + * @param $data Die Daten für das neue Kind + */ +public function addKind($data){ + $pdo = $this->linkDB(); + + $params = array(); + + $sql = "INSERT INTO Kind ("; + + foreach($data as $index=>$value){ + $sql .= $index.", "; + $params[":"+$index] = $index; + } + + $sql = substr($sql, 0, strlen($sql)-2).") VALUES ("; + + foreach($data as $value){ + $sql .= ":".$value." ,"; + $params[":"+$value] = $value; + } + + $sql = substr($sql, 0, strlen($sql)-2).");"; + + try{ + $stmt = $pdo->prepare($sql); + $stmt->excute($params); + } + catch(\PDOException $e){ + return false; + } + + $result = $stmt->fetchALL(\PDO::FETCH_ASSOC); + + return $result; +} + + +public function deleteKind($kindId){ + $pdo = $this->linkDB(); + + $params = array(); + + $sql = "DELETE FROM Kind WHERE id = :id"; + + $params[":id"] = $kindId; + + try{ + $stmt = $pdo->prepare($sql); + $stmt->excute($params); + } + 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 new file mode 100644 index 0000000..c0cb206 --- /dev/null +++ b/restAPI.php @@ -0,0 +1,85 @@ +$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