diff --git a/api-rest/dbConnection.php b/api-rest/dbConnection.php
index fe9c710..615fc11 100644
--- a/api-rest/dbConnection.php
+++ b/api-rest/dbConnection.php
@@ -1,57 +1,24 @@
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;
+ class DatabaseConnection extends PDO{
+ private $stmt;
- 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();
+ public function __construct(string $dsn, string $username, string $password){
+ parent::__construct($dsn,$username,$password);
+ $this->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
+ }
+
+ public function execQuery(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->connection;
+ return $this->stmt->execute();
+ }
+
+ public function getRes():array{
+ return $this->stmt->fetchall();
}
}
+
?>
\ No newline at end of file
diff --git a/api-rest/gateways/userGateway.php b/api-rest/gateways/userGateway.php
new file mode 100644
index 0000000..f6190cb
--- /dev/null
+++ b/api-rest/gateways/userGateway.php
@@ -0,0 +1,90 @@
+connection=$con;
+ }
+
+ // execute get method to find all users in database
+ public function getUsers(){
+ $query= "SELECT * FROM User";
+ $this->connection->execQuery($query,[]);
+ $res=$this->connection->getRes();
+ foreach($res as $row){
+ $tabUser[] = new User ($row['id'],$row['username'],$row['password'],$row['nationality'],$row['sex'],$row['dateOfBirth'],$row['currentBobCoins'],$row['totalBobCoins'],$row['nbGamesPlayed']);
+ }
+ return $tabUser;
+ }
+
+ // execute get method to find one user by his id in database
+ public function getUserById(string $id):array{
+
+ $query= "SELECT * FROM User U WHERE id = :id ";
+ $arg= array('id'=> array($id,PDO::PARAM_STR));
+ $this->connection->execQuery($query,$arg);
+ $res=$this->connection->getRes();
+ foreach($res as $row){
+ $usr = new User ($row['id'],$row['username'],$row['password'],$row['nationality'],$row['sex'],$row['dateOfBirth'],$row['currentBobCoins'],$row['totalBobCoins'],$row['nbGamesPlayed']);
+ }
+ return $usr;
+
+ }
+
+ // execute get method to find one user by his username in database
+ public function getUserByUsername(string $username):?User{
+ $query= "SELECT * FROM User U WHERE username = :username ";
+ $arg = array('username'=>array($username,PDO::PARAM_STR));
+ $this->connection->execQuery($query,$arg);
+ $res=$this->connection->getRes();
+ foreach($res as $row){
+ $usr = new User ($row['id'],$row['username'],$row['password'],$row['nationality'],$row['sex'],$row['dateOfBirth'],$row['currentBobCoins'],$row['totalBobCoins'],$row['nbGamesPlayed']);
+ }
+ return $usr;
+ }
+
+ // execute get method to find one user by his username and password for his connection in database
+ public function getUserForConnection(string $username, string $password):?User{
+ $query= "SELECT * FROM User U WHERE username = :username AND password = :password";
+ $arg = array('username'=>array($username,PDO::PARAM_STR),'password'=>array($password,PDO::PARAM_STR));
+ $this->connection->execQuery($query,$arg);
+ $res=$this->connection->getRes();
+ foreach($res as $row){
+ $usr = new User ($row['id'],$row['username'],$row['password'],$row['nationality'],$row['sex'],$row['dateOfBirth'],$row['currentBobCoins'],$row['totalBobCoins'],$row['nbGamesPlayed']);
+ }
+ return $usr;
+ }
+
+ // execute put method to create a new user in database
+ public function postUser(User $u): void{
+ if ($u->currentBobCoins != 0 | $u->totalBobCoins != 0| $u->nbGamesPlayed !=0){
+ echo "new user, can't have any coin or games played";
+ return;
+ }
+ $query = "INSERT INTO User VALUES (:id, :username, :password, :nationality, :sex, :dateOfBirth, 0, 0, 0)";
+ $arg=array('id' => array($u->id, PDO::PARAM_STR), 'username' => array($u->username, PDO::PARAM_STR), 'password' => array($u->password, PDO::PARAM_STR),'nationality' => array($u->nationality, PDO::PARAM_STR), 'sex' => array($u->sex, PDO::PARAM_STR),'dateOfBirth' => array($u->dateOfBirth, PDO::PARAM_STR));
+ $this->connection->execQuery($query, $arg);
+ }
+
+ // executing put method to update an user (by his id) in database
+ public function putUser(User $u){
+ $query="UPDATE User SET username = :username, password=:password, sex=:sex, nationality=:nationality, currentBobCoins=:currentBobCoins, totalBobCoins=:totalBobCoins, nbGamesPlayed=:nbGamesPlayed WHERE id=:id";
+ $arg=array(':id' => array($u->id, PDO::PARAM_STR), ':username' => array($u->username, PDO::PARAM_STR), ':password' => array($u->password, PDO::PARAM_STR),':nationality' => array($u->nationality, PDO::PARAM_STR), ':sex' => array($u->sex, PDO::PARAM_STR),':currentBobCoins' => array($u->currentBobCoins, PDO::PARAM_INT),':totalBobCoins' => array($u->totalBobCoins, PDO::PARAM_INT), ':nbGamesPlayed' => array($u->nbGamesPlayed, PDO::PARAM_INT));
+ $this->connection->execQuery($query, $arg);
+ }
+
+ // exectuing delete method to delete an user in database
+ public function deleteUser(User $u): void{
+ $query = "DELETE from User WHERE id = :id";
+ $arg=array(':id' => array($u->id, PDO::PARAM_STR));
+ $this->connection->execQuery($query,$arg);
+ }
+}
+
+?>
+
+
diff --git a/api-rest/index.php b/api-rest/index.php
new file mode 100644
index 0000000..20f372b
--- /dev/null
+++ b/api-rest/index.php
@@ -0,0 +1,124 @@
+
+ deleteUser($usr);
+ $res=$usergw->getUsers();
+ echo json_encode($res);
+ // Managing request and routing
+
+ $requestMethod = $_SERVER['REQUEST_METHOD'];
+ $requestName = $_REQUEST['fname'];
+ if(empty($requestName)){
+ header("HTTP/1.0 400 Request Name Empty");
+ http_response_code(400);
+ }
+ else{
+ switch ($requestMethod){
+ case 'GET':
+ switch ($requestName){
+ case 'getUser':
+ if (!empty($_GET["id"])){
+ //read an user by his id
+ $id = intval($_GET["id"]);
+ $res=$usergw->getUserById($id);
+ }
+ elseif (!empty($_GET["username"])){
+ // read an user by his username
+ $username = intval($_GET["username"]);
+ $res=$usergw->getUserByUsername($username);
+ }
+ else{
+ // read all users
+ $res= $usergw->getUsers();
+ echo json_encode($res);
+ }
+ break;
+ case 'getMatch':
+
+ break;
+ case 'getMessage':
+
+ break;
+ case 'getConversation ':
+
+ break;
+ }
+ break;
+
+ case 'POST':
+ switch ($requestName){
+ case 'postUser':
+ // rcreate a new user
+ $res= $usergw->postUser();
+ echo json_encode($res);
+ break;
+ case 'postMatch':
+
+ break;
+ case 'postMessage':
+
+ break;
+ case 'postConversation ':
+
+ break;
+ }
+ break;
+
+ case 'PUT':
+ switch ($requestName){
+ case 'putUser':
+
+ break;
+ case 'putMatch':
+
+ break;
+ /* case 'putMessage':
+
+ break; */
+ case 'putConversation ':
+
+ break;
+ }
+ break;
+
+ case 'DELETE':
+ switch ($requestName){
+ case 'delUser':
+
+ break;
+ case 'delMatch':
+
+ break;
+ /* case 'delMessage':
+
+ break; */
+ case 'delConversation ':
+
+ break;
+ }
+ break;
+ default :
+ // Invalid request
+ header("HTTP/1.0 405 Request Name Empty");
+ http_response_code(405);
+ break;
+ }
+
+ }
+
+
+ ?>
\ No newline at end of file
diff --git a/api-rest/model/user.php b/api-rest/model/user.php
new file mode 100644
index 0000000..d939086
--- /dev/null
+++ b/api-rest/model/user.php
@@ -0,0 +1,31 @@
+id=$_id;
+ $this->username=$_username;
+ $this->password=$_password;
+ $this->nationality=$_nationality;
+ $this->sex=$_sex;
+ $this->dateOfBirth=$_dateOfBirth;
+ $this->currentBobCoins=$_currentBobCoins;
+ $this->totalBobCoins=$_totalBobCoins;
+ $this->nbGamesPlayed=$_nbGamesPlayed;
+
+ }
+
+}
+
+?>
\ No newline at end of file
diff --git a/api-rest/models/User.php b/api-rest/models/User.php
deleted file mode 100644
index 7eba19f..0000000
--- a/api-rest/models/User.php
+++ /dev/null
@@ -1,65 +0,0 @@
-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;
- }
-
- public function post(){
- $sqlQuery= "INSERT INTO User VALUES(\"" . $this->id . "\",\"" . $this->username . "\",\"" . $this->password . "\",\"" . $this->nationality . "\",\"" . $this->sex . "\",\"" . $this->dateOfBirth . "\",0,0,0)";
- $query = $this->connection->prepare($sqlQuery);
- $query->execute();
- return $query;
- }
-
- public function delete(){
- $sqlQuery = "DELETE FROM User WHERE username=\"" . $this->username . "\"";
- $query = $this->connection->prepare($sqlQuery);
- $query->execute();
- return $query;
- }
-
- public function readOneId(){
- $sqlQuery = "SELECT * FROM User WHERE id=\"" . $this->id . "\"";
- $query = $this->connection->prepare($sqlQuery);
- $query->execute();
- return $query;
- }
-
- public function readOneUsername(){
- $sqlQuery = "SELECT * FROM User WHERE username=\"" . $this->username . "\"";
- $query = $this->connection->prepare($sqlQuery);
- $query->execute();
- return $query;
- }
-
- public function put(){
- $sqlQuery = "UPDATE User SET username='" . $this->username . "', nationality='" . $this->nationality . "', sex='" . $this->sex . "', dateOfBirth='" . $this->dateOfBirth . "', currentBobCoins='" . $this->currentBobCoins . "', totalBobCoins='" . $this->totalBobCoins . "', nbGamesPlayed='" . $this->nbGamesPlayed . "' WHERE id =\"" . $this->id . "\"";
- $query=$this->connection->prepare($sqlQuery);
- $query->execute();
- return $query;
- }
-}
-
-?>
\ No newline at end of file
diff --git a/api-rest/users/delete.php b/api-rest/users/delete.php
deleted file mode 100644
index 395f6b2..0000000
--- a/api-rest/users/delete.php
+++ /dev/null
@@ -1,44 +0,0 @@
- "Unauthorized method"]);
-}else{
- include_once '../dbConnection.php';
- include_once '../models/User.php';
-
- $db= new Database();
- $db= $db->establishConnection();
-
- $user = new User($db);
- $user->id="4";
- $user->username="petitFilou";
- $user->password="blblbl";
- $user->nationality="Francaise";
- $user->sex="M";
- $user->dateOfBirth="2002-05-10";
-
-
- $stmt = $user->delete();
-
- if($stmt->rowCount() > 0){
-
- echo "User deleted successfully";
- http_response_code(200);
-
- }
- else{
- echo "The user can't be deleted because not found in database";
- http_response_code(200);
- }
-}
-
-?>
\ No newline at end of file
diff --git a/api-rest/users/post.php b/api-rest/users/post.php
deleted file mode 100644
index 8ff9f2b..0000000
--- a/api-rest/users/post.php
+++ /dev/null
@@ -1,40 +0,0 @@
- "Unauthorized method"]);
-}else{
- include_once '../dbConnection.php';
- include_once '../models/User.php';
-
- $db= new Database();
- $db= $db->establishConnection();
-
- $user = new User($db);
- $user->id="U0004";
- $user->username="petitFilou";
- $user->password="blblbl";
- $user->nationality="Francaise";
- $user->sex="M";
- $user->dateOfBirth="2002-05-10";
-
-
- $stmt = $user->post();
-
- if($stmt != false){
-
- echo "user created successfully :)";
- http_response_code(200);
-
- }
-}
-
-?>
\ No newline at end of file
diff --git a/api-rest/users/put.php b/api-rest/users/put.php
deleted file mode 100644
index 926a812..0000000
--- a/api-rest/users/put.php
+++ /dev/null
@@ -1,43 +0,0 @@
- "Unauthorized method"]);
-}else{
- include_once '../dbConnection.php';
- include_once '../models/User.php';
-
- $db= new Database();
- $db= $db->establishConnection();
-
- $user = new User($db);
- $user->id="U0004";
- $user->username="petitFilou";
- $user->password="blblbl";
- $user->nationality="Francaise";
- $user->sex="M";
- $user->dateOfBirth="2002-05-10";
- $user->currentBobCoins=10;
- $user->totalBobCoins=10;
- $user->nbGamesPlayed=1;
-
- $stmt = $user->put();
-
- if($stmt != false){
-
- http_response_code(200);
- echo "User updated successfully :)";
-
- }
-
-}
-
-?>
\ No newline at end of file
diff --git a/api-rest/users/read.php b/api-rest/users/read.php
deleted file mode 100644
index ab964c6..0000000
--- a/api-rest/users/read.php
+++ /dev/null
@@ -1,55 +0,0 @@
- "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
diff --git a/api-rest/users/readOneId.php b/api-rest/users/readOneId.php
deleted file mode 100644
index 0612ee6..0000000
--- a/api-rest/users/readOneId.php
+++ /dev/null
@@ -1,50 +0,0 @@
- "Unauthorized method"]);
-}else{
- include_once '../dbConnection.php';
- include_once '../models/User.php';
-
- $db= new Database();
- $db= $db->establishConnection();
-
- $user = new User($db);
- $user->id="U0004";
-
- $stmt = $user->readOneId();
-
- if($stmt != false){
-
- $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,
- ];
-
- http_response_code(200);
-
- echo json_encode($user);
-
- }
-}
-
-?>
\ No newline at end of file
diff --git a/api-rest/users/readOneUsername.php b/api-rest/users/readOneUsername.php
deleted file mode 100644
index b6a2bdd..0000000
--- a/api-rest/users/readOneUsername.php
+++ /dev/null
@@ -1,50 +0,0 @@
- "Unauthorized method"]);
-}else{
- include_once '../dbConnection.php';
- include_once '../models/User.php';
-
- $db= new Database();
- $db= $db->establishConnection();
-
- $user = new User($db);
- $user->username="lulu";
-
- $stmt = $user->readOneUsername();
-
- if($stmt != false){
-
- $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,
- ];
-
- http_response_code(200);
-
- echo json_encode($user);
-
- }
-}
-
-?>
\ No newline at end of file