From 087d45389f3c8648225ea1423f841241009453b1 Mon Sep 17 00:00:00 2001 From: majean5 Date: Tue, 6 Dec 2022 10:12:25 +0100 Subject: [PATCH] Gateways --- Connection.php | 34 +++++++++++ Liste.php | 27 +++++++++ ListeGateway.php | 96 ++++++++++++++++++++++++++++++ Tache.php | 33 ++++++++++ UserGateway.php | 152 +++++++++++++++++++++++++++++++++++++++++++++++ Utilisateur.php | 30 ++++++++++ 6 files changed, 372 insertions(+) create mode 100644 Connection.php create mode 100644 Liste.php create mode 100644 ListeGateway.php create mode 100644 Tache.php create mode 100644 UserGateway.php create mode 100644 Utilisateur.php diff --git a/Connection.php b/Connection.php new file mode 100644 index 0000000..19ed707 --- /dev/null +++ b/Connection.php @@ -0,0 +1,34 @@ +setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); + } + + + /** * @param string $query + * @param array $parameters * + * @return bool Returns `true` on success, `false` otherwise + */ + + public function executeQuery(string $query, array $parameters = []) : bool{ + $this->stmt = parent::prepare($query); + foreach ($parameters as $name => $value) { + $this->stmt->bindValue($name, $value[0], $value[1]); + } + + return $this->stmt->execute(); + } + + public function getResults() : array { + return $this->stmt->fetchall(); + + } + } + +?> \ No newline at end of file diff --git a/Liste.php b/Liste.php new file mode 100644 index 0000000..10f104f --- /dev/null +++ b/Liste.php @@ -0,0 +1,27 @@ +id=$i; + $this->nom=$n; + $this->taches=$t; + } + + function get_id(): int { + return $this->id; + } + + function get_nom(): string { + return $this->nom; + } + + function get_taches(): array { + return $this->taches; + } +} + +?> \ No newline at end of file diff --git a/ListeGateway.php b/ListeGateway.php new file mode 100644 index 0000000..d082a7a --- /dev/null +++ b/ListeGateway.php @@ -0,0 +1,96 @@ +co = $co; + } + + public function getByCreator(int $idUsr) : array { + $listes = null; + $taches = null; + if(!empty($idUsr)){ + try { + $co = $this->co; + + $query = "SELECT idListe FROM HasList WHERE idUser=:idUser"; + + $co->executeQuery($query, array(':id' => array($id, PDO::PARAM_STR))); + + $results = $co->getResults(); + + Foreach($results as $row){ + $idListe = $row['idListe']; + $queryTaches = "SELECT t.* FROM Tache t, HasTache h WHERE t.id=h.idTache AND h.idListe=:idListe"; + $co->executeQuery($queryTaches, array(':idListe' => array($idListe, PDO::PARAM_STR))); + $resultsTaches = $co->getResults(); + + Foreach($resultsTaches as $rowTaches){ + $taches[] = new Tache($rowTaches['id'], $rowTaches['intitule'], $rowTaches['isCompleted'], $rowTaches['description']); + } + + $listes[] = new Liste($row['id'], $row['nom'], $taches); + $taches = null; + } + } + catch(PDOException $Exception) { + echo 'erreur'; + echo $Exception->getMessage(); + } + } + return $listes; + } + + public function creerTache(int $id, string $intitule, boolean $isCompleted){ + if(!empty($id) && !empty($intitutle)){ + try{ + $co = $this->co; + + $query = "INSERT INTO Tache VALUES (:id, :intitule, :isCompleted)"; + + $co->executeQuery($query, array(':id' => array($id, PDO::PARAM_STR), ':intitule' => array($nom, PDO::PARAM_STR), ':isCompleted' => array($taches, PDO::PARAM_STR))); + } + catch(PDOException $Exception){ + echo 'erreur'; + echo $Exception->getMessage(); + } + } + } + + public function delTache(int $id){ + if(!empty($id)){ + try{ + $co = $this->co; + + $query = "DELETE FROM Tache WHERE id=:id"; + + $co->executeQuery($query, array(':id' => array($id, PDO::PARAM_STR))); + } + catch(PDOException $Exception){ + echo 'erreur'; + echo $Exception->getMessage(); + } + } + } + + public function completeTache(int $id){ + if(!empty($id)){ + try{ + $co = $this->co; + + $query = "UPDATE Tache SET isCompleted=true WHERE id=:id"; + + $co->executeQuery($query, array(':id' => array($id, PDO::PARAM_STR))); + } + catch(PDOException $Exception){ + echo 'erreur'; + echo $Exception->getMessage(); + } + } + } +} + +?> \ No newline at end of file diff --git a/Tache.php b/Tache.php new file mode 100644 index 0000000..1e22090 --- /dev/null +++ b/Tache.php @@ -0,0 +1,33 @@ +id = $i; + $this->intitule = $in; + $this->isCompleted = $is; + $this->description = $desc; + } + + function get_id(): int { + return $this->id; + } + + function get_intitule(): string { + return $this->intitule; + } + + function get_isCompleted(): boolean { + return $this->isCompleted; + } + + function get_description(): string { + return $this->description; + } +} + +?> \ No newline at end of file diff --git a/UserGateway.php b/UserGateway.php new file mode 100644 index 0000000..9dadc5a --- /dev/null +++ b/UserGateway.php @@ -0,0 +1,152 @@ +co = $co; + } + + public function creerUtilisateur(int $id, string $nom, string $pwd){ + if(!empty($id) && !empty($nom) && empty($password)){ + try{ + $co = $this->co; + + $query = "INSERT INTO Utilisateur VALUES (:id, :nom, :pwd)"; + + $co->executeQuery($query, array(':id' => array($id, PDO::PARAM_STR), ':nom' => array($nom, PDO::PARAM_STR), ':pwd' => array($pwd, PDO::PARAM_STR))); + } + catch(PDOException $Excception){ + echo 'erreur'; + echo $Exception->getMessage(); + } + } + } + + public function delUtilisateur(int $id){ + if(!empty($id)){ + try{ + $co = $this->co; + + $query = "DELETE FROM Utilisateur WHERE id=:id"; + + $co->executeQuery($query, array(':id' => array($id, PDO::PARAM_STR))); + } + catch(PDOException $Exception){ + echo 'erreur'; + echo $Exception->getMessage(); + } + } + } + + public function putUtilisateur(Utilisateur $usr){ + if(!empty($usr.getId()) && !empty($usr.getNom()) && empty($usr.getPassword())){ + try{ + $co = $this->co; + + $updateQuery = "UPDATE Utilisateur SET id=:id AND nom=:nom AND pwd=:pwd"; + $deleteQuery = "DELETE FROM HasList WHERE user=:id AND liste=:liste"; + $insertQuery = "INSERT INTO HasList VALUES (:id, :liste)"; + + $co->executeQuery($updateQuery, array(':id' => array($usr.getId(), PDO::PARAM_STR), ':nom' => array($usr.getNom(), PDO::PARAM_STR), ':pwd' => array($usr.getPassword(), PDO::PARAM_STR))); + foreach($usr.getListListe() as $l){ + $co->executeQuery($deleteQuery, array(':id' => array($usr.getId(), PDO::PARAM_STR), ':liste' => array($l, PDO::PARAM_STR))); + $co->executeQuery($insertQuery, array(':id' => array($usr.getId(), PDO::PARAM_STR), ':liste' => array($l, PDO::PARAM_STR))); + } + + } + + catch(PDOException $Excception){ + echo 'erreur'; + echo $Excception->getMesage(); + } + } + } + + public function getUtilisateurById(int $id) : Utilisateur { + $usr = null; + if(!empty($id)){ + try{ + $co = $this->co; + + $query = "SELECT * FROM Utilisateur WHERE id=:id"; + + $co->executeQuery($query, array(':id' => array($id, PDO::PARAM_STR))); + + $results = $co->getResults(); + + Foreach($results as $row){ + $usr = new Utilisateur($row['id'], $row['nom'], $row['pwd']); + } + } + catch(PDOException $Exception){ + echo 'erreur'; + echo $Exception->getMessage(); + } + + } + return $usr; + } + + public function getUtilisateurbyNameAndPassword(string $nom, string $pwd) : Utilisateur { + $usr = null; + if(!empty($nom) && !empty($password)){ + try{ + $co = $this->co; + + $query = "SELECT * FROM Utilisateur WHERE nom=:nom AND pwd=:pwd"; + + $co->executeQuery($query, array(':nom' => array($nom, PDO::PARAM_STR), ':pwd' => array($pwd, PDO::PARAM_STR))); + + $results = $co->getResults(); + + Foreach($results as $row){ + $usr = new Utilisateur($row['id'], $row['nom'], $row['pwd']); + } + } + catch(PDOException $Exception){ + echo 'erreur'; + echo $Exception->getMessage(); + } + } + return $usr; + } + + public function creerListe(int $id, string $nom){ + if(!empty($id) && !empty($nom)){ + try{ + $co = $this->co; + + $query = "INSERT INTO Liste VALUES (:id, :nom, :taches)"; + + $co->executeQuery($query, array(':id' => array($id, PDO::PARAM_STR), ':nom' => array($nom, PDO::PARAM_STR), ':taches' => array($taches, PDO::PARAM_STR))); + } + catch(PDOException $Exception){ + echo 'erreur'; + echo $Exception->getMessage(); + } + } + } + + public function delListe(int $id){ + if(!empty($id)){ + try{ + $co = $this->co; + + $query = "DELETE FROM Tache WHERE id=:id"; + + $co->executeQuery($query, array(':id' => array($id, PDO::PARAM_STR))); + } + catch(PDOException $Exception){ + echo 'erreur'; + echo $Exception->getMessage(); + } + } + } + +} + +?> \ No newline at end of file diff --git a/Utilisateur.php b/Utilisateur.php new file mode 100644 index 0000000..72bf17f --- /dev/null +++ b/Utilisateur.php @@ -0,0 +1,30 @@ +id=$i; + $this->nom=$n; + $this->password=$p; + $this->listListe=$liste; + } + function get_id(): int { + return $this->id; + } + function get_nom(): string { + return $this->nom; + } + function get_password(): string { + return $this->password; + } + function get_listListe(){ + return $this->get_listListe; + } +} + + +?> \ No newline at end of file