Add User Gateway
continuous-integration/drone/push Build is passing
Details
continuous-integration/drone/push Build is passing
Details
parent
5be89731ad
commit
8a2736e6ff
@ -0,0 +1,54 @@
|
||||
<?php
|
||||
class Connection extends PDO {
|
||||
|
||||
private $stmt;
|
||||
public function __construct(string $dsn) {
|
||||
// $dsn = "pgsql:host=$this->host;port=$this->port;dbname=$this->database;user=$this->username;password=$this->password";
|
||||
// This should be remove or set to just use a debug
|
||||
try {
|
||||
parent::__construct($dsn);
|
||||
echo "Successfully connected to the database";
|
||||
$this->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
|
||||
// should be a more accurate exception
|
||||
} catch (PDOException $e) {
|
||||
echo("Error connecting to the database: " . $e->getMessage());
|
||||
// do something ...
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/** * @param string $query
|
||||
* @param array $parameters *
|
||||
* @return bool Returns `true` on success, `false` otherwise
|
||||
*/
|
||||
public function executeQuery(string $query, array $parameters = []) : bool{
|
||||
|
||||
$this->stmt = parent::prepare($query);
|
||||
|
||||
foreach ($parameters as $name => $value) {
|
||||
$this->stmt->bindValue($name, $value[0], $value[1]);
|
||||
}
|
||||
|
||||
return $this->stmt->execute();
|
||||
|
||||
}
|
||||
public function executeWithErrorHandling(string $query,array $params = []) {
|
||||
try {
|
||||
$this->beginTransaction();
|
||||
$this->executeQuery($query,$params);
|
||||
$this->commit();
|
||||
return $this->getResults();
|
||||
} catch (PDOException $e) {
|
||||
$this->rollBack();
|
||||
throw new Exception('Unexpected error on database client: ' . $e->getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
public function getResults() : array {
|
||||
return $this->stmt->fetchall(PDO::FETCH_ASSOC);
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
?>
|
@ -0,0 +1,50 @@
|
||||
<?php
|
||||
class UserGateway {
|
||||
private $connection;
|
||||
|
||||
public function __construct(Connection $connection) {
|
||||
$this->connection = $connection;
|
||||
}
|
||||
|
||||
public function getAthlete() {
|
||||
$query = "SELECT * FROM Athlete";
|
||||
return $this->connection->executeWithErrorHandling($query);
|
||||
}
|
||||
|
||||
public function getCoach() {
|
||||
$query = "SELECT * FROM Coach";
|
||||
return $this->connection->executeWithErrorHandling($query);
|
||||
}
|
||||
|
||||
public function getAthleteById($userId) {
|
||||
$query = "SELECT * FROM Athlete WHERE idAthlete = :id";
|
||||
$params = [':id' => [$userId, PDO::PARAM_INT]];
|
||||
return $this->connection->executeWithErrorHandling($query, $params);
|
||||
}
|
||||
|
||||
public function getCoachById($userId) {
|
||||
$query = "SELECT * FROM Coach WHERE idCoach = :id";
|
||||
$params = [':id' => [$userId, PDO::PARAM_INT]];
|
||||
return $this->connection->executeWithErrorHandling($query, $params);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// Exemple d'utilisation
|
||||
//$dsn = "pgsql:host=localhost;port=5432;dbname=mydatabase;user=myuser;password=mypassword";
|
||||
//$connection = new Connection($dsn);
|
||||
//$gateway = new UserGateway($connection);
|
||||
|
||||
//$allAth = $gateway->getAthlete();
|
||||
//print_r($allAth);
|
||||
|
||||
//$allCoach = $gateway->getCoach();
|
||||
//print_r($allCoach);
|
||||
|
||||
//$singleAth = $gateway->getAthleteById(1);
|
||||
//print_r($singleAth);
|
||||
|
||||
//$singleCoach = $gateway->getCoachById(1);
|
||||
//print_r($singleCoach);
|
||||
|
||||
?>
|
Loading…
Reference in new issue