From fa3667394362b93d33a5725077b86aab7a89c6a4 Mon Sep 17 00:00:00 2001 From: tracer Date: Sat, 22 Jan 2022 19:09:44 +0100 Subject: [PATCH] initial commit Signed-off-by: tracer --- .../src/Controller/NameserverController.php | 182 ++++++++++++++++++ 1 file changed, 182 insertions(+) create mode 100644 bindAPI/src/Controller/NameserverController.php diff --git a/bindAPI/src/Controller/NameserverController.php b/bindAPI/src/Controller/NameserverController.php new file mode 100644 index 0000000..9c29d0a --- /dev/null +++ b/bindAPI/src/Controller/NameserverController.php @@ -0,0 +1,182 @@ +databaseConnection->getConnection()->query($statement); + return $statement->fetchAll(mode: PDO::FETCH_ASSOC); + } catch (PDOException $e) { + exit($e->getMessage()); + } + } + + + /** + * @param String $name + * + * @return array|false + */ + public function findByName(String $name): bool|array + { + $sql = " + SELECT id, name, a, aaaa, apikey + FROM " . DatabaseConnection::TABLE_NAMESERVERS . " + WHERE name = :name"; + + try { + $statement = $this->databaseConnection->getConnection()->prepare($sql); + $statement->bindParam(param: ':name', var: $name); + $statement->execute(); + return $statement->fetch(PDO::FETCH_ASSOC); + } catch (PDOException $e) { + exit($e->getMessage()); + } + } + + + /** + * @param Int $id + * + * @return array|false + */ + public function findByID(Int $id): bool|array + { + $sql = " + SELECT id, name, a, aaaa, apikey + FROM " . DatabaseConnection::TABLE_NAMESERVERS . " + WHERE id = :id"; + + try { + $statement = $this->databaseConnection->getConnection()->prepare($sql); + $statement->bindParam(param:':id', var: $id); + $statement->execute(); + return $statement->fetch(PDO::FETCH_ASSOC); + } catch (PDOException $e) { + exit($e->getMessage()); + } + } + + + /** + * @param String $name + * @param String $a + * @param String $aaaa + * @param String $apikey + * + * @return int + */ + public function insert(String $name, String $a, String $aaaa, String $apikey): int + { + $sql = " + INSERT INTO " . DatabaseConnection::TABLE_NAMESERVERS . " (name, a, aaaa, apikey) + VALUES (:name, :a, :aaaa, :apikey)"; + + try { + $statement = $this->databaseConnection->getConnection()->prepare($sql); + $statement->bindParam(param: ':name', var: $name); + $statement->bindParam(param: ':a', var: $a); + $statement->bindParam(param: ':aaaa', var: $aaaa); + $statement->bindParam(param: ':apikey', var: $apikey); + $statement->execute(); + return $this->databaseConnection->getConnection()->lastInsertId(); + } catch (PDOException $e) { + exit($e->getMessage()); + } + + } + + + /** + * @param Int $id + * @param String $name + * @param String $a + * @param String $aaaa + * @param String $apikey + * + * @return false|int + */ + public function update(Int $id, String $name, String $a, String $aaaa, String $apikey): bool|int + { + $current = $this->findByID($id); + + if (empty($name)) { + $name = $current['name'] ?? ''; + } + if (empty($a)) { + $a = $current['a'] ?? ''; + } + if (empty($aaaa)) { + $aaaa = $current['aaaa'] ?? ''; + } + if (empty($apikey)) { + $apikey = $current['apikey'] ?? ''; + } + + $sql = " + UPDATE " . DatabaseConnection::TABLE_NAMESERVERS . " SET + name = :name, + a = :a, + aaaa = :aaaa, + apikey = :apikey + WHERE id = :id"; + + try { + $statement = $this->databaseConnection->getConnection()->prepare($sql); + $statement->bindParam(param: 'id', var: $id); + $statement->bindParam(param: 'name', var: $name); + $statement->bindParam(param: 'a', var: $a); + $statement->bindParam(param: 'aaaa', var: $aaaa); + $statement->bindParam(param: 'apikey', var: $apikey); + $statement->execute(); + return $statement->rowCount(); + } catch (PDOException $e) { + print($e->getMessage()); + return false; + } + } + + + /** + * @param $id + * + * @return int + */ + public function delete($id): int + { + // TODO delete zone file and include + $statement = " + DELETE FROM " . DatabaseConnection::TABLE_NAMESERVERS . " + WHERE id = :id"; + + try { + $statement = $this->databaseConnection->getConnection()->prepare($statement); + $statement->bindParam(param: 'id', var: $id); + $statement->execute(); + return $statement->rowCount(); + } catch (PDOException $e) { + exit($e->getMessage()); + } + } +} \ No newline at end of file