From 8ca5766a11daa8d60ebc36d4d652c65bcafa4e65 Mon Sep 17 00:00:00 2001 From: Lucie Bedouret Date: Thu, 3 Nov 2022 20:57:14 +0100 Subject: [PATCH] =?UTF-8?q?ADD=20:=20cr=C3=A9ation=20de=20la=20premi=C3=A8?= =?UTF-8?q?re=20requ=C3=AAte=20GET=20pour=20les=20users?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- api-rest/dbConnection.php | 57 +++++++++++++++++++++++++++++++++++++++ api-rest/models/User.php | 30 +++++++++++++++++++++ api-rest/users/read.php | 55 +++++++++++++++++++++++++++++++++++++ 3 files changed, 142 insertions(+) create mode 100644 api-rest/dbConnection.php create mode 100644 api-rest/models/User.php create mode 100644 api-rest/users/read.php diff --git a/api-rest/dbConnection.php b/api-rest/dbConnection.php new file mode 100644 index 0000000..fe9c710 --- /dev/null +++ b/api-rest/dbConnection.php @@ -0,0 +1,57 @@ +connect_error) { + echo 'Errno: '.$mysqli->connect_errno; + echo '
'; + echo 'Error: '.$mysqli->connect_error; + exit(); + } + + + echo '
'; + echo 'Host information: '.$mysqli->host_info; + echo '
'; + echo 'Protocol version: '.$mysqli->protocol_version; + + $mysqli->close(); + + try{ + $dbh = new PDO($db_dsn,$db_user,$db_password); + $dbh->exec("set names utf8"); + echo 'Success: A proper connection to MySQL was made.'; + }catch(PDOException $exception){ + echo "Connection error : " . $exception->getMessage(); + } + */ + class Database{ + public $connection; + + public function establishConnection(){ + $this->connection=null; + + try{ + $this->connection = new PDO("mysql:dbname=bobParty;host=127.0.0.1;port=8889", "root", "root"); + $this->connection->exec("set names utf8"); + }catch(PDOException $exception){ + echo "Connection error : " . $exception->getMessage(); + } + + return $this->connection; + } + } +?> \ No newline at end of file diff --git a/api-rest/models/User.php b/api-rest/models/User.php new file mode 100644 index 0000000..dbb45aa --- /dev/null +++ b/api-rest/models/User.php @@ -0,0 +1,30 @@ +connection=$db; + } + + public function read(){ + $sqlQuery= "SELECT U.id, U.username, U.password, U.nationality, U.sex, U.dateOfBirth, U.currentBobCoins, U.totalBobCoins, U.nbGamesPlayed FROM User U"; + $query = $this->connection->prepare($sqlQuery); + $query->execute(); + return $query; + } +} + +?> \ No newline at end of file diff --git a/api-rest/users/read.php b/api-rest/users/read.php new file mode 100644 index 0000000..ab964c6 --- /dev/null +++ b/api-rest/users/read.php @@ -0,0 +1,55 @@ + "Unauthorized method"]); +}else{ + include_once '../dbConnection.php'; + include_once '../models/User.php'; + + $db= new Database(); + $db= $db->establishConnection(); + + $user = new User($db); + + $stmt = $user->read(); + + if($stmt->rowCount() >= 0){ + + $arrayUser=[]; + $arrayUser['users']=[]; + + while($row = $stmt->fetch(PDO::FETCH_ASSOC)){ + extract($row); + + $user= [ + "id" => $id, + "username" => $username, + "password" => $password, + "nationality" => $nationality, + "sex" => $sex, + "dateofBirth" => $dateOfBirth, + "currentBobCoins"=>$currentBobCoins, + "totalBobCoins" => $totalBobCoins, + "nbGamesPlayed" => $nbGamesPlayed, + ]; + + $arrayUser['users'][]=$user; + } + + http_response_code(200); + + echo json_encode($arrayUser); + + } +} + +?> \ No newline at end of file