diff --git a/gateway/listeGateway.php b/gateway/listeGateway.php index f962fd9..f7abf98 100644 --- a/gateway/listeGateway.php +++ b/gateway/listeGateway.php @@ -5,7 +5,7 @@ require_once('./metier/liste.php'); class ListeGateway { - + private $con; public function __construct(Connection $con){ @@ -32,7 +32,6 @@ class ListeGateway 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))); } @@ -42,27 +41,33 @@ class ListeGateway $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{ + public function getLastId(): array{ $query = "SELECT max(id) as oldId FROM Liste"; $this->con->executeQuery($query, array()); $results=$this->con->getResults(); - return $results[0]['oldId']; + return $results; } -/* public function findByName(string $name): array{ + $results=[]; if (!empty($name)){ - $query = "SELECT * FROM Tache WHERE name=:name"; + $name=$name.'%'; + $query = "SELECT * FROM Liste WHERE name LIKE :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; + $results=$this->con->getResults(); + return $results; } + return $results; + } + + public function findByUserId(string $userId): array{ + $query = "SELECT * FROM Liste WHERE idCreator=:idCreator"; + $this->con->executeQuery($query, array(':idCreator' => array($userId, PDO::PARAM_STR))); + + $results=$this->con->getResults(); + return $results; } -*/ } ?> \ No newline at end of file diff --git a/gateway/tacheGateway.php b/gateway/tacheGateway.php index 863ae87..e05abcb 100644 --- a/gateway/tacheGateway.php +++ b/gateway/tacheGateway.php @@ -32,6 +32,7 @@ class TacheGateway $query = "SELECT * FROM Tache t where idListe=:id"; $this->con->executeQuery($query, array(':id' => array($id, PDO::PARAM_INT))); $results=$this->con->getResults(); + return $results; foreach ($results as $row) { $tabTaches[]=new Tache($row['id'], $row['name'], $row['content'], $row['completed']); } diff --git a/gateway/userGateway.php b/gateway/userGateway.php index 58370a6..fb8a97f 100644 --- a/gateway/userGateway.php +++ b/gateway/userGateway.php @@ -25,24 +25,25 @@ class UserGateway $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 findByNamePassword(string $username, string $password): array{ + $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(); + return $results; + } + + public function findByName(string $username): array{ + $query = "SELECT * FROM Utilisateur WHERE username=:username"; + $this->con->executeQuery($query, array(':username' => array($username, PDO::PARAM_STR))); + $results=$this->con->getResults(); + return $results; } - public function getLastId(): int{ - $query = "SELECT max(id) as oldId FROM User"; + public function getLastId(): array{ + $query = "SELECT max(id) as oldId FROM Utilisateur"; $this->con->executeQuery($query, array()); $results=$this->con->getResults(); - return $results[0]['oldId']; + return $results; } /* diff --git a/index.php b/index.php index da0e894..546f7e2 100644 --- a/index.php +++ b/index.php @@ -9,6 +9,11 @@ require_once('gateway/tacheGateway.php'); require_once('gateway/userGateway.php'); require_once("gateway/listeGateway.php"); require_once('metier/user.php'); +require_once('metier/liste.php'); +require_once('metier/tache.php'); +require_once('modeles/mdlUser.php'); +require_once('modeles/mdlListe.php'); + //A CHANGER $user= 'thchazot1'; @@ -23,18 +28,19 @@ $content='Argent, Encore de l\'argent'; try{ $con=new Connection($dsn,$user,$pass); - $u=new User(1, "test", "mdp"); + $u=new User(2, "boby", "test"); $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); + $mdlUser=new MdlUser($con); + $mdlListe=new MdlListe($con); - $listeGateway->insert($l); - + $list=$mdlListe->update($u); + echo ($list[0]); /* $tacheGateway->delete($t); $user=$userGateway->insert(new User(1, "test", "mdp")); diff --git a/metier/liste.php b/metier/liste.php index 11d4c37..0143334 100644 --- a/metier/liste.php +++ b/metier/liste.php @@ -46,6 +46,10 @@ class Liste return $this->taches; } + public function setId(int $id){ + $this->id=$id; + } + public function setName(string $name){ $this->name=$name; } diff --git a/metier/user.php b/metier/user.php index 4eb6e1d..ad15a50 100644 --- a/metier/user.php +++ b/metier/user.php @@ -30,6 +30,10 @@ class User return $this->password; } + public function setId(int $id){ + $this->id=$id; + } + public function setUsername(string $username){ $this->username=$username; } diff --git a/modeles/mdlListe.php b/modeles/mdlListe.php new file mode 100644 index 0000000..2d66bf2 --- /dev/null +++ b/modeles/mdlListe.php @@ -0,0 +1,77 @@ +con = $con; + } + + + public function insert(string $name, bool $private, ?User $creator): Liste{ + $gate=new ListeGateway($this->con); + $l=new Liste(0, $name, $private, $creator, []); + $gate->insert($l); + $id=$gate->getLastId(); + $l->setId($id[0]['oldId']); + return $l; + } + + public function delete(Liste $liste){ + $gate=new ListeGateway($this->con); + $gate->delete($liste); + } + + public function update(Liste $liste, string $name, string $content, bool $private){ + $gate=new ListeGateway($this->con); + $liste->setName($name); + $liste->setContent($content); + if ($liste->getCreator()!=null){ + $liste->setPrivate($private); + } + $gate->update($liste); + } + + public function findByName(string $name, ?User $u): array{ + $gate=new ListeGateway($this->con); + $gateTache=new TacheGateway($this->con); + $results=$gate->findByName($name); + $tabListe=[]; + foreach ($results as $row ) { + $tabTache=[]; + $taches=$gateTache->getTacheFromIdList($row['id']); + foreach($taches as $tata){ + $tabTache[]=new Tache($tata['id'], $tata['name'], $tata['content'], $tata['completed']); + } + if ($row['private']==false){ + $tabListe[]=new Liste($row['id'], $row['name'], $row['private'], null, $tabTache); + } + if ($row['idCreator']==$u->getId()){ + $tabListe[]=new Liste($row['id'], $row['name'], $row['private'], $u, $tabTache); + } + } + return $tabListe; + } + + public function findByUser(User $u): array{ + $gate=new ListeGateway($this->con); + $gateTache=new TacheGateway($this->con); + + $results=$gate->findByUserId($u->getId()); + $tabListe=[]; + foreach ($results as $row ) { + $tabTache=[]; + $taches=$gateTache->getTacheFromIdList($row['id']); + foreach($taches as $tata){ + $tabTache[]=new Tache($tata['id'], $tata['name'], $tata['content'], $tata['completed']); + } + $tabListe[]=new Liste($row['id'], $row['name'], $row['private'], $u, $tabTache); + } + return $tabListe; + } + +} + +?> \ No newline at end of file diff --git a/modeles/mdlTache.php b/modeles/mdlTache.php new file mode 100644 index 0000000..280623f --- /dev/null +++ b/modeles/mdlTache.php @@ -0,0 +1,37 @@ +con = $con; + } + + + public function insert(string $name, string $content ,bool $completed, string $idListe): Tache{ + $gate=new TacheGateway($this->con); + $t=new Tache(0, $name, $content, $completed); + $gate->insert($t); + $id=$gate->getLastId(); + $t->setId($id[0]['oldId']); + return $t; + } + + public function delete(Tache $tache){ + $gate=new TacheGateway($this->con); + $gate->delete($tache); + } + + public function update(Tache $tache, string $name, string $content, bool $completed){ + $gate=new ListeGateway($this->con); + $tache->setName($name); + $tache->setContent($content); + $tache->setCompleted($completed); + $gate->update($tache); + } + +} + +?> \ No newline at end of file diff --git a/modeles/mdlUser.php b/modeles/mdlUser.php new file mode 100644 index 0000000..a23efe4 --- /dev/null +++ b/modeles/mdlUser.php @@ -0,0 +1,58 @@ +con = $con; + } + + public function getUserForConnection(string $username, string $password): ?User{ + if (!empty($username) && !empty($password)){ + $results=[]; + $gate=new UserGateway($this->con); + $results=$gate->findByNamePassword($username, $password); + if (!empty($results)){ + $user=new User($results[0]['id'], $results[0]['username'], $results[0]['password']); + return $user; + } + } + return null; + } + + public function insert(string $username, string $password): ?User{ + $gate=new UserGateway($this->con); + $results=$gate->findByName($username); + if ($results==null){ + $u=new User(0, $username, $password); + $gate->insert($u); + $id=$gate->getLastId(); + $u->setId($id[0]['oldId']); + return $u; + } + return null; + } + + public function delete(User $user){ + $gate=new UserGateway($this->con); + $gate->delete($user); + } + + public function update(User $user, string $username, string $password){ + $user->setUsername($username); + $user->setPassword($password); + $gate=new UserGateway($this->con); + $gate->update($user); + } + + +} + + + + + + +?> \ No newline at end of file