implémentation de la classe AbsGateway

php
Anthony RICHARD 1 year ago
parent 817425eae2
commit 8d2f02fe64

@ -0,0 +1,21 @@
<?php
namespace gateway;
use PDO;
use PDOException;
use config\Connection;
abstract class AbsGateway
{
protected Connection $con;
public function __construct(Connection $con) {
$this->con = $con;
}
public abstract function add(array $parameters): int;
public abstract function remove(array $id): void;
public abstract function findAll(): array;
public abstract function findById(int $id);
}

@ -2,16 +2,46 @@
namespace gateway;
class GroupGateway
use PDO;
use PDOException;
class GroupGateway extends AbsGateway
{
private Connection $con;
public function __construct(Connection $con){
$this->con = $con;
parent::__construct($con);
}
public function findAllGroup(){
public function add(array $parameters): int //require 4 elements
{
try{
$query = "INSERT INTO Group_ values(:id,:num,:year,:sec)";
$args = array(':id'=>array($parameters[0],PDO::PARAM_INT),
':num'=>array($parameters[1],PDO::PARAM_INT),
':year'=>array($parameters[2],PDO::PARAM_INT),
':sec'=>array($parameters[3],PDO::PARAM_STR));
$this->con->ExecuteQuery($query,$args);
return $this->con->lastInsertId();
}
catch (PDOException $e){
throw new Exception($e->getMessage());
}
}
public function remove(array $id): void
{
try{
$query = "DELETE FROM Group_ g WHERE g.id=:id ";
$args = array(':id'=>array($id,PDO::PARAM_INT));
$this->con->ExecuteQuery($query,$args);
}
catch (PDOException $e){
throw new Exception($e->getMessage());
}
}
public function findAll(): array
{
try{
$query = "SELECT * FROM Group_";
$this->con->ExecuteQuery($query);
@ -22,13 +52,17 @@ class GroupGateway
}
Return $tab_group;
}
catch(PDOException $e ){
error_log('PDOException: ' . $e->getMessage(), 3, 'error.log');
throw new Exception($e->getMessage());
}
}
public function findById(int $id)
{
// TODO: Implement findById() method.
}
public function findByNum(String $num){
public function findByNum(String $num): array{
try{
$query = "SELECT * FROM Group_ g WHERE g.num = :num";
@ -44,38 +78,8 @@ class GroupGateway
}
catch(PDOException $e ){
error_log('PDOException: ' . $e->getMessage(), 3, 'error.log');
}
}
public function addGroup(int $id, int $num, int $year,string $sector):void{
try{
$query = "INSERT INTO Group_ values(:id,:num,:year,:sec)";
$args = array(':id'=>array($id,PDO::PARAM_INT),
':num'=>array($num,PDO::PARAM_INT),
':year'=>array($year,PDO::PARAM_INT),
':sec'=>array($sector,PDO::PARAM_STR));
$this->con->ExecuteQuery($query,$args);
}
catch (\PDOException $e){
error_log('PDOException: ' . $e->getMessage(), 3, 'error.log');
}
}
public function delGroupById(int $id):void{
try{
$query = "DELETE FROM Group_ g WHERE g.id=:id ";
$args = array(':id'=>array($id,PDO::PARAM_INT));
$this->con->ExecuteQuery($query,$args);
throw new Exception($e->getMessage());
}
catch (\PDOException $e){
error_log('PDOException: ' . $e->getMessage(), 3, 'error.log');
}
}
public function ModifGroupbById(int $id, int $num, int $year ,String $sector):void{
@ -87,23 +91,8 @@ class GroupGateway
':sector'=>array($sector,PDO::PARAM_STR));
$this->con->ExecuteQuery($query,$args);
}
catch (\PDOException $e){
error_log('PDOException: ' . $e->getMessage(), 3, 'error.log');
catch (PDOException $e){
throw new Exception($e->getMessage());
}
}
}
/*
$con = new Connection('mysql:host=localhost;dbname=project','root','');
$g = new GroupGateway($con);
var_dump($g->findAllGroup());
var_dump($g->findByNum(1));
var_dump($g->addGroup(2,2,2,"b"));
var_dump($g->addGroup(1,1,1,"a"));
var_dump($g->findAllGroup());
var_dump($g->delGroupById(1));
var_dump($g->findAllGroup());
var_dump($g->ModifGroupbById(2,3,3,"c"));
*/
}

