plein d'ajouts

main
thchazot1 2 years ago
parent 6a1acab12f
commit 482c558202

@ -32,7 +32,6 @@ class ListeGateway
foreach($l->getTaches() as $taches){ foreach($l->getTaches() as $taches){
$tacheGateway->delete($taches); $tacheGateway->delete($taches);
} }
$query = "DELETE FROM Liste where id=:id"; $query = "DELETE FROM Liste where id=:id";
$this->con->executeQuery($query, array(':id' => array($l->getId(), PDO::PARAM_INT))); $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))); $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"; $query = "SELECT max(id) as oldId FROM Liste";
$this->con->executeQuery($query, array()); $this->con->executeQuery($query, array());
$results=$this->con->getResults(); $results=$this->con->getResults();
return $results[0]['oldId']; return $results;
} }
/*
public function findByName(string $name): array{ public function findByName(string $name): array{
$results=[];
if (!empty($name)){ 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))); $this->con->executeQuery($query, array(':name' => array($name, PDO::PARAM_STR)));
$results=$con->getResults(); $results=$this->con->getResults();
foreach ($results as $row ) { return $results;
$tabTaches[]=new Tache($row['id'], $row['name'], $row['content']);
}
return $tabTaches;
} }
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;
} }
*/
} }
?> ?>

@ -32,6 +32,7 @@ class TacheGateway
$query = "SELECT * FROM Tache t where idListe=:id"; $query = "SELECT * FROM Tache t where idListe=:id";
$this->con->executeQuery($query, array(':id' => array($id, PDO::PARAM_INT))); $this->con->executeQuery($query, array(':id' => array($id, PDO::PARAM_INT)));
$results=$this->con->getResults(); $results=$this->con->getResults();
return $results;
foreach ($results as $row) { foreach ($results as $row) {
$tabTaches[]=new Tache($row['id'], $row['name'], $row['content'], $row['completed']); $tabTaches[]=new Tache($row['id'], $row['name'], $row['content'], $row['completed']);
} }

@ -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))); $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{ public function findByNamePassword(string $username, string $password): array{
if (!empty($username) && !empty($password)){ $query = "SELECT * FROM Utilisateur WHERE username=:username AND password=: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)));
$this->con->executeQuery($query, array(':username' => array($username, PDO::PARAM_STR), ':password' => array($password, PDO::PARAM_STR))); $results=$this->con->getResults();
$results=$this->con->getResults(); return $results;
if (!empty($results)){ }
$user=new User($results[0]['id'], $results[0]['username'], $results[0]['password']);
return $user; public function findByName(string $username): array{
} $query = "SELECT * FROM Utilisateur WHERE username=:username";
} $this->con->executeQuery($query, array(':username' => array($username, PDO::PARAM_STR)));
return null; $results=$this->con->getResults();
return $results;
} }
public function getLastId(): int{ public function getLastId(): array{
$query = "SELECT max(id) as oldId FROM User"; $query = "SELECT max(id) as oldId FROM Utilisateur";
$this->con->executeQuery($query, array()); $this->con->executeQuery($query, array());
$results=$this->con->getResults(); $results=$this->con->getResults();
return $results[0]['oldId']; return $results;
} }
/* /*

@ -9,6 +9,11 @@ require_once('gateway/tacheGateway.php');
require_once('gateway/userGateway.php'); require_once('gateway/userGateway.php');
require_once("gateway/listeGateway.php"); require_once("gateway/listeGateway.php");
require_once('metier/user.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 //A CHANGER
$user= 'thchazot1'; $user= 'thchazot1';
@ -23,18 +28,19 @@ $content='Argent, Encore de l\'argent';
try{ try{
$con=new Connection($dsn,$user,$pass); $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); $t=new Tache($id, $name, $content, true);
$t1=new Tache(478, "TEST", $content, false); $t1=new Tache(478, "TEST", $content, false);
$l=new Liste(6, "test", false, null, array($t, $t1));
$tacheGateway=new TacheGateway($con); $tacheGateway=new TacheGateway($con);
$userGateway=new UserGateway($con); $userGateway=new UserGateway($con);
$listeGateway=new ListeGateway($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); $tacheGateway->delete($t);
$user=$userGateway->insert(new User(1, "test", "mdp")); $user=$userGateway->insert(new User(1, "test", "mdp"));

@ -46,6 +46,10 @@ class Liste
return $this->taches; return $this->taches;
} }
public function setId(int $id){
$this->id=$id;
}
public function setName(string $name){ public function setName(string $name){
$this->name=$name; $this->name=$name;
} }

@ -30,6 +30,10 @@ class User
return $this->password; return $this->password;
} }
public function setId(int $id){
$this->id=$id;
}
public function setUsername(string $username){ public function setUsername(string $username){
$this->username=$username; $this->username=$username;
} }

@ -0,0 +1,77 @@
<?php
class MdlListe{
private $con;
public function __construct(Connection $con){
$this->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;
}
}
?>

@ -0,0 +1,37 @@
<?php
class MdlTache{
private $con;
public function __construct(Connection $con){
$this->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);
}
}
?>

@ -0,0 +1,58 @@
<?php
class MdlUser{
private $con;
public function __construct(Connection $con){
$this->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);
}
}
?>
Loading…
Cancel
Save