From b2fb03908a743aea7517e95c929c47b1a126e133 Mon Sep 17 00:00:00 2001 From: thchazot1 Date: Tue, 6 Dec 2022 11:24:58 +0100 Subject: [PATCH] toutes les gateways --- index.php | 27 ++++++++++++++--- liste.php | 33 +++++++++++--------- listeGateway.php | 68 ++++++++++++++++++++++++++++++++++++++++++ tache.php | 8 ++--- tacheGateway.php | 17 +++++++++++ user.php | 14 +++++---- userGateway.php | 64 +++++++++++++++++++++++++++++++++++++++ utilisateurGateway.php | 42 -------------------------- 8 files changed, 204 insertions(+), 69 deletions(-) create mode 100644 listeGateway.php create mode 100644 userGateway.php delete mode 100644 utilisateurGateway.php diff --git a/index.php b/index.php index 54961ad..b0c8622 100644 --- a/index.php +++ b/index.php @@ -6,22 +6,40 @@ require_once("connection.php"); require_once('tacheGateway.php'); +require_once('userGateway.php'); +require_once("listeGateway.php"); +require_once('user.php'); //A CHANGER $user= 'thchazot1'; $pass='achanger'; $dsn='mysql:host=localhost;dbname=dbthchazot1'; -$id=468; -$name='LALALILALOU'; +$id=1; +$name='Argent'; $content='Argent, Encore de l\'argent'; try{ $con=new Connection($dsn,$user,$pass); - $t=new Tache($id, $name, $content, FALSE); - $gateway=new TacheGateway($con); + $u=new User(1, "test", "mdp"); + $t=new Tache($id, $name, $content, true); + $t1=new Tache(478, "TEST", $content, false); + $l=new Liste(6, "test", false, null, array($t, $t1)); + + $tacheGateway=new TacheGateway($con); + $userGateway=new UserGateway($con); + $listeGateway=new ListeGateway($con); + + + $listeGateway->insert($l); + +/* + $tacheGateway->delete($t); + $user=$userGateway->insert(new User(1, "test", "mdp")); + $user=$userGateway->findByNamePassword("test", "mdp"); + print($user); $gateway->insert($t, 1); $tabTache=$gateway->getTacheFromIdList(1); @@ -29,6 +47,7 @@ try{ print($tache); echo "
"; } + */ } catch( PDOException $Exception ) { echo 'erreur'; diff --git a/liste.php b/liste.php index 949b959..11d4c37 100644 --- a/liste.php +++ b/liste.php @@ -6,18 +6,19 @@ require_once('tache.php'); class Liste { - private $id; - private $name; - private $private; - private $creator; - private $taches; + private int $id; + private string $name; + private bool $private; + private ?User $creator; + private array $taches; - function __construct( int $id, string $name, boolean $private, User $creator, array $taches) + function __construct(int $id, string $name, bool $private, ?User $creator, array $taches) { $this->id=$id; $this->name=$name; - $this->$creator=$creator; - $this->$taches=$taches; + $this->private=$private; + $this->creator=$creator; + $this->taches=$taches; } public function __toString() @@ -26,30 +27,34 @@ class Liste } public function getId(){ - return $id; + return $this->id; } public function getName(){ - return $name; + return $this->name; } public function getCreator(){ - return $creator; + return $this->creator; + } + + public function getPrivate(){ + return $this->private; } public function getTaches(){ - return $taches; + return $this->taches; } public function setName(string $name){ $this->name=$name; } - public function setPrivate(boolean $private){ + public function setPrivate(bool $private){ $this->private=$private; } - public function setCreator(string $creator){ + public function setCreator(?User $creator){ $this->creator=$creator; } diff --git a/listeGateway.php b/listeGateway.php new file mode 100644 index 0000000..81a2346 --- /dev/null +++ b/listeGateway.php @@ -0,0 +1,68 @@ +con = $con; + } + + public function insert(Liste $l): void{ + $tacheGateway=new TacheGateway($this->con); + foreach($l->getTaches() as $taches){ + $tacheGateway->insert($taches, $this->getLastId()+1); + } + if ($l->getCreator()==null){ + $query = "INSERT INTO Liste VALUES (null, :name, :private, null)"; + $this->con->executeQuery($query, array(':name' => array($l->getName(), PDO::PARAM_STR), ':private' => array($l->getPrivate(), PDO::PARAM_BOOL))); + } + else{ + $query = "INSERT INTO Liste VALUES (null, :name, :private, :creator)"; + $this->con->executeQuery($query, array(':name' => array($l->getName(), PDO::PARAM_STR), ':private' => array($l->getPrivate(), PDO::PARAM_BOOL), ':creator' => array($l->getCreator()->getId(), PDO::PARAM_STR))); + } + } + + public function delete(Liste $l): void{ + $tacheGateway=new TacheGateway($this->con); + foreach($l->getTaches() as $taches){ + $tacheGateway->delete($taches); + } + + $query = "DELETE FROM Liste where id=:id"; + $this->con->executeQuery($query, array(':id' => array($l->getId(), PDO::PARAM_INT))); + } + + public function update(Liste $l): void{ + $query = "UPDATE Liste SET name=:name, private=:private WHERE id=:id"; + $this->con->executeQuery($query, array(':id' => array($l->getId(), PDO::PARAM_INT), ':name' => array($l->getName(), PDO::PARAM_STR), ':private' => array($l->getPrivate(), PDO::PARAM_BOOL))); + } + + public function getLastId(): int{ + $query = "SELECT max(id) as oldId FROM Liste"; + $this->con->executeQuery($query, array()); + $results=$this->con->getResults(); + return $results[0]['oldId']; + } + +/* + public function findByName(string $name): array{ + if (!empty($name)){ + $query = "SELECT * FROM Tache WHERE name=:name"; + $this->con->executeQuery($query, array(':name' => array($name, PDO::PARAM_STR))); + + $results=$con->getResults(); + foreach ($results as $row ) { + $tabTaches[]=new Tache($row['id'], $row['name'], $row['content']); + } + return $tabTaches; + } + } +*/ +} + +?> \ No newline at end of file diff --git a/tache.php b/tache.php index a9a429a..e0a9e44 100644 --- a/tache.php +++ b/tache.php @@ -4,10 +4,10 @@ require_once('user.php'); class Tache { - private $id; - private $name; - private $content; - private $completed; + private int $id; + private string $name; + private string $content; + private bool $completed; function __construct( int $id, string $name, string $content, bool $completed) { diff --git a/tacheGateway.php b/tacheGateway.php index a3acd3c..9a253da 100644 --- a/tacheGateway.php +++ b/tacheGateway.php @@ -17,6 +17,16 @@ class TacheGateway $this->con->executeQuery($query, array(':name' => array($t->getName(), PDO::PARAM_STR), ':content' => array($t->getContent(), PDO::PARAM_STR), ':completed' => array($t->getCompleted(), PDO::PARAM_BOOL), ':idList' => array($idList, PDO::PARAM_INT))); } + public function delete(Tache $t): void{ + $query = "DELETE FROM Tache where id=:id"; + $this->con->executeQuery($query, array(':id' => array($t->getId(), PDO::PARAM_INT))); + } + + public function update(Tache $t): void{ + $query = "UPDATE Tache SET name=:name, content=:content, completed=:completed WHERE id=:id"; + $this->con->executeQuery($query, array(':id' => array($t->getId(), PDO::PARAM_INT), ':name' => array($t->getName(), PDO::PARAM_STR), ':content' => array($t->getContent(), PDO::PARAM_STR), ':completed' => array($t->getCompleted(), PDO::PARAM_BOOL))); + } + public function getTacheFromIdList(int $id): array{ $tabTaches=[]; $query = "SELECT * FROM Tache t where idListe=:id"; @@ -28,6 +38,13 @@ class TacheGateway return $tabTaches; } + public function getLastId(): int{ + $query = "SELECT max(id) as oldId FROM Tache"; + $this->con->executeQuery($query, array()); + $results=$this->con->getResults(); + return $results[0]['oldId']; + } + /* public function findByName(string $name): array{ if (!empty($name)){ diff --git a/user.php b/user.php index af208ae..4eb6e1d 100644 --- a/user.php +++ b/user.php @@ -1,6 +1,6 @@ id . " " . $this->username . " " . $this->contenu; + return $this->id . " " . $this->username; } public function getId(){ - return $id; + return $this->id; } public function getUsername(){ - return $username; + return $this->username; } public function getPassword(){ - return $password; + return $this->password; } public function setUsername(string $username){ $this->username=$username; } + + public function setPassword(string $password){ + $this->password=$password; + } } ?> \ No newline at end of file diff --git a/userGateway.php b/userGateway.php new file mode 100644 index 0000000..67eba8e --- /dev/null +++ b/userGateway.php @@ -0,0 +1,64 @@ +con = $con; + } + + public function insert(User $u): void{ + $query = "INSERT INTO Utilisateur VALUES (null, :username, :password)"; + $this->con->executeQuery($query, array(':username' => array($u->getUsername(), PDO::PARAM_STR), ':password' => array($u->getPassword(), PDO::PARAM_STR))); + } + + public function delete(User $u): void{ + $query = "DELETE FROM Utilisateur where id=:id"; + $this->con->executeQuery($query, array(':id' => array($u->getId(), PDO::PARAM_INT))); + } + + public function update(User $u): void{ + $query = "UPDATE Utilisateur SET username=:username, password=:password WHERE id=:id"; + $this->con->executeQuery($query, array(':id' => array($u->getId(), PDO::PARAM_INT), ':username' => array($u->getUsername(), PDO::PARAM_STR), ':password' => array($u->getPassword(), PDO::PARAM_STR))); + } + + public function findByNamePassword(string $username, string $password): ?User{ + if (!empty($username) && !empty($password)){ + $query = "SELECT * FROM Utilisateur WHERE username=:username AND password=:password"; + $this->con->executeQuery($query, array(':username' => array($username, PDO::PARAM_STR), ':password' => array($password, PDO::PARAM_STR))); + $results=$this->con->getResults(); + if (!empty($results)){ + $user=new User($results[0]['id'], $results[0]['username'], $results[0]['password']); + return $user; + } + } + return null; + } + + public function getLastId(): int{ + $query = "SELECT max(id) as oldId FROM User"; + $this->con->executeQuery($query, array()); + $results=$this->con->getResults(); + return $results[0]['oldId']; + } + +/* + public function getTacheFromIdList(int $id): array{ + $tabTaches=[]; + $query = "SELECT * FROM Tache t where idListe=:id"; + $this->con->executeQuery($query, array(':id' => array($id, PDO::PARAM_INT))); + $results=$this->con->getResults(); + foreach ($results as $row) { + $tabTaches[]=new Tache($row['id'], $row['name'], $row['content'], $row['completed']); + } + return $tabTaches; + } + + +*/ +} + +?> \ No newline at end of file diff --git a/utilisateurGateway.php b/utilisateurGateway.php deleted file mode 100644 index 8fa6a81..0000000 --- a/utilisateurGateway.php +++ /dev/null @@ -1,42 +0,0 @@ - - - -test - -insert($t); - - $query = "SELECT * FROM Tache"; - - $con->executeQuery($query, array(':id' => array($id, PDO::PARAM_STR))); - $results=$con->getResults(); - Foreach($results as $row){ - echo ($row['id']. " | ". $row['name'] . " | ". $row['content'] . "

"); - } -} -catch( PDOException $Exception ) { - echo 'erreur'; - echo $Exception->getMessage(); -} -?*/ -?> - - -