@ -2,38 +2,66 @@
namespace gateway;
use config\Connection;
use model\User;
use PDO;
use PDOException;
use model\User;
class UserGateway
class UserGateway extends AbsGateway
{
private Connection $con;
/**
* @param Connection $con
*/
public function __construct(Connection $con)
{
$this->con = $con;
parent::__construct($con);
}
private function getRoles(int $id): array {
public function add(array $parameters): int //require 9 elements
{
try {
$query = "SELECT r.name FROM Be b, Role_ r WHERE b.userID=:id AND b.roleID=r.id";
$query = "INSERT INTO User_ VALUES (NULL, :password, :email, :name, :surname, :nickname, :image, :extraTime, :group)";
$args = array(':password' => array($parameters[0], PDO::PARAM_STR),
':email' => array($parameters[1], PDO::PARAM_STR),
':name' => array($parameters[2], PDO::PARAM_STR),
':surname' => array($parameters[3], PDO::PARAM_STR),
':nickname' => array($parameters[4], PDO::PARAM_STR),
':image' => array($parameters[5], PDO::PARAM_STR),
':extraTime' => array($parameters[6], PDO::PARAM_BOOL),
':group' => array($parameters[7], PDO::PARAM_INT));
$this->con->executeQuery($query, $args);
$userID = $this->con->lastInsertId();
foreach ($parameters[8] as $role) {
$query = "INSERT INTO Be VALUES (:userID, :roleID)";
$args = array(':userID' => array($userID, PDO::PARAM_INT),
':roleID' => array($role, PDO::PARAM_INT));
$this->con->executeQuery($query, $args);
}
return $userID;
}
catch (PDOException $e){
throw new Exception($e->getMessage());
}
}
public function remove(array $id): void
{
try {
$query="DELETE FROM Vocabulary WHERE creator=:id";
$args = array(':id' => array($id, PDO::PARAM_INT));
$this->con->executeQuery($query, $args);
$results = $this->con->getResults();
$tab = array();
foreach ($results as $row) $tab[] = $row['name'];
return $tab;
$query="DELETE FROM Be WHERE userID=:id";
$this->con->executeQuery($query, $args);
$query="DELETE FROM User_ WHERE id=:id";
$this->con->executeQuery($query, $args);
}
catch (PDOException $e) {
catch(PDOException $e ){
throw new Exception($e->getMessage());
}
}
public function findAllUsers() : array{
public function findAll(): array
{
try {
$this->con->executeQuery("SELECT * FROM User_");
$results = $this->con->getResults();
@ -47,7 +75,8 @@ class UserGateway
}
}
public function findUserById(int $id) : User{
public function findById(int $id)
{
try {
$query = "SELECT * FROM User_ WHERE id=:id";
$args = array(':id' => array($id, PDO::PARAM_INT));
@ -60,39 +89,25 @@ class UserGateway
}
}
public function findUserByEmail(string $email) : User{
try {
$query = "SELECT * FROM User_ WHERE email=:email";
$args = array(':email' => array($email, PDO::PARAM_STR));
$this->con->executeQuery($query, $args);
$results = $this->con->getResults();
return new User($results[0]['id'], $results[0]['password'], $results[0]['email'], $results[0]['name'], $results[0]['surname'], $results[0]['nickname'], $results[0]['image'], $results[0]['extraTime'], $results[0]['groupID'], $this->getRoles($results[0]['id']));
}
catch(PDOException $e ){
throw new Exception($e->getMessage());
}
}
public function findUserByName(string $name) : array{
private function getRoles(int $id): array {
try {
$query = "SELECT * FROM User_ WHERE name=:name";
$args = array(':name' => array($name, PDO::PARAM_STR));
$query = "SELECT r.name FROM Be b, Role_ r WHERE b.userID=:id AND b.roleID=r.id";
$args = array(':id' => array($id, PDO::PARAM_INT));
$this->con->executeQuery($query, $args);
$results = $this->con->getResults();
$tab = array();
foreach ($results as $row)
$tab[] = new User($row['id'], $row['password'], $row['email'], $row['name'], $row['surname'], $row['nickname'], $row['image'], $row['extraTime'], $row['groupID'], $this->getRoles($row['id']));
foreach ($results as $row) $tab[] = $row['name'];
return $tab;
}
catch(PDOException $e ){
catch (PDOException $e) {
throw new Exception($e->getMessage());
}
}
public function findUserBySurname(string $surname) : array{
public function findUserByLoginPassword(string $login, string $password) : User{
try {
$query = "SELECT * FROM User_ WHERE surname=:surname";
$args = array(':surname' => array($surname, PDO::PARAM_STR));
$query = "SELECT * FROM User_ WHERE email=:email AND password=:password";
$args = array(':email' => array($login, PDO::PARAM_STR), ':password' => array($password, PDO::PARAM_STR));
$this->con->executeQuery($query, $args);
$results = $this->con->getResults();
return new User($results[0]['id'], $results[0]['password'], $results[0]['email'], $results[0]['name'], $results[0]['surname'], $results[0]['nickname'], $results[0]['image'], $results[0]['extraTime'], $results[0]['groupID'], $this->getRoles($results[0]['id']));
@ -102,10 +117,10 @@ class UserGateway
}
}
public function findUserByLoginPassword(string $login, string $password) : User{
public function findUserByEmail(string $email) : User{
try {
$query = "SELECT * FROM User_ WHERE email=:email AND password=:password";
$args = array(':email' => array($login, PDO::PARAM_STR), ':password' => array($password, PDO::PARAM_STR));
$query = "SELECT * FROM User_ WHERE email=:email";
$args = array(':email' => array($email, PDO::PARAM_STR));
$this->con->executeQuery($query, $args);
$results = $this->con->getResults();
return new User($results[0]['id'], $results[0]['password'], $results[0]['email'], $results[0]['name'], $results[0]['surname'], $results[0]['nickname'], $results[0]['image'], $results[0]['extraTime'], $results[0]['groupID'], $this->getRoles($results[0]['id']));
@ -115,10 +130,10 @@ class UserGateway
}
}
public function findUserByNickname(string $nickname) : array{
public function findUserByName(string $name) : array{
try {
$query = "SELECT * FROM User_ WHERE nickname=:nickname";
$args = array(':nickname' => array($nickname, PDO::PARAM_STR));
$query = "SELECT * FROM User_ WHERE name=:name";
$args = array(':name' => array($name, PDO::PARAM_STR));
$this->con->executeQuery($query, $args);
$results = $this->con->getResults();
$tab = array();
@ -131,40 +146,29 @@ class UserGateway
}
}
public function isAdmin(int $id) : bool {
public function findUserBySurname(string $surname) : array{
try {
$query = "SELECT * FROM Be WHERE userID=:id AND roleID=1";
$args = array(':id' => array($id, PDO::PARAM_INT));
$query = "SELECT * FROM User_ WHERE surname=:surname";
$args = array(':surname' => array($surname, PDO::PARAM_STR));
$this->con->executeQuery($query, $args);
return !empty($this->con->getResults());
$results = $this->con->getResults();
return new User($results[0]['id'], $results[0]['password'], $results[0]['email'], $results[0]['name'], $results[0]['surname'], $results[0]['nickname'], $results[0]['image'], $results[0]['extraTime'], $results[0]['groupID'], $this->getRoles($results[0]['id']));
}
catch(PDOException $e ){
throw new Exception($e->getMessage());
}
}
public function addUser(string $password, string $email, string $name, string $surname, string $nickname, string $image, bool $extraTime, int $group, array $roles): int {
public function findUserByNickname(string $nickname) : array{
try {
$query = "INSERT INTO User_ VALUES (NULL, :password, :email, :name, :surname, :nickname, :image, :extraTime, :group)";
$args = array(':password' => array($password, PDO::PARAM_STR),
':email' => array($email, PDO::PARAM_STR),
':name' => array($name, PDO::PARAM_STR),
':surname' => array($surname, PDO::PARAM_STR),
':nickname' => array($nickname, PDO::PARAM_STR),
':image' => array($image, PDO::PARAM_STR),
':extraTime' => array($extraTime, PDO::PARAM_BOOL),
':group' => array($group, PDO::PARAM_INT));
$query = "SELECT * FROM User_ WHERE nickname=:nickname";
$args = array(':nickname' => array($nickname, PDO::PARAM_STR));
$this->con->executeQuery($query, $args);
$userID = $this->con->lastInsertId();
foreach ($roles as $role) {
$query = "INSERT INTO Be VALUES (:userID, :roleID)";
$args = array(':userID' => array($userID, PDO::PARAM_INT),
':roleID' => array($role, PDO::PARAM_INT));
$this->con->executeQuery($query, $args);
}
return $userID;
$results = $this->con->getResults();
$tab = array();
foreach ($results as $row)
$tab[] = new User($row['id'], $row['password'], $row['email'], $row['name'], $row['surname'], $row['nickname'], $row['image'], $row['extraTime'], $row['groupID'], $this->getRoles($row['id']));
return $tab;
}
catch(PDOException $e ){
throw new Exception($e->getMessage());
@ -214,21 +218,4 @@ class UserGateway
throw new Exception($e->getMessage());
}
}
public function removeUser(int $id) {
try {
$query="DELETE FROM Vocabulary WHERE creator=:id";
$args = array(':id' => array($id, PDO::PARAM_INT));
$this->con->executeQuery($query, $args);
$query="DELETE FROM Be WHERE userID=:id";
$this->con->executeQuery($query, $args);
$query="DELETE FROM User_ WHERE id=:id";
$this->con->executeQuery($query, $args);
}
catch(PDOException $e ){
throw new Exception($e->getMessage());
}
}
}

@ -1,14 +1,45 @@
<?php
namespace gateway;
class VocabularyGateway
use PDO;
use PDOException;
class VocabularyGateway extends AbsGateway
{
private Connection $con;
public function __construct(Connection $con){
$this->con = $con;
parent::__construct($con);
}
public function add(array $parameters): int // require 4 elements
{
try{
$query = "INSERT INTO Vocabulary values(:id,:name,:img,:aut)";
$args = array(':id'=>array($parameters[0],PDO::PARAM_INT),
':name'=>array($parameters[1],PDO::PARAM_STR),
':img'=>array($parameters[2],PDO::PARAM_STR),
':aut'=>array($parameters[3],PDO::PARAM_INT));
$this->con->ExecuteQuery($query,$args);
return $this->con->lastInsertId();
}
catch (PDOException $e){
throw new Exception('problème pour ajouter une liste de vocabulaire');
}
}
public function remove(array $id): void
{
try{
$query = "DELETE FROM Vocabulary v WHERE v.id=:id ";
$args = array(':id'=>array($id,PDO::PARAM_INT));
$this->con->ExecuteQuery($query,$args);
}
catch (PDOException $e){
throw new Exception('problème pour supprimer les vocabulaires avec leur Id');
}
}
public function findAllVoc(){
public function findAll(): array
{
try{
$query = "SELECT * FROM Vocabulary";
@ -21,15 +52,17 @@ class VocabularyGateway
}
Return $tab_vocab;
}
catch(PDOException $e ){
throw new Exception('problème pour affichage de tous les vocabulaires');
}
}
public function findById(int $id)
{
// TODO: Implement findById() method.
}
public function findByName(String $name){
public function findByName(String $name): array {
try{
$query = "SELECT * FROM Vocabulary v WHERE v.name = :name";
@ -50,35 +83,6 @@ class VocabularyGateway
}
public function addVocab(int $id, String $name, String $img, ?int $aut):void{
try{
$query = "INSERT INTO Vocabulary values(:id,:name,:img,:aut)";
$args = array(':id'=>array($id,PDO::PARAM_INT),
':name'=>array($name,PDO::PARAM_STR),
':img'=>array($img,PDO::PARAM_STR),
':aut'=>array($aut,PDO::PARAM_INT));
$this->con->ExecuteQuery($query,$args);
}
catch (\PDOException $e){
throw new Exception('problème pour ajouter une liste de vocabulaire');
}
}
public function delVocabById(int $id):void{
try{
$query = "DELETE FROM Vocabulary v WHERE v.id=:id ";
$args = array(':id'=>array($id,PDO::PARAM_INT));
$this->con->ExecuteQuery($query,$args);
}
catch (\PDOException $e){
throw new Exception('problème pour supprimer les vocabulaires avec leur Id');
}
}
public function ModifVocabById(int $id, String $name,String $img,String $aut):void{
try{
$query = "UPDATE Vocabulary SET name=:name, image=:img, creator=:aut WHERE id=:id";
@ -88,36 +92,8 @@ class VocabularyGateway
':aut'=>array($aut,PDO::PARAM_INT));
$this->con->ExecuteQuery($query,$args);
}
catch (\PDOException $e){
catch (PDOException $e){
throw new Exception('problème pour modifier les vocabulaires');
}
}
}
/*
$con = new Connection('mysql:host=localhost;dbname=dbanrichard7','anrichard7','achanger');
$g = new VocabularyGateway($con);
var_dump($g->findByName('gogo'));
echo "<br> avant <br>";
$g->addVocab(3,"gogo","img",2);
$g->addVocab(4,"gogo","img",2);
$g->addVocab(5,"troto","img",2);
echo"apres <br>";
var_dump($g->findAllVoc());
//print_r($g->findByName('gogo'));
echo" <br> suppression normalement <br>";
$g->delVocabById(3);
var_dump($g->findByName('gogo'));
echo "<br> modifié normalement <br>";
$g->ModifVocabById(4,"changer","new_img",4);
var_dump($g->findByName('gogo'));
var_dump($g->findByName('changer'));
*/
}
Loading…
Cancel
Save