From 8a2736e6ffd36d5416fe8beb72db16fd5e57003f Mon Sep 17 00:00:00 2001 From: "kevin.monteiro" Date: Fri, 17 Nov 2023 09:49:46 +0100 Subject: [PATCH] Add User Gateway --- Sources/src/data/core/database/Connexion.php | 54 +++++++++++++++++++ .../src/data/core/database/UserGateway.php | 50 +++++++++++++++++ 2 files changed, 104 insertions(+) create mode 100644 Sources/src/data/core/database/Connexion.php create mode 100644 Sources/src/data/core/database/UserGateway.php diff --git a/Sources/src/data/core/database/Connexion.php b/Sources/src/data/core/database/Connexion.php new file mode 100644 index 00000000..41141675 --- /dev/null +++ b/Sources/src/data/core/database/Connexion.php @@ -0,0 +1,54 @@ +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); + } + + + +} +?> \ No newline at end of file diff --git a/Sources/src/data/core/database/UserGateway.php b/Sources/src/data/core/database/UserGateway.php new file mode 100644 index 00000000..dc58f98c --- /dev/null +++ b/Sources/src/data/core/database/UserGateway.php @@ -0,0 +1,50 @@ +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); + +?>