try catch dans UserGateway.php

php
Anthony RICHARD 2 years ago
parent 278e09c468
commit 879f7bf0f8

@ -17,98 +17,145 @@ class UserGateway
} }
public function findAllUsers() : array{ public function findAllUsers() : array{
$query = "SELECT * FROM User_"; try {
$this->con->executeQuery($query, array()); $query = "SELECT * FROM User_";
$results = $this->con->getResults(); $this->con->executeQuery($query, array());
$tab = array(); $results = $this->con->getResults();
foreach ($results as $row) $tab = array();
$tab[] = new User($row['id'], $row['password'], $row['email'], $row['name'], $row['surname'], $row['nickname'], $row['image'], $row['extraTime'], $row['groupID']); foreach ($results as $row)
return $tab; $tab[] = new User($row['id'], $row['password'], $row['email'], $row['name'], $row['surname'], $row['nickname'], $row['image'], $row['extraTime'], $row['groupID']);
return $tab;
}
catch(PDOException $e ){
error_log('PDOException: ' . $e->getMessage(), 3, 'error.log');
return array();
}
} }
public function findUserById(int $id) : array{ public function findUserById(int $id) : array{
$query = "SELECT * FROM User_ WHERE id=:id"; try {
$args = array(':id' => array($id, PDO::PARAM_INT)); $query = "SELECT * FROM User_ WHERE id=:id";
$this->con->executeQuery($query, $args); $args = array(':id' => array($id, PDO::PARAM_INT));
$results = $this->con->getResults(); $this->con->executeQuery($query, $args);
$tab = array(); $results = $this->con->getResults();
foreach ($results as $row) $tab = array();
$tab[] = new User($row['id'], $row['password'], $row['email'], $row['name'], $row['surname'], $row['nickname'], $row['image'], $row['extraTime'], $row['groupID']); foreach ($results as $row)
return $tab; $tab[] = new User($row['id'], $row['password'], $row['email'], $row['name'], $row['surname'], $row['nickname'], $row['image'], $row['extraTime'], $row['groupID']);
return $tab;
}
catch(PDOException $e ){
error_log('PDOException: ' . $e->getMessage(), 3, 'error.log');
return array();
}
} }
public function findUserByName(string $name) : array{ public function findUserByName(string $name) : array{
$query = "SELECT * FROM User_ WHERE name=:name"; try {
$args = array(':name' => array($name, PDO::PARAM_STR)); $query = "SELECT * FROM User_ WHERE name=:name";
$this->con->executeQuery($query, $args); $args = array(':name' => array($name, PDO::PARAM_STR));
$results = $this->con->getResults(); $this->con->executeQuery($query, $args);
$tab = array(); $results = $this->con->getResults();
foreach ($results as $row) $tab = array();
$tab[] = new User($row['id'], $row['password'], $row['email'], $row['name'], $row['surname'], $row['nickname'], $row['image'], $row['extraTime'], $row['groupID']); foreach ($results as $row)
return $tab; $tab[] = new User($row['id'], $row['password'], $row['email'], $row['name'], $row['surname'], $row['nickname'], $row['image'], $row['extraTime'], $row['groupID']);
return $tab;
}
catch(PDOException $e ){
error_log('PDOException: ' . $e->getMessage(), 3, 'error.log');
return array();
}
} }
public function findUserBySurname(string $surname) : array{ public function findUserBySurname(string $surname) : array{
$query = "SELECT * FROM User_ WHERE surname=:surname"; try {
$args = array(':surname' => array($surname, PDO::PARAM_STR)); $query = "SELECT * FROM User_ WHERE surname=:surname";
$this->con->executeQuery($query, $args); $args = array(':surname' => array($surname, PDO::PARAM_STR));
$results = $this->con->getResults(); $this->con->executeQuery($query, $args);
$tab = array(); $results = $this->con->getResults();
foreach ($results as $row) $tab = array();
$tab[] = new User($row['id'], $row['password'], $row['email'], $row['name'], $row['surname'], $row['nickname'], $row['image'], $row['extraTime'], $row['groupID']); foreach ($results as $row)
return $tab; $tab[] = new User($row['id'], $row['password'], $row['email'], $row['name'], $row['surname'], $row['nickname'], $row['image'], $row['extraTime'], $row['groupID']);
return $tab;
}
catch(PDOException $e ){
error_log('PDOException: ' . $e->getMessage(), 3, 'error.log');
return array();
}
} }
public function findUserByNickname(string $nickname) : array{ public function findUserByNickname(string $nickname) : array{
$query = "SELECT * FROM User_ WHERE nickname=:nickname"; try {
$args = array(':nickname' => array($nickname, PDO::PARAM_STR)); $query = "SELECT * FROM User_ WHERE nickname=:nickname";
$this->con->executeQuery($query, $args); $args = array(':nickname' => array($nickname, PDO::PARAM_STR));
$results = $this->con->getResults(); $this->con->executeQuery($query, $args);
$tab = array(); $results = $this->con->getResults();
foreach ($results as $row) $tab = array();
$tab[] = new User($row['id'], $row['password'], $row['email'], $row['name'], $row['surname'], $row['nickname'], $row['image'], $row['extraTime'], $row['groupID']); foreach ($results as $row)
return $tab; $tab[] = new User($row['id'], $row['password'], $row['email'], $row['name'], $row['surname'], $row['nickname'], $row['image'], $row['extraTime'], $row['groupID']);
return $tab;
}
catch(PDOException $e ){
error_log('PDOException: ' . $e->getMessage(), 3, 'error.log');
return array();
}
} }
public function isAdmin(int $id):bool { public function isAdmin(int $id) : bool {
$query = "SELECT * FROM Be WHERE userID=:id AND roleID=1"; try {
$args = array(':id' => array($id, PDO::PARAM_INT)); $query = "SELECT * FROM Be WHERE userID=:id AND roleID=1";
$this->con->executeQuery($query, $args); $args = array(':id' => array($id, PDO::PARAM_INT));
return !empty($this->con->getResults()); $this->con->executeQuery($query, $args);
return !empty($this->con->getResults());
}
catch(PDOException $e ){
error_log('PDOException: ' . $e->getMessage(), 3, 'error.log');
return false;
}
} }
public function addUser(string $password, string $email, string $name, string $surname, string $nickname, string $image, bool $extraTime, int $group, array $roles): int { public function addUser(string $password, string $email, string $name, string $surname, string $nickname, string $image, bool $extraTime, int $group, array $roles): int {
$query = "INSERT INTO User_ VALUES (NULL, :password, :email, :name, :surname, :nickname, :image, :extraTime, :group)"; try {
$args = array(':password' => array($password, PDO::PARAM_STR), $query = "INSERT INTO User_ VALUES (NULL, :password, :email, :name, :surname, :nickname, :image, :extraTime, :group)";
':email' => array($email, PDO::PARAM_STR), $args = array(':password' => array($password, PDO::PARAM_STR),
':name' => array($name, PDO::PARAM_STR), ':email' => array($email, PDO::PARAM_STR),
':surname' => array($surname, PDO::PARAM_STR), ':name' => array($name, PDO::PARAM_STR),
':nickname' => array($nickname, PDO::PARAM_STR), ':surname' => array($surname, PDO::PARAM_STR),
':image' => array($image, PDO::PARAM_STR), ':nickname' => array($nickname, PDO::PARAM_STR),
':extraTime' => array($extraTime, PDO::PARAM_BOOL), ':image' => array($image, PDO::PARAM_STR),
':group' => array($group, PDO::PARAM_INT)); ':extraTime' => array($extraTime, PDO::PARAM_BOOL),
$this->con->executeQuery($query, $args); ':group' => array($group, PDO::PARAM_INT));
$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); $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; return $userID;
}
catch(PDOException $e ){
error_log('PDOException: ' . $e->getMessage(), 3, 'error.log');
return 0;
}
} }
public function RemoveUser(int $id) { public function RemoveUser(int $id) {
$query="DELETE FROM Vocabulary WHERE creator=:id"; try {
$args = array(':id' => array($id, PDO::PARAM_INT)); $query="DELETE FROM Vocabulary WHERE creator=:id";
$this->con->executeQuery($query, $args); $args = array(':id' => array($id, PDO::PARAM_INT));
$this->con->executeQuery($query, $args);
$query="DELETE FROM Be WHERE userID=:id"; $query="DELETE FROM Be WHERE userID=:id";
$this->con->executeQuery($query, $args); $this->con->executeQuery($query, $args);
$query="DELETE FROM User_ WHERE id=:id"; $query="DELETE FROM User_ WHERE id=:id";
$this->con->executeQuery($query, $args); $this->con->executeQuery($query, $args);
}
catch(PDOException $e ){
error_log('PDOException: ' . $e->getMessage(), 3, 'error.log');
}
} }
} }
Loading…
Cancel
Save