From 8ca5766a11daa8d60ebc36d4d652c65bcafa4e65 Mon Sep 17 00:00:00 2001 From: Lucie Bedouret Date: Thu, 3 Nov 2022 20:57:14 +0100 Subject: [PATCH 01/14] =?UTF-8?q?ADD=20:=20cr=C3=A9ation=20de=20la=20premi?= =?UTF-8?q?=C3=A8re=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 From 8a7eb760e05e91d772a32f1e02135bd0f4157ef4 Mon Sep 17 00:00:00 2001 From: Lucie Bedouret Date: Fri, 4 Nov 2022 16:13:09 +0100 Subject: [PATCH 02/14] ADD : request POST PUT DELETE readOneId and readOneUsername for User --- api-rest/models/User.php | 35 +++++++++++++++++++++ api-rest/users/delete.php | 44 ++++++++++++++++++++++++++ api-rest/users/post.php | 40 ++++++++++++++++++++++++ api-rest/users/put.php | 43 +++++++++++++++++++++++++ api-rest/users/readOneId.php | 50 ++++++++++++++++++++++++++++++ api-rest/users/readOneUsername.php | 50 ++++++++++++++++++++++++++++++ 6 files changed, 262 insertions(+) create mode 100644 api-rest/users/delete.php create mode 100644 api-rest/users/post.php create mode 100644 api-rest/users/put.php create mode 100644 api-rest/users/readOneId.php create mode 100644 api-rest/users/readOneUsername.php diff --git a/api-rest/models/User.php b/api-rest/models/User.php index dbb45aa..7eba19f 100644 --- a/api-rest/models/User.php +++ b/api-rest/models/User.php @@ -25,6 +25,41 @@ class User{ $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 new file mode 100644 index 0000000..395f6b2 --- /dev/null +++ b/api-rest/users/delete.php @@ -0,0 +1,44 @@ + "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 new file mode 100644 index 0000000..8ff9f2b --- /dev/null +++ b/api-rest/users/post.php @@ -0,0 +1,40 @@ + "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 new file mode 100644 index 0000000..926a812 --- /dev/null +++ b/api-rest/users/put.php @@ -0,0 +1,43 @@ + "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/readOneId.php b/api-rest/users/readOneId.php new file mode 100644 index 0000000..0612ee6 --- /dev/null +++ b/api-rest/users/readOneId.php @@ -0,0 +1,50 @@ + "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 new file mode 100644 index 0000000..b6a2bdd --- /dev/null +++ b/api-rest/users/readOneUsername.php @@ -0,0 +1,50 @@ + "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 From 7291994ad08a7438929639e151876eca4671fdd0 Mon Sep 17 00:00:00 2001 From: Lucie Bedouret Date: Mon, 14 Nov 2022 17:22:50 +0100 Subject: [PATCH 03/14] =?UTF-8?q?UPDATE:=20code=20php=20remis=20=C3=A0=20z?= =?UTF-8?q?=C3=A9ro=20pour=20la=20partie=20user,=20routing=20toujours=20in?= =?UTF-8?q?complet=20mais=20les=20requetes=20marchent=20(test=C3=A9es=20et?= =?UTF-8?q?=20approuv=C3=A9es)=20:)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- api-rest/dbConnection.php | 67 ++++------------ api-rest/gateways/userGateway.php | 90 +++++++++++++++++++++ api-rest/index.php | 124 +++++++++++++++++++++++++++++ api-rest/model/user.php | 31 ++++++++ api-rest/models/User.php | 65 --------------- api-rest/users/delete.php | 44 ---------- api-rest/users/post.php | 40 ---------- api-rest/users/put.php | 43 ---------- api-rest/users/read.php | 55 ------------- api-rest/users/readOneId.php | 50 ------------ api-rest/users/readOneUsername.php | 50 ------------ 11 files changed, 262 insertions(+), 397 deletions(-) create mode 100644 api-rest/gateways/userGateway.php create mode 100644 api-rest/index.php create mode 100644 api-rest/model/user.php delete mode 100644 api-rest/models/User.php delete mode 100644 api-rest/users/delete.php delete mode 100644 api-rest/users/post.php delete mode 100644 api-rest/users/put.php delete mode 100644 api-rest/users/read.php delete mode 100644 api-rest/users/readOneId.php delete mode 100644 api-rest/users/readOneUsername.php 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 From e47e97e6e79b3b9bce96ce20ec2a99d9c09c2bc0 Mon Sep 17 00:00:00 2001 From: Lucie Bedouret Date: Fri, 18 Nov 2022 10:16:10 +0100 Subject: [PATCH 04/14] ADD : functions for the Match, end of the users databased sized --- api-rest/gateways/matchGateway.php | 59 ++++++++++++++++++++++++++++++ api-rest/gateways/userGateway.php | 1 - api-rest/index.php | 35 ++++++++++-------- api-rest/model/match.php | 22 +++++++++++ api-rest/model/user.php | 1 - 5 files changed, 101 insertions(+), 17 deletions(-) create mode 100644 api-rest/gateways/matchGateway.php create mode 100644 api-rest/model/match.php diff --git a/api-rest/gateways/matchGateway.php b/api-rest/gateways/matchGateway.php new file mode 100644 index 0000000..69c1f19 --- /dev/null +++ b/api-rest/gateways/matchGateway.php @@ -0,0 +1,59 @@ +connection=$con; + } + + // Fucntions executing SQL requests on database + /* + * get : trouver un match grâce à un id de joueur + * put : pour modifier le match + * post : créer un match dans la bd + * delete : supprimer un match dans la bd + */ + + // Function executing get method to find a match + public function getMatch(string $matchId){ + $query1="SELECT id, inGame, idGame FROM Matchs WHERE id = :id"; + $query2="SELECT idUser FROM InMatch WHERE idMatch=:id"; + $arg=array('id' => array($matchId, PDO::PARAM_STR)); + $this->connection->execQuery($query2, $arg); + $res=$this->connection->getRes(); + foreach($res as $row){ + $tabUser[] = $row['idUser']; + } + + $this->connection->execQuery($query1, $arg); + $res=$this->connection->getRes(); + foreach($res as $row){ + $match = new Matchs($row['id'],$row['inGame'],$row['idGame'],$tabUser); + } + return $match; + } + + // Function executing post method to create a match in database + public function postMatch(Matchs $m){ + $query1="INSERT INTO Matchs VALUES(:idMatch,0,:idGame)"; + $query2="INSERT INTO InMatch VALUES(:idMatch,:idUser)"; + $arg1=array('idMatch'=>array($m->id, PDO::PARAM_STR), + 'idGame'=>array($m->idGame, PDO::PARAM_STR)); + $this->connection->execQuery($query1,$arg1); + foreach($m->listIdUsers as $idUsr){ + $arg2=array('idMatch'=>array($m->id, PDO::PARAM_STR), + 'idUser'=>array($idUsr, PDO::PARAM_STR)); + $this->connection->execQuery($query2,$arg2); + } + return; + } + +} + +?> \ No newline at end of file diff --git a/api-rest/gateways/userGateway.php b/api-rest/gateways/userGateway.php index f6190cb..bd28fe6 100644 --- a/api-rest/gateways/userGateway.php +++ b/api-rest/gateways/userGateway.php @@ -32,7 +32,6 @@ class UserGateway{ $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 diff --git a/api-rest/index.php b/api-rest/index.php index 20f372b..b59a59f 100644 --- a/api-rest/index.php +++ b/api-rest/index.php @@ -1,7 +1,8 @@ deleteUser($usr); - $res=$usergw->getUsers(); + $res=$matchgw->getMatch("M0001"); + echo json_encode($res); - // Managing request and routing + // Managing request, routing and sending responses + /* $requestMethod = $_SERVER['REQUEST_METHOD']; $requestName = $_REQUEST['fname']; + + if(empty($requestName)){ header("HTTP/1.0 400 Request Name Empty"); http_response_code(400); @@ -62,9 +69,7 @@ case 'POST': switch ($requestName){ case 'postUser': - // rcreate a new user - $res= $usergw->postUser(); - echo json_encode($res); + // create a new user and add it in database break; case 'postMatch': @@ -86,9 +91,9 @@ case 'putMatch': break; - /* case 'putMessage': + case 'putMessage': - break; */ + break; case 'putConversation ': break; @@ -103,9 +108,9 @@ case 'delMatch': break; - /* case 'delMessage': + case 'delMessage': - break; */ + break; case 'delConversation ': break; @@ -119,6 +124,6 @@ } } - + */ ?> \ No newline at end of file diff --git a/api-rest/model/match.php b/api-rest/model/match.php new file mode 100644 index 0000000..b8921f7 --- /dev/null +++ b/api-rest/model/match.php @@ -0,0 +1,22 @@ +id=$_id; + $this->inGame=$_inGame; + $this->idGame=$_idGame; + // Only one user at the moment of the creation + $this->listIdUsers=$_listIdUsers; + } +} + +?> \ No newline at end of file diff --git a/api-rest/model/user.php b/api-rest/model/user.php index d939086..92f83be 100644 --- a/api-rest/model/user.php +++ b/api-rest/model/user.php @@ -23,7 +23,6 @@ class User { $this->currentBobCoins=$_currentBobCoins; $this->totalBobCoins=$_totalBobCoins; $this->nbGamesPlayed=$_nbGamesPlayed; - } } From eec7c3aabd85a0e062426620bbf4d9c99e87a07e Mon Sep 17 00:00:00 2001 From: Lucie Bedouret Date: Fri, 18 Nov 2022 11:33:53 +0100 Subject: [PATCH 05/14] CONTINUE : documentation and all CRUD methods to manage Matches databased sided --- api-rest/gateways/matchGateway.php | 52 ++++++++++++++++++++++++------ api-rest/gateways/userGateway.php | 46 ++++++++++++++++++++------ api-rest/index.php | 6 ---- 3 files changed, 79 insertions(+), 25 deletions(-) diff --git a/api-rest/gateways/matchGateway.php b/api-rest/gateways/matchGateway.php index 69c1f19..462fba3 100644 --- a/api-rest/gateways/matchGateway.php +++ b/api-rest/gateways/matchGateway.php @@ -12,16 +12,19 @@ class MatchGateway{ $this->connection=$con; } - // Fucntions executing SQL requests on database - /* - * get : trouver un match grâce à un id de joueur - * put : pour modifier le match - * post : créer un match dans la bd - * delete : supprimer un match dans la bd + /* Functions implemented to manage matches' data from database + + * getMatch : returning a match found in database with his id + * postMatch : adding a NEW user in database + * putMatch : modifying an EXISTING user in database + * deleteMatch : deleting an user from database + */ - // Function executing get method to find a match - public function getMatch(string $matchId){ +/// Brief : Returning a match found in database with his id +/// Parameters : * $id (string): identifier of the match we are looking for + public function getMatch(string $matchId):?Matchs{ + $match=NULL; $query1="SELECT id, inGame, idGame FROM Matchs WHERE id = :id"; $query2="SELECT idUser FROM InMatch WHERE idMatch=:id"; $arg=array('id' => array($matchId, PDO::PARAM_STR)); @@ -39,7 +42,8 @@ class MatchGateway{ return $match; } - // Function executing post method to create a match in database +/// Brief : Adding a NEW match in database +/// Parameters : * $u (Matchs): match we want to insert in database public function postMatch(Matchs $m){ $query1="INSERT INTO Matchs VALUES(:idMatch,0,:idGame)"; $query2="INSERT INTO InMatch VALUES(:idMatch,:idUser)"; @@ -54,6 +58,36 @@ class MatchGateway{ return; } +/// Brief : Modifying an EXISTING match in database +/// Parameters : * $u (Matchs): match we want to update in database + public function putMatch(Matchs $m){ + $query1="UPDATE Matchs SET inGame= :inGame WHERE id=:id"; + //Peut-etre la possibilité de faire mieux??? + $query2="DELETE FROM InMatch WHERE idMatch=:idMatch"; + $query3="INSERT INTO InMatch VALUES(:idMatch,:idUser)"; + $arg1=array('inGame'=>array($m->inGame, PDO::PARAM_BOOL), + 'id'=>array($m->id,PDO::PARAM_STR)); + $arg2=array('idMatch'=>array($m->id,PDO::PARAM_STR)); + $this->connection->execQuery($query1,$arg1); + $this->connection->execQuery($query2,$arg2); + foreach($m->listIdUsers as $idUsr){ + $arg3=array('idMatch'=>array($m->id, PDO::PARAM_STR), + 'idUser'=>array($idUsr,PDO::PARAM_STR)); + $this->connection->execQuery($query3,$arg3); + } + return; + } + +/// Brief : Deleting a match from database +/// Parameters : * $u (Matchs): match we want to delete from database + public function deleteMatch(Matchs $m){ + $query1="DELETE FROM InMatch WHERE idMatch=:id"; + $query2="DELETE FROM Matchs WHERE id=:id"; + $arg=array('id'=>array($m->id, PDO::PARAM_STR)); + $this->connection->execQuery($query1,$arg); + $this->connection->execQuery($query2,$arg); + } + } ?> \ No newline at end of file diff --git a/api-rest/gateways/userGateway.php b/api-rest/gateways/userGateway.php index bd28fe6..4ef1d2d 100644 --- a/api-rest/gateways/userGateway.php +++ b/api-rest/gateways/userGateway.php @@ -10,20 +10,36 @@ class UserGateway{ $this->connection=$con; } - // execute get method to find all users in database - public function getUsers(){ + /* Functions implemented to manage user's data from database + + * getUsers : returning an array of users containing all the user stored in database + * getUserById : returning an user found in database with his id + * getUserByUsername : returning an user found in database with his username + * getUserForConnection : returning an user if there is a correspondance between + the username and the password, used for connection + * postUser : adding a NEW user in database + * putUser : modifying an EXISTING user in database + * deleteUser : deleting an user from database + + */ + +/// Brief : Returning an array of users containing all the user stored in database + public function getUsers():array{ + $tabUser=NULL; $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{ - +/// Brief : Returning an user found in database with his id +/// Parameters : * $id (string): identifier of the user we are looking for + public function getUserById(string $id):?User{ + $usr=NULL; $query= "SELECT * FROM User U WHERE id = :id "; $arg= array('id'=> array($id,PDO::PARAM_STR)); $this->connection->execQuery($query,$arg); @@ -34,8 +50,10 @@ class UserGateway{ return $usr; } - // execute get method to find one user by his username in database +/// Brief : Returning an user found in database with his username +/// Parameters : * $username (string): username of the user we are looking for public function getUserByUsername(string $username):?User{ + $usr=NULL; $query= "SELECT * FROM User U WHERE username = :username "; $arg = array('username'=>array($username,PDO::PARAM_STR)); $this->connection->execQuery($query,$arg); @@ -46,8 +64,13 @@ class UserGateway{ return $usr; } - // execute get method to find one user by his username and password for his connection in database +/// Brief : Returning an user if there is a correspondance between the username and the password, used for connection +/// Parameters : * $username (string): username of the user we are looking for +/// * $password (string): password of the user we are looking for +/// Comment : this function returns an user if it finds a match between an username and password, +/// if it doesn't, it means there are no corresponding user public function getUserForConnection(string $username, string $password):?User{ + $usr=NULL; $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); @@ -58,7 +81,8 @@ class UserGateway{ return $usr; } - // execute put method to create a new user in database +/// Brief : Adding a NEW user in database +/// Parameters : * $u (User): user we want to insert 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"; @@ -69,14 +93,16 @@ class UserGateway{ $this->connection->execQuery($query, $arg); } - // executing put method to update an user (by his id) in database +/// Brief : Modifying an EXISTING user in database +/// Parameters : * $u (User): user we want to update 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 +/// Brief : Deleting an user from database +/// Parameters : * $u (User): user we want to delete from database public function deleteUser(User $u): void{ $query = "DELETE from User WHERE id = :id"; $arg=array(':id' => array($u->id, PDO::PARAM_STR)); diff --git a/api-rest/index.php b/api-rest/index.php index b59a59f..4d8c9b0 100644 --- a/api-rest/index.php +++ b/api-rest/index.php @@ -16,12 +16,6 @@ // Initializing Gateways $usergw = new UserGateway($database); $matchgw = new MatchGateway($database); - - // Testing get method on matches - - $res=$matchgw->getMatch("M0001"); - - echo json_encode($res); // Managing request, routing and sending responses /* From 981cc3e957093c9435e50a522feb650851262f21 Mon Sep 17 00:00:00 2001 From: Lucie Bedouret Date: Sat, 19 Nov 2022 11:32:19 +0100 Subject: [PATCH 06/14] MODIFY : user's methods database sided completed --- api-rest/gateways/userGateway.php | 69 +++++++++++++++++++++++++++---- api-rest/index.php | 6 ++- api-rest/model/user.php | 6 ++- 3 files changed, 70 insertions(+), 11 deletions(-) diff --git a/api-rest/gateways/userGateway.php b/api-rest/gateways/userGateway.php index 4ef1d2d..9226c83 100644 --- a/api-rest/gateways/userGateway.php +++ b/api-rest/gateways/userGateway.php @@ -23,29 +23,38 @@ class UserGateway{ */ +/* /// Brief : Returning an array of users containing all the user stored in database public function getUsers():array{ $tabUser=NULL; - $query= "SELECT * FROM User"; + $query1= "SELECT * FROM User"; + $query2="SELECT idSkin FROM HasSkin WHERE idUser=:idUser"; $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']); + $tabUser[] = new User ($row['id'],$row['username'],$row['password'],$row['nationality'],$row['sex'],$row['dateOfBirth'],$row['currentBobCoins'],$row['totalBobCoins'],$row['nbGamesPlayed'],$row['currentIdSkin']); } return $tabUser; } +*/ /// Brief : Returning an user found in database with his id /// Parameters : * $id (string): identifier of the user we are looking for public function getUserById(string $id):?User{ $usr=NULL; $query= "SELECT * FROM User U WHERE id = :id "; + $query2="SELECT idSkin FROM HasSkin WHERE idUser=:id"; $arg= array('id'=> array($id,PDO::PARAM_STR)); + $this->connection->execQuery($query2,$arg); + $res=$this->connection->getRes(); + foreach($res as $row){ + $tabSkin[]=$row['idSkin']; + } $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']); + $usr = new User ($row['id'],$row['username'],$row['password'],$row['nationality'],$row['sex'],$row['dateOfBirth'],$row['currentBobCoins'],$row['totalBobCoins'],$row['nbGamesPlayed'],$row['currentIdSkin'],$tabSkin); } return $usr; } @@ -54,13 +63,22 @@ class UserGateway{ /// Parameters : * $username (string): username of the user we are looking for public function getUserByUsername(string $username):?User{ $usr=NULL; + $query= "SELECT * FROM User U WHERE username = :username "; + $query2="SELECT idSkin FROM HasSkin WHERE idUser=:id"; $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']); + $usr = new User ($row['id'],$row['username'],$row['password'],$row['nationality'],$row['sex'],$row['dateOfBirth'],$row['currentBobCoins'],$row['totalBobCoins'],$row['nbGamesPlayed'],$row['currentIdSkin'],null); } + $arg2=array('id'=>array($usr->id, PDO::PARAM_STR)); + $this->connection->execQuery($query2,$arg2); + $res=$this->connection->getRes(); + foreach($res as $row){ + $tabSkin[]=$row['idSkin']; + } + $usr->listIdSkin=$tabSkin; return $usr; } @@ -72,12 +90,20 @@ class UserGateway{ public function getUserForConnection(string $username, string $password):?User{ $usr=NULL; $query= "SELECT * FROM User U WHERE username = :username AND password = :password"; + $query2="SELECT idSkin FROM HasSkin WHERE idUser=:id"; $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']); + $usr = new User ($row['id'],$row['username'],$row['password'],$row['nationality'],$row['sex'],$row['dateOfBirth'],$row['currentBobCoins'],$row['totalBobCoins'],$row['nbGamesPlayed'],$row['currentIdSkin'],null); } + $arg2=array('id'=>array($usr->id, PDO::PARAM_STR)); + $this->connection->execQuery($query2,$arg2); + $res=$this->connection->getRes(); + foreach($res as $row){ + $tabSkin[]=$row['idSkin']; + } + $usr->listIdSkin=$tabSkin; return $usr; } @@ -88,17 +114,42 @@ class UserGateway{ 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)); + $query = "INSERT INTO User VALUES (:id, :username, :password, :nationality, :sex, :dateOfBirth, 0, 0, 0, 'S0001')"; + $query2 = "INSERT INTO HasSkin VALUES(:id,'S0001')"; + $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)); + $arg2=array('id' => array($u->id, PDO::PARAM_STR)); $this->connection->execQuery($query, $arg); + $this->connection->execQuery($query2,$arg2); } /// Brief : Modifying an EXISTING user in database /// Parameters : * $u (User): user we want to update 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)); + $query="UPDATE User SET username = :username, password=:password, sex=:sex, nationality=:nationality, currentBobCoins=:currentBobCoins, totalBobCoins=:totalBobCoins, nbGamesPlayed=:nbGamesPlayed, currentIdSkin=:currentIdSkin WHERE id=:id"; + $query2="DELETE FROM HasSkin WHERE idUser=:id"; + $query3="INSERT INTO HasSkin VALUES(:idUsr,:idSkin)"; + $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), + ':currentIdSkin'=> array($u->currentIdSkin, PDO::PARAM_STR)); + $arg2=array('id'=>array($u->id,PDO::PARAM_STR)); $this->connection->execQuery($query, $arg); + $this->connection->execQuery($query2,$arg2); + foreach($u->listIdSkin as $idSkin){ + $arg3=array('idUsr'=>array($u->id,PDO::PARAM_STR), + 'idSkin'=>array($idSkin,PDO::PARAM_STR)); + $this->connection->execQuery($query3,$arg3); + } } /// Brief : Deleting an user from database diff --git a/api-rest/index.php b/api-rest/index.php index 4d8c9b0..cfd3aa4 100644 --- a/api-rest/index.php +++ b/api-rest/index.php @@ -1,5 +1,9 @@ + id=$_id; $this->username=$_username; $this->password=$_password; @@ -23,6 +25,8 @@ class User { $this->currentBobCoins=$_currentBobCoins; $this->totalBobCoins=$_totalBobCoins; $this->nbGamesPlayed=$_nbGamesPlayed; + $this->currentIdSkin=$_currentIdSkin; + $this->listIdSkin=$_listIdSkin; } } From fb0d6ec69a0356fd93be777a64605fe1c2dac43e Mon Sep 17 00:00:00 2001 From: Lucie Bedouret Date: Mon, 21 Nov 2022 17:12:09 +0100 Subject: [PATCH 07/14] ADD : methodes pour les conversations et les messages --- api-rest/gateways/conversationGataway.php | 169 ++++++++++++++++++++++ api-rest/gateways/matchGateway.php | 5 +- api-rest/gateways/userGateway.php | 28 ++-- api-rest/index.php | 13 +- api-rest/model/conversation.php | 19 +++ api-rest/model/match.php | 2 +- api-rest/model/message.php | 17 +++ 7 files changed, 230 insertions(+), 23 deletions(-) create mode 100644 api-rest/gateways/conversationGataway.php create mode 100644 api-rest/model/conversation.php create mode 100644 api-rest/model/message.php diff --git a/api-rest/gateways/conversationGataway.php b/api-rest/gateways/conversationGataway.php new file mode 100644 index 0000000..c49e74c --- /dev/null +++ b/api-rest/gateways/conversationGataway.php @@ -0,0 +1,169 @@ +connection=$_connection; + } + + /* Functions implemented to manage conversations' data from database + + * getConversations : returning all the ids of the conversations of an user + (with all the id of the messages and the users in the conversation) + * postConversation : adding a NEW conversation in database + * putMatch : modifying an EXISTING conversation in database + * deleteMatch : deleting an conversation from database + + */ + + +/// Brief : Returning all the ids of the conversations where an user belongs + ///(with all the id of the messages and the users in the conversation) +/// Parameters : * $idUser (string): identifier of the user we want to get the conversations + public function getConversations(string $_idUser):?array{ + $tabIdConversation=NULL; + $tabConversations=NULL; + $tabUsers=NULL; + $tabIdMessages=NULL; + $tabMessages=NULL; + + $query1 = "SELECT idConversation FROM InConversation WHERE idUser=:idUser"; + $query2 = "SELECT idUser FROM InConversation WHERE idConversation=:idConv"; + $query3 = "SELECT idMessage FROM Have WHERE idConversation=:idConv"; + $query4 = "SELECT id, message, idSender FROM Message WHERE id=:id"; + $query5 = "SELECT id, nom FROM Conversation WHERE id=:idConv"; + + $arg1=array('idUser'=>array($_idUser, PDO::PARAM_STR)); + + $this->connection->execQuery($query1,$arg1); + $res=$this->connection->getRes(); + foreach($res as $row){ + $tabIdConversation[] = $row['idConversation']; + } + + foreach($tabIdConversation as $idConv){ + + $arg2 = array('idConv'=>array($idConv, PDO::PARAM_STR)); + $this->connection->execQuery($query2,$arg2); + $res=$this->connection->getRes(); + foreach($res as $row){ + $tabUsers[] = $row['idUser']; + } + + $this->connection->execQuery($query3,$arg2); + $res=$this->connection->getRes(); + foreach($res as $row){ + $tabIdMessages[] = $row['idMessage']; + } + + foreach($tabIdMessages as $idMessage){ + $arg3=array('id'=>array($idMessage,PDO::PARAM_STR)); + $this->connection->execQuery($query4,$arg3); + $res=$this->connection->getRes(); + foreach($res as $row){ + $tabMessages[] = new Message($row['id'],$row['message'],$row['idSender']); + } + } + + $this->connection->execQuery($query5,$arg2); + $res=$this->connection->getRes(); + foreach($res as $row){ + $tabConversations[]= new Conversation($row['id'], $row['nom'],$tabMessages,$tabUsers); + } + + $tabUsers=array(); + $tabIdMessages=array(); + $tabMessages=array(); + } + return $tabConversations; + } + +/// Brief : Adding a new conversation in database +/// Parameters : * $c (Conversation): conversation we want to insert in database + public function postConversation(Conversation $c): void{ + $query1 = "INSERT INTO Conversation VALUES(:idConv,:name)"; + $query2 = "INSERT INTO InConversation VALUES(:idUser,:idConv)"; + + $arg1 = array('idConv'=>array($c->id,PDO::PARAM_STR), + 'name'=>array($c->name, PDO::PARAM_STR)); + + $this->connection->execQuery($query1,$arg1); + + foreach($c->listIdUsers as $idUsr){ + $arg2 = array('idUser'=>array($idUsr, PDO::PARAM_STR), + 'idConv'=>array($c->id, PDO::PARAM_STR)); + $this->connection->execQuery($query2,$arg2); + } + } + +/// Brief : Modifying an EXISTING match in database +/// Parameters : * $u (Matchs): match we want to update in database + public function putConversation(Conversation $c):void{ + $query7 = "SELECT idMessage FROM Have WHERE idConversation=:idConv"; + $query8 = "DELETE FROM Message WHERE id = :id"; + $query1 = "DELETE FROM Have WHERE idConversation = :idConv"; + $query2 = "DELETE FROM InConversation WHERE idConversation = :idConv"; + $query3 = "UPDATE Conversation SET nom=:nom WHERE id=:id"; + $query4 = "INSERT INTO Have VALUES (:idConv,:idMessage)"; + $query5 = "INSERT INTO Message VALUES(:id,:message,:idSender)"; + $query6 = "INSERT INTO InConversation VALUES(:idUsr,:idConv)"; + + + $arg1 = array('idConv'=>array($c->id,PDO::PARAM_STR)); + $arg2 = array('nom'=>array($c->name, PDO::PARAM_STR), + 'id'=>array($c->id,PDO::PARAM_STR)); + + $this->connection->execQuery($query7,$arg1); + $res = $this->connection->getRes(); + foreach($res as $idMsg){ + $arg6 = array('id'=>array($idMsg['idMessage'],PDO::PARAM_STR)); + $this->connection->execQuery($query8,$arg6); + } + + $this->connection->execQuery($query1,$arg1); + $this->connection->execQuery($query2, $arg1); + $this->connection->execQuery($query3,$arg2); + + foreach($c->listMessages as $msg){ + $arg3 = array('idConv'=>array($c->id,PDO::PARAM_STR), + 'idMessage'=>array($msg->id,PDO::PARAM_STR)); + $arg4 = array('id'=>array($msg->id,PDO::PARAM_STR), + 'message'=>array($msg->message,PDO::PARAM_STR), + 'idSender'=>array($msg->idSender,PDO::PARAM_STR)); + $this->connection->execQuery($query4,$arg3); + $this->connection->execQuery($query5,$arg4); + } + + foreach($c->listIdUsers as $idUsr){ + $arg5 = array('idUsr'=>array($idUsr,PDO::PARAM_STR), + 'idConv'=>array($c->id,PDO::PARAM_STR)); + $this->connection->execQuery($query6,$arg5); + } + } + +/// Brief : Deleting a conversation and its messages from database +/// Parameters : * $c (Conversation): conversation we want to delete from database + public function deleteConversation(Conversation $c):void{ + $query1 = "DELETE FROM Message WHERE id=:id"; + $query2 = "DELETE FROM Have WHERE idConversation = :idConv"; + $query3 = "DELETE FROM InConversation WHERE idConversation = :idConv"; + $query4 = "DELETE FROM Conversation WHERE id = :idConv"; + + foreach($c->listMessages as $msg){ + $arg1 = array('id'=>array($msg->id,PDO::PARAM_STR)); + $this->connection->execQuery($query1,$arg1); + } + $arg2 = array('idConv'=>array($c->id,PDO::PARAM_STR)); + $this->connection->execQuery($query2,$arg2); + $this->connection->execQuery($query3,$arg2); + $this->connection->execQuery($query4,$arg2); + } +} + +?> \ No newline at end of file diff --git a/api-rest/gateways/matchGateway.php b/api-rest/gateways/matchGateway.php index 462fba3..638b3b0 100644 --- a/api-rest/gateways/matchGateway.php +++ b/api-rest/gateways/matchGateway.php @@ -1,10 +1,9 @@ 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'],$row['currentIdSkin']); - } - - return $tabUser; - } -*/ - /// Brief : Returning an user found in database with his id /// Parameters : * $id (string): identifier of the user we are looking for public function getUserById(string $id):?User{ @@ -107,6 +92,17 @@ class UserGateway{ return $usr; } +/// Brief : Returning the last Id of the users + public function getLastId():string{ + $query = "SELECT id FROM User WHERE id >= ALL (SELECT max(id) FROM User)"; + $this->connection->execQuery($query,[]); + $res=$this->connection->getRes(); + foreach($res as $row){ + $lastId=$row['id']; + } + return $lastId; + } + /// Brief : Adding a NEW user in database /// Parameters : * $u (User): user we want to insert in database public function postUser(User $u): void{ diff --git a/api-rest/index.php b/api-rest/index.php index cfd3aa4..7e1fe64 100644 --- a/api-rest/index.php +++ b/api-rest/index.php @@ -7,6 +7,7 @@ include ('dbConnection.php'); include ('gateways/userGateway.php'); include ('gateways/matchGateway.php'); + include ('gateways/conversationGataway.php'); // Connection to database // A changer quand la base de données sera hébergée, comment masquer les var? @@ -20,6 +21,14 @@ // Initializing Gateways $usergw = new UserGateway($database); $matchgw = new MatchGateway($database); + $conversationgw = new ConversationGateway($database); + + // Testing conversation/messages methods + $listUser=array("U0001","U0004","U0005"); + $listMessages= array(new Message("ME005","coucou","U0001"), new Message("ME006","ca va","U0004"), new Message("ME007","bien et toi?","U0001")); + $newConv = new Conversation("C0006","yémen",$listMessages,$listUser); + $conversationgw->deleteConversation($newConv); + // Managing request, routing and sending responses /* @@ -47,9 +56,7 @@ $res=$usergw->getUserByUsername($username); } else{ - // read all users - $res= $usergw->getUsers(); - echo json_encode($res); + } break; case 'getMatch': diff --git a/api-rest/model/conversation.php b/api-rest/model/conversation.php new file mode 100644 index 0000000..5fc5018 --- /dev/null +++ b/api-rest/model/conversation.php @@ -0,0 +1,19 @@ +id=$_id; + $this->name=$_name; + $this->listMessages=$_listMessages; + $this->listIdUsers=$_listIdUsers; + } +} + +?> \ No newline at end of file diff --git a/api-rest/model/match.php b/api-rest/model/match.php index b8921f7..1853209 100644 --- a/api-rest/model/match.php +++ b/api-rest/model/match.php @@ -14,7 +14,7 @@ class Matchs{ $this->id=$_id; $this->inGame=$_inGame; $this->idGame=$_idGame; - // Only one user at the moment of the creation + // Only one user at creation $this->listIdUsers=$_listIdUsers; } } diff --git a/api-rest/model/message.php b/api-rest/model/message.php new file mode 100644 index 0000000..c36dfa0 --- /dev/null +++ b/api-rest/model/message.php @@ -0,0 +1,17 @@ +id=$_id; + $this->message=$_message; + $this->idSender=$_idSender; + } +} + +?> \ No newline at end of file From 5c0ad12fd991d8fca156a6a12448c0819ad14f7d Mon Sep 17 00:00:00 2001 From: Lucie Bedouret Date: Tue, 22 Nov 2022 11:57:18 +0100 Subject: [PATCH 08/14] ADD : skin and games methods --- api-rest/gateways/conversationGataway.php | 12 +++--- api-rest/gateways/gameGateway.php | 46 +++++++++++++++++++++++ api-rest/gateways/matchGateway.php | 10 ++--- api-rest/gateways/skinGateway.php | 45 ++++++++++++++++++++++ api-rest/gateways/userGateway.php | 26 ++++++------- api-rest/index.php | 19 +++++++--- api-rest/model/game.php | 17 +++++++++ api-rest/model/skin.php | 16 ++++++++ api-rest/model/user.php | 6 +-- 9 files changed, 164 insertions(+), 33 deletions(-) create mode 100644 api-rest/gateways/gameGateway.php create mode 100644 api-rest/gateways/skinGateway.php create mode 100644 api-rest/model/game.php create mode 100644 api-rest/model/skin.php diff --git a/api-rest/gateways/conversationGataway.php b/api-rest/gateways/conversationGataway.php index c49e74c..ee2d4a9 100644 --- a/api-rest/gateways/conversationGataway.php +++ b/api-rest/gateways/conversationGataway.php @@ -33,8 +33,8 @@ class ConversationGateway{ $tabIdMessages=NULL; $tabMessages=NULL; - $query1 = "SELECT idConversation FROM InConversation WHERE idUser=:idUser"; - $query2 = "SELECT idUser FROM InConversation WHERE idConversation=:idConv"; + $query1 = "SELECT idConversation FROM Use WHERE idUser=:idUser"; + $query2 = "SELECT idUser FROM Use WHERE idConversation=:idConv"; $query3 = "SELECT idMessage FROM Have WHERE idConversation=:idConv"; $query4 = "SELECT id, message, idSender FROM Message WHERE id=:id"; $query5 = "SELECT id, nom FROM Conversation WHERE id=:idConv"; @@ -88,7 +88,7 @@ class ConversationGateway{ /// Parameters : * $c (Conversation): conversation we want to insert in database public function postConversation(Conversation $c): void{ $query1 = "INSERT INTO Conversation VALUES(:idConv,:name)"; - $query2 = "INSERT INTO InConversation VALUES(:idUser,:idConv)"; + $query2 = "INSERT INTO Use VALUES(:idUser,:idConv)"; $arg1 = array('idConv'=>array($c->id,PDO::PARAM_STR), 'name'=>array($c->name, PDO::PARAM_STR)); @@ -108,11 +108,11 @@ class ConversationGateway{ $query7 = "SELECT idMessage FROM Have WHERE idConversation=:idConv"; $query8 = "DELETE FROM Message WHERE id = :id"; $query1 = "DELETE FROM Have WHERE idConversation = :idConv"; - $query2 = "DELETE FROM InConversation WHERE idConversation = :idConv"; + $query2 = "DELETE FROM Use WHERE idConversation = :idConv"; $query3 = "UPDATE Conversation SET nom=:nom WHERE id=:id"; $query4 = "INSERT INTO Have VALUES (:idConv,:idMessage)"; $query5 = "INSERT INTO Message VALUES(:id,:message,:idSender)"; - $query6 = "INSERT INTO InConversation VALUES(:idUsr,:idConv)"; + $query6 = "INSERT INTO Use VALUES(:idUsr,:idConv)"; $arg1 = array('idConv'=>array($c->id,PDO::PARAM_STR)); @@ -152,7 +152,7 @@ class ConversationGateway{ public function deleteConversation(Conversation $c):void{ $query1 = "DELETE FROM Message WHERE id=:id"; $query2 = "DELETE FROM Have WHERE idConversation = :idConv"; - $query3 = "DELETE FROM InConversation WHERE idConversation = :idConv"; + $query3 = "DELETE FROM Use WHERE idConversation = :idConv"; $query4 = "DELETE FROM Conversation WHERE id = :idConv"; foreach($c->listMessages as $msg){ diff --git a/api-rest/gateways/gameGateway.php b/api-rest/gateways/gameGateway.php new file mode 100644 index 0000000..53c0600 --- /dev/null +++ b/api-rest/gateways/gameGateway.php @@ -0,0 +1,46 @@ +connection=$_connection; + } + + /* Functions implemented to manage games' data from database + * getGames : returning all the games found in database + * getGameById : returning a game found in database with its id + */ + +/// Brief : Returning all the games found in database + public function getGames():?array{ + $tabGames=null; + $query="SELECT * FROM Game"; + $this->connection->execQuery($query,[]); + $res = $this->connection->getRes(); + foreach($res as $row){ + $tabGames[]= new Game($row['id'],$row['name'],$row['image']); + } + return $tabGames; + } + +/// Brief : Returning a game found in database with its id +/// Parameters : * $id (string): identifier of the game we are looking for + public function getGameById(string $id):?Game{ + $game=null; + $query="SELECT * FROM Game WHERE id=:id"; + $arg=array('id'=>array($id,PDO::PARAM_STR)); + $this->connection->execQuery($query,$arg); + $res=$this->connection->getRes(); + foreach($res as $row){ + $game= new Game($row['id'],$row['name'],$row['image']); + } + return $game; + } +} + +?> \ No newline at end of file diff --git a/api-rest/gateways/matchGateway.php b/api-rest/gateways/matchGateway.php index 638b3b0..b5f8eea 100644 --- a/api-rest/gateways/matchGateway.php +++ b/api-rest/gateways/matchGateway.php @@ -25,7 +25,7 @@ class MatchGateway{ public function getMatch(string $matchId):?Matchs{ $match=NULL; $query1="SELECT id, inGame, idGame FROM Matchs WHERE id = :id"; - $query2="SELECT idUser FROM InMatch WHERE idMatch=:id"; + $query2="SELECT idUser FROM Play WHERE idMatch=:id"; $arg=array('id' => array($matchId, PDO::PARAM_STR)); $this->connection->execQuery($query2, $arg); $res=$this->connection->getRes(); @@ -45,7 +45,7 @@ class MatchGateway{ /// Parameters : * $u (Matchs): match we want to insert in database public function postMatch(Matchs $m){ $query1="INSERT INTO Matchs VALUES(:idMatch,0,:idGame)"; - $query2="INSERT INTO InMatch VALUES(:idMatch,:idUser)"; + $query2="INSERT INTO Play VALUES(:idMatch,:idUser)"; $arg1=array('idMatch'=>array($m->id, PDO::PARAM_STR), 'idGame'=>array($m->idGame, PDO::PARAM_STR)); $this->connection->execQuery($query1,$arg1); @@ -62,8 +62,8 @@ class MatchGateway{ public function putMatch(Matchs $m){ $query1="UPDATE Matchs SET inGame= :inGame WHERE id=:id"; //Peut-etre la possibilité de faire mieux??? - $query2="DELETE FROM InMatch WHERE idMatch=:idMatch"; - $query3="INSERT INTO InMatch VALUES(:idMatch,:idUser)"; + $query2="DELETE FROM Play WHERE idMatch=:idMatch"; + $query3="INSERT INTO Play VALUES(:idMatch,:idUser)"; $arg1=array('inGame'=>array($m->inGame, PDO::PARAM_BOOL), 'id'=>array($m->id,PDO::PARAM_STR)); $arg2=array('idMatch'=>array($m->id,PDO::PARAM_STR)); @@ -80,7 +80,7 @@ class MatchGateway{ /// Brief : Deleting a match from database /// Parameters : * $u (Matchs): match we want to delete from database public function deleteMatch(Matchs $m){ - $query1="DELETE FROM InMatch WHERE idMatch=:id"; + $query1="DELETE FROM Play WHERE idMatch=:id"; $query2="DELETE FROM Matchs WHERE id=:id"; $arg=array('id'=>array($m->id, PDO::PARAM_STR)); $this->connection->execQuery($query1,$arg); diff --git a/api-rest/gateways/skinGateway.php b/api-rest/gateways/skinGateway.php new file mode 100644 index 0000000..cde2b6f --- /dev/null +++ b/api-rest/gateways/skinGateway.php @@ -0,0 +1,45 @@ +connection=$_connection; + } + + /* Functions implemented to manage skins' data from database + * getGames : returning all the skins found in database + * getGameById : returning a skin found in database with its id + */ + +/// Brief : Returning all the skins found in database +public function getSkins():?array{ + $tabSkins=null; + $query="SELECT * FROM Skin"; + $this->connection->execQuery($query,[]); + $res = $this->connection->getRes(); + foreach($res as $row){ + $tabSkins[]= new Game($row['id'],$row['name'],$row['image']); + } + return $tabSkins; +} + +/// Brief : Returning a skin found in database with its id +/// Parameters : * $id (string): identifier of the skin we are looking for +public function getSkinById(string $id):?Game{ + $skin=null; + $query="SELECT * FROM Skin WHERE id=:id"; + $arg=array('id'=>array($id,PDO::PARAM_STR)); + $this->connection->execQuery($query,$arg); + $res=$this->connection->getRes(); + foreach($res as $row){ + $skin= new Game($row['id'],$row['name'],$row['image']); + } + return $skin; +} +} + +?> \ No newline at end of file diff --git a/api-rest/gateways/userGateway.php b/api-rest/gateways/userGateway.php index 9f4ea3a..4c87f64 100644 --- a/api-rest/gateways/userGateway.php +++ b/api-rest/gateways/userGateway.php @@ -13,8 +13,8 @@ class UserGateway{ /* Functions implemented to manage user's data from database * getUsers : returning an array of users containing all the user stored in database - * getUserById : returning an user found in database with his id - * getUserByUsername : returning an user found in database with his username + * getUserById : returning an user found in database with its id + * getUserByUsername : returning an user found in database with its username * getUserForConnection : returning an user if there is a correspondance between the username and the password, used for connection * getLastId : returning the last Id of the users @@ -29,7 +29,7 @@ class UserGateway{ public function getUserById(string $id):?User{ $usr=NULL; $query= "SELECT * FROM User U WHERE id = :id "; - $query2="SELECT idSkin FROM HasSkin WHERE idUser=:id"; + $query2="SELECT idSkin FROM Own WHERE idUser=:id"; $arg= array('id'=> array($id,PDO::PARAM_STR)); $this->connection->execQuery($query2,$arg); $res=$this->connection->getRes(); @@ -39,7 +39,7 @@ class UserGateway{ $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'],$row['currentIdSkin'],$tabSkin); + $usr = new User ($row['id'],$row['username'],$row['password'],$row['nationality'],$row['sex'],$row['dateOfBirth'],$row['currentBobCoins'],$row['totalBobCoins'],$row['nbGamesPlayed'],$row['currentSkin'],$tabSkin); } return $usr; } @@ -50,12 +50,12 @@ class UserGateway{ $usr=NULL; $query= "SELECT * FROM User U WHERE username = :username "; - $query2="SELECT idSkin FROM HasSkin WHERE idUser=:id"; + $query2="SELECT idSkin FROM Own WHERE idUser=:id"; $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'],$row['currentIdSkin'],null); + $usr = new User ($row['id'],$row['username'],$row['password'],$row['nationality'],$row['sex'],$row['dateOfBirth'],$row['currentBobCoins'],$row['totalBobCoins'],$row['nbGamesPlayed'],$row['currentSkin'],null); } $arg2=array('id'=>array($usr->id, PDO::PARAM_STR)); $this->connection->execQuery($query2,$arg2); @@ -75,12 +75,12 @@ class UserGateway{ public function getUserForConnection(string $username, string $password):?User{ $usr=NULL; $query= "SELECT * FROM User U WHERE username = :username AND password = :password"; - $query2="SELECT idSkin FROM HasSkin WHERE idUser=:id"; + $query2="SELECT idSkin FROM Own WHERE idUser=:id"; $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'],$row['currentIdSkin'],null); + $usr = new User ($row['id'],$row['username'],$row['password'],$row['nationality'],$row['sex'],$row['dateOfBirth'],$row['currentBobCoins'],$row['totalBobCoins'],$row['nbGamesPlayed'],$row['currentSkin'],null); } $arg2=array('id'=>array($usr->id, PDO::PARAM_STR)); $this->connection->execQuery($query2,$arg2); @@ -111,7 +111,7 @@ class UserGateway{ return; } $query = "INSERT INTO User VALUES (:id, :username, :password, :nationality, :sex, :dateOfBirth, 0, 0, 0, 'S0001')"; - $query2 = "INSERT INTO HasSkin VALUES(:id,'S0001')"; + $query2 = "INSERT INTO Own VALUES(:id,'S0001')"; $arg=array('id' => array($u->id, PDO::PARAM_STR), 'username' => array($u->username, PDO::PARAM_STR), 'password' => array($u->password, PDO::PARAM_STR), @@ -126,9 +126,9 @@ class UserGateway{ /// Brief : Modifying an EXISTING user in database /// Parameters : * $u (User): user we want to update 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, currentIdSkin=:currentIdSkin WHERE id=:id"; - $query2="DELETE FROM HasSkin WHERE idUser=:id"; - $query3="INSERT INTO HasSkin VALUES(:idUsr,:idSkin)"; + $query="UPDATE User SET username = :username, password=:password, sex=:sex, nationality=:nationality, currentBobCoins=:currentBobCoins, totalBobCoins=:totalBobCoins, nbGamesPlayed=:nbGamesPlayed, currentSkin=:currentSkin WHERE id=:id"; + $query2="DELETE FROM Own WHERE idUser=:id"; + $query3="INSERT INTO Own VALUES(:idUsr,:idSkin)"; $arg=array(':id' => array($u->id, PDO::PARAM_STR), ':username' => array($u->username, PDO::PARAM_STR), ':password' => array($u->password, PDO::PARAM_STR), @@ -137,7 +137,7 @@ class UserGateway{ ':currentBobCoins' => array($u->currentBobCoins, PDO::PARAM_INT), ':totalBobCoins' => array($u->totalBobCoins, PDO::PARAM_INT), ':nbGamesPlayed' => array($u->nbGamesPlayed, PDO::PARAM_INT), - ':currentIdSkin'=> array($u->currentIdSkin, PDO::PARAM_STR)); + ':currentSkin'=> array($u->currentSkin, PDO::PARAM_STR)); $arg2=array('id'=>array($u->id,PDO::PARAM_STR)); $this->connection->execQuery($query, $arg); $this->connection->execQuery($query2,$arg2); diff --git a/api-rest/index.php b/api-rest/index.php index 7e1fe64..8863f10 100644 --- a/api-rest/index.php +++ b/api-rest/index.php @@ -8,6 +8,8 @@ include ('gateways/userGateway.php'); include ('gateways/matchGateway.php'); include ('gateways/conversationGataway.php'); + include ('gateways/gameGateway.php'); + include ('gateways/skinGateway.php'); // Connection to database // A changer quand la base de données sera hébergée, comment masquer les var? @@ -22,20 +24,25 @@ $usergw = new UserGateway($database); $matchgw = new MatchGateway($database); $conversationgw = new ConversationGateway($database); + $gamegw = new GameGateway($database); + $skingw = new SkinGateway($database); - // Testing conversation/messages methods - $listUser=array("U0001","U0004","U0005"); - $listMessages= array(new Message("ME005","coucou","U0001"), new Message("ME006","ca va","U0004"), new Message("ME007","bien et toi?","U0001")); - $newConv = new Conversation("C0006","yémen",$listMessages,$listUser); - $conversationgw->deleteConversation($newConv); + // Testing + $res=$gamegw->getGames(); + echo json_encode($res); + $res=$gamegw->getGameById("G0002"); + echo json_encode($res); + $res=$skingw->getSkins(); + echo json_encode($res); + $res=$skingw->getSkinById("S0001"); + echo json_encode($res); // Managing request, routing and sending responses /* $requestMethod = $_SERVER['REQUEST_METHOD']; $requestName = $_REQUEST['fname']; - if(empty($requestName)){ header("HTTP/1.0 400 Request Name Empty"); http_response_code(400); diff --git a/api-rest/model/game.php b/api-rest/model/game.php new file mode 100644 index 0000000..64fedd3 --- /dev/null +++ b/api-rest/model/game.php @@ -0,0 +1,17 @@ +id=$_id; + $this->name=$_name; + $this->image=$_image; + } + +} + +?> \ No newline at end of file diff --git a/api-rest/model/skin.php b/api-rest/model/skin.php new file mode 100644 index 0000000..f4ce795 --- /dev/null +++ b/api-rest/model/skin.php @@ -0,0 +1,16 @@ +id=$_id; + $this->name=$_name; + $this->image=$_image; + } +} + +?> \ No newline at end of file diff --git a/api-rest/model/user.php b/api-rest/model/user.php index 50b3fc3..2ef6275 100644 --- a/api-rest/model/user.php +++ b/api-rest/model/user.php @@ -12,10 +12,10 @@ class User { public int $currentBobCoins; public int $totalBobCoins; public int $nbGamesPlayed; - public string $currentIdSkin; + public string $currentSkin; public $listIdSkin; - public function __construct(string $_id,string $_username,string $_password, string $_nationality,string $_sex, string $_dateOfBirth, int $_currentBobCoins, int $_totalBobCoins, int $_nbGamesPlayed, string $_currentIdSkin, $_listIdSkin){ + public function __construct(string $_id,string $_username,string $_password, string $_nationality,string $_sex, string $_dateOfBirth, int $_currentBobCoins, int $_totalBobCoins, int $_nbGamesPlayed, string $_currentSkin, $_listIdSkin){ $this->id=$_id; $this->username=$_username; $this->password=$_password; @@ -25,7 +25,7 @@ class User { $this->currentBobCoins=$_currentBobCoins; $this->totalBobCoins=$_totalBobCoins; $this->nbGamesPlayed=$_nbGamesPlayed; - $this->currentIdSkin=$_currentIdSkin; + $this->currentSkin=$_currentSkin; $this->listIdSkin=$_listIdSkin; } From 9b7dab18834fdb929a41470c9bfafab9eba42901 Mon Sep 17 00:00:00 2001 From: Lucie Bedouret Date: Tue, 29 Nov 2022 11:39:05 +0100 Subject: [PATCH 09/14] =?UTF-8?q?ADD=20:=20tentative=20pour=20le=20routing?= =?UTF-8?q?=20des=20requ=C3=AAtes?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- api-rest/index.php | 109 ++++++++++++++++++++++++++++++++++----------- 1 file changed, 84 insertions(+), 25 deletions(-) diff --git a/api-rest/index.php b/api-rest/index.php index 8863f10..d1f48e3 100644 --- a/api-rest/index.php +++ b/api-rest/index.php @@ -27,19 +27,11 @@ $gamegw = new GameGateway($database); $skingw = new SkinGateway($database); - // Testing - $res=$gamegw->getGames(); - echo json_encode($res); - $res=$gamegw->getGameById("G0002"); - echo json_encode($res); - - $res=$skingw->getSkins(); - echo json_encode($res); - $res=$skingw->getSkinById("S0001"); - echo json_encode($res); - - // Managing request, routing and sending responses - /* + // Managing request, routing and sending + // ------ + // RAPPEL POUR MOI MÊME : NE PAS OUBLIER DE FAIRE DES TRY CATCH !!!!!!! + // ------ + $requestMethod = $_SERVER['REQUEST_METHOD']; $requestName = $_REQUEST['fname']; @@ -53,27 +45,86 @@ switch ($requestName){ case 'getUser': if (!empty($_GET["id"])){ - //read an user by his id + //read an user by its id $id = intval($_GET["id"]); - $res=$usergw->getUserById($id); + try{ + $res=$usergw->getUserById($id); + //retourner le résultat + } catch (PDOException $e){ + header("HTTP/1.0 ".$e->getMessage()); + http_response_code(600); // Quel code pour les erreurs PDO? + } } elseif (!empty($_GET["username"])){ // read an user by his username $username = intval($_GET["username"]); - $res=$usergw->getUserByUsername($username); + try{ + $res=$usergw->getUserByUsername($username); + //retourner le résultat + } catch (PDOException $e){ + header("HTTP/1.0 ".$e->getMessage()); + http_response_code(600); // Quel code pour les erreurs PDO? + } } else{ - + header("HTTP/1.0 405 Missing argument id or username"); + http_response_code(405); } - break; case 'getMatch': - + if(!empty($_GET["id"])){ + //read a match by its id + $id = intval($_GET["id"]); + try{ + $res=$matchgw->getMatchById($id); + //retourner le résultat + } catch (PDOException $e) { + header("HTTP/1.0 ".$e->getMessage()); + http_response_code(600); // Quel code pour les erreurs PDO? + } + } + else{ + header("HTTP/1.0 405 Missing argument id"); + http_response_code(405); + } break; - case 'getMessage': - + case 'getConversation': + if(!emptyempty($_GET["id"])){ + // read conversations by the id of a user + $idUsr = intval($_GET["id"]); + try{ + $res=$conversationgw->getConversations($idUsr); + // retourner le résultat + } catch (PDOException $e) { + header("HTTP/1.0 ".$e->getMessage()); + http_response_code(600); // Quel code pour les erreurs PDO? + } + } + else{ + header("HTTP/1.0 405 Missing argument idUsr"); + http_response_code(405); + } break; - case 'getConversation ': - + case 'getSkin': + try{ + $res = $skingw->getSkins(); + //retourner le résultat + } catch (PDOException $e) { + header("HTTP/1.0 ".$e->getMessage()); + http_response_code(600); // Quel code pour les erreurs PDO? + } + break; + case 'getGames': + try{ + $res = $gamegw->getGames(); + //retourner le résultat + } catch (PDOException $e) { + header("HTTP/1.0 ".$e->getMessage()); + http_response_code(600); // Quel code pour les erreurs PDO? + } + break; + default: + header("HTTP/1.0 406 unknown method"); + http_response_code(406); // Le bon code ? break; } break; @@ -81,7 +132,16 @@ case 'POST': switch ($requestName){ case 'postUser': - // create a new user and add it in database + if(!empty($_POST["id"])){ + $usr = new User($_POST["id"],$_POST["username"],$_POST["password"],$_POST["nationality"],$_POST["sex"],$_POST["dateOfBirth"],0,0,0,"S0001",[]); + try{ + $usergw->postUser($usr); + http_response_code(200); + } catch (PDOException $e) { + header("HTTP/1.0 ".$e->getMessage()); + http_response_code(600); // Quel code pour les erreurs PDO? + } + } break; case 'postMatch': @@ -136,6 +196,5 @@ } } - */ ?> \ No newline at end of file From edfa17394019d02ffae3e5a894e9cedda86b7f6c Mon Sep 17 00:00:00 2001 From: Lucie Bedouret Date: Tue, 29 Nov 2022 17:44:46 +0100 Subject: [PATCH 10/14] =?UTF-8?q?MODIFY=20:=20d=C3=A9but=20des=20correctio?= =?UTF-8?q?ns?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- api-rest/config.ini | 5 + api-rest/gateways/.htaccess | 6 + api-rest/gateways/conversationGataway.php | 178 ++++++++++------------ api-rest/index.php | 34 ++++- 4 files changed, 117 insertions(+), 106 deletions(-) create mode 100644 api-rest/config.ini create mode 100644 api-rest/gateways/.htaccess diff --git a/api-rest/config.ini b/api-rest/config.ini new file mode 100644 index 0000000..ef648d6 --- /dev/null +++ b/api-rest/config.ini @@ -0,0 +1,5 @@ +; Database connection informations +[database_section] +dsn = "mysql:dbname=bobParty;host=127.0.0.1;port=8889" +username = "root" +password = "root"; diff --git a/api-rest/gateways/.htaccess b/api-rest/gateways/.htaccess new file mode 100644 index 0000000..f714801 --- /dev/null +++ b/api-rest/gateways/.htaccess @@ -0,0 +1,6 @@ + +order allow, deny +deny from all + +RewriteCond %{REQUEST_METHOD} ^(TRACE|TRACK) [NC] +RewriteRule (.*) - [F] \ No newline at end of file diff --git a/api-rest/gateways/conversationGataway.php b/api-rest/gateways/conversationGataway.php index ee2d4a9..b3e7ed0 100644 --- a/api-rest/gateways/conversationGataway.php +++ b/api-rest/gateways/conversationGataway.php @@ -27,59 +27,45 @@ class ConversationGateway{ ///(with all the id of the messages and the users in the conversation) /// Parameters : * $idUser (string): identifier of the user we want to get the conversations public function getConversations(string $_idUser):?array{ - $tabIdConversation=NULL; + // Declaration of arrays (NULL) and queries $tabConversations=NULL; $tabUsers=NULL; - $tabIdMessages=NULL; $tabMessages=NULL; - - $query1 = "SELECT idConversation FROM Use WHERE idUser=:idUser"; - $query2 = "SELECT idUser FROM Use WHERE idConversation=:idConv"; - $query3 = "SELECT idMessage FROM Have WHERE idConversation=:idConv"; - $query4 = "SELECT id, message, idSender FROM Message WHERE id=:id"; - $query5 = "SELECT id, nom FROM Conversation WHERE id=:idConv"; - - $arg1=array('idUser'=>array($_idUser, PDO::PARAM_STR)); - - $this->connection->execQuery($query1,$arg1); + $conversationQuery = "SELECT c.id, c.nom + FROM T_E_CONVERSATION_COV c, T_J_DISCUTE_DIS d + WHERE c.id=d.idConv + AND d.idUser=:idUser"; + $messagesQuery = "SELECT m.id, m.message, m.idSender + FROM T_R_MESSAGE_MSG m, T_J_DISCUTE_DIS d + WHERE m.id=h.idMessage + AND h.idConv=:idConv"; + $usersQuery = "SELECT d.idUser + FROM T_J_DISCUTE_DIS d + WHERE d.idConv = :idConv"; + //Find all the conversations where the user belong + $argIdUser=array('idUser'=>array($_idUser, PDO::PARAM_STR)); + $this->connection->execQuery($conversationQuery,$argIdUser); $res=$this->connection->getRes(); - foreach($res as $row){ - $tabIdConversation[] = $row['idConversation']; - } - - foreach($tabIdConversation as $idConv){ - $arg2 = array('idConv'=>array($idConv, PDO::PARAM_STR)); - $this->connection->execQuery($query2,$arg2); - $res=$this->connection->getRes(); - foreach($res as $row){ - $tabUsers[] = $row['idUser']; - } - - $this->connection->execQuery($query3,$arg2); - $res=$this->connection->getRes(); - foreach($res as $row){ - $tabIdMessages[] = $row['idMessage']; - } - - foreach($tabIdMessages as $idMessage){ - $arg3=array('id'=>array($idMessage,PDO::PARAM_STR)); - $this->connection->execQuery($query4,$arg3); - $res=$this->connection->getRes(); - foreach($res as $row){ - $tabMessages[] = new Message($row['id'],$row['message'],$row['idSender']); - } + foreach($res as $row){ + $argIdConv= array('idConv'=>array($row['idConversation'], PDO::PARAM_STR)); + // Find all messages of the conversation + $this->connection->execQuery($messagesQuery,$argIdConv); + $resMessages=$this->connection->getRes(); + foreach($resMessages as $rowMessages){ + $tabUsers[] = new Message($rowMessages['id'],$rowMessages['message'],$rowMessages['idSender']); } - - $this->connection->execQuery($query5,$arg2); - $res=$this->connection->getRes(); - foreach($res as $row){ - $tabConversations[]= new Conversation($row['id'], $row['nom'],$tabMessages,$tabUsers); + // Find all the users in the conversation + $this->connection->execQuery($usersQuery,$argIdConv); + $resUsers=$this->connection->getRes(); + foreach($resUsers as $rowUsers){ + $tabUsers[] = $rowUsers['idUser']; } - + // Add the conversation into the array + $tabConversations = new Conversation($row['id'],$row['nom'],$tabMessages,$tabUsers); + // Restore the arrays $tabUsers=array(); - $tabIdMessages=array(); - $tabMessages=array(); + $tabMessages=array(); } return $tabConversations; } @@ -87,16 +73,17 @@ class ConversationGateway{ /// Brief : Adding a new conversation in database /// Parameters : * $c (Conversation): conversation we want to insert in database public function postConversation(Conversation $c): void{ - $query1 = "INSERT INTO Conversation VALUES(:idConv,:name)"; - $query2 = "INSERT INTO Use VALUES(:idUser,:idConv)"; - - $arg1 = array('idConv'=>array($c->id,PDO::PARAM_STR), + // Declare queries + $convCreationQuery = "INSERT INTO T_E_CONVERSATION_COV VALUES(:idConv,:name)"; + $addUserInConvQuery = "INSERT INTO T_J_DISCUTE_DIS VALUES(:idUser,:idConv)"; + $argconvCreationQuery = array('idConv'=>array($c->id,PDO::PARAM_STR), 'name'=>array($c->name, PDO::PARAM_STR)); - $this->connection->execQuery($query1,$arg1); - + // Create a new conversation + $this->connection->execQuery($convCreationQuery,$argconvCreationQuery); + // Add users of the conversation in the conversation foreach($c->listIdUsers as $idUsr){ - $arg2 = array('idUser'=>array($idUsr, PDO::PARAM_STR), + $argUserInConvQuery = array('idUser'=>array($idUsr, PDO::PARAM_STR), 'idConv'=>array($c->id, PDO::PARAM_STR)); $this->connection->execQuery($query2,$arg2); } @@ -105,64 +92,53 @@ class ConversationGateway{ /// Brief : Modifying an EXISTING match in database /// Parameters : * $u (Matchs): match we want to update in database public function putConversation(Conversation $c):void{ - $query7 = "SELECT idMessage FROM Have WHERE idConversation=:idConv"; - $query8 = "DELETE FROM Message WHERE id = :id"; - $query1 = "DELETE FROM Have WHERE idConversation = :idConv"; - $query2 = "DELETE FROM Use WHERE idConversation = :idConv"; - $query3 = "UPDATE Conversation SET nom=:nom WHERE id=:id"; - $query4 = "INSERT INTO Have VALUES (:idConv,:idMessage)"; - $query5 = "INSERT INTO Message VALUES(:id,:message,:idSender)"; - $query6 = "INSERT INTO Use VALUES(:idUsr,:idConv)"; - - - $arg1 = array('idConv'=>array($c->id,PDO::PARAM_STR)); - $arg2 = array('nom'=>array($c->name, PDO::PARAM_STR), - 'id'=>array($c->id,PDO::PARAM_STR)); - - $this->connection->execQuery($query7,$arg1); - $res = $this->connection->getRes(); - foreach($res as $idMsg){ - $arg6 = array('id'=>array($idMsg['idMessage'],PDO::PARAM_STR)); - $this->connection->execQuery($query8,$arg6); - } - - $this->connection->execQuery($query1,$arg1); - $this->connection->execQuery($query2, $arg1); - $this->connection->execQuery($query3,$arg2); - + // Declare the queries + $conversationInsertionQuery = "INSERT INTO T_E_CONVERSATION_COV VALUES (:id,:nom)"; + $messageInsertionQuery = "INSERT INTO T_R_MESSAGE_MSG VALUES(:id,:message,:idSender)"; + $discuteInsertionQuery = "INSERT INTO T_J_DISCUTE_DIS VALUES(:idUser,:idConv)"; + $containInsertionQuery = "INSERT INTO T_J_CONTAIN_MESSAGE_CTN VALUES(:idConv,:idMessage)"; + $argConversationInsertion = array('id'=>array($c->id, PDO::PARAM_STR), + 'nom'=>array($c->name,PDO::PARAM_STR)); + // Delete current data from database + deleteConversation($c); + // Add conversation + $this->connection->execQuery($conversationInsertionQuery,$argConversationInsertion); + // Add messages to conversation foreach($c->listMessages as $msg){ - $arg3 = array('idConv'=>array($c->id,PDO::PARAM_STR), - 'idMessage'=>array($msg->id,PDO::PARAM_STR)); - $arg4 = array('id'=>array($msg->id,PDO::PARAM_STR), - 'message'=>array($msg->message,PDO::PARAM_STR), - 'idSender'=>array($msg->idSender,PDO::PARAM_STR)); - $this->connection->execQuery($query4,$arg3); - $this->connection->execQuery($query5,$arg4); + $argContainInsertion = array('idConv'=>array($c->id,PDO::PARAM_STR), + 'idMessage'=>array($msg->id,PDO::PARAM_STR)); + $argMessageInsertion = array('id'=>array($msg->id,PDO::PARAM_STR), + 'message'=>array($msg->message,PDO::PARAM_STR), + 'idSender'=>array($msg->idSender,PDO::PARAM_STR)); + $this->connection->execQuery($containInsertionQuery,$argContainInsertion); + $this->connection->execQuery($messageInsertionQuery,$argMessageInsertion); } - + // Add user to conversation foreach($c->listIdUsers as $idUsr){ - $arg5 = array('idUsr'=>array($idUsr,PDO::PARAM_STR), - 'idConv'=>array($c->id,PDO::PARAM_STR)); - $this->connection->execQuery($query6,$arg5); + $argDiscuteInsertion = array('idUsr'=>array($idUsr,PDO::PARAM_STR), + 'idConv'=>array($c->id,PDO::PARAM_STR)); + $this->connection->execQuery($discuteInsertionQuery,$argDiscuteInsertion); } } /// Brief : Deleting a conversation and its messages from database /// Parameters : * $c (Conversation): conversation we want to delete from database +// ---- +// Ne pas oublier le on delete cascade dans la création des tables +// ---- public function deleteConversation(Conversation $c):void{ - $query1 = "DELETE FROM Message WHERE id=:id"; - $query2 = "DELETE FROM Have WHERE idConversation = :idConv"; - $query3 = "DELETE FROM Use WHERE idConversation = :idConv"; - $query4 = "DELETE FROM Conversation WHERE id = :idConv"; - - foreach($c->listMessages as $msg){ - $arg1 = array('id'=>array($msg->id,PDO::PARAM_STR)); - $this->connection->execQuery($query1,$arg1); - } - $arg2 = array('idConv'=>array($c->id,PDO::PARAM_STR)); - $this->connection->execQuery($query2,$arg2); - $this->connection->execQuery($query3,$arg2); - $this->connection->execQuery($query4,$arg2); + // Declare query and argument table + $deleteMessagesQuery = "DELETE FROM T_R_MESSAGE_MSG + WHERE id=(SELECT id + FROM T_R_MESSAGE_MSG m, T_J_CONTAIN_MESSAGE_CTN c + WHERE m.id = c.idConversation + AND c.idConversation=:idConv"; + $deleteConv = "DELETE FROM T_E_CONVERSATION_COV + WHERE id=:idConv"; // Suffisant grâce au on delete cascade (à ne pas oublier) + $argIdConv = array('idConv'=>array($c->id,PDO::PARAM_STR)); + // Executing queries + $this->connection->execQuery($deleteMessagesQuery,$argIdConv); + $this->connection->execQuery($deleteConv,$argIdConv); } } diff --git a/api-rest/index.php b/api-rest/index.php index d1f48e3..2862cec 100644 --- a/api-rest/index.php +++ b/api-rest/index.php @@ -13,14 +13,24 @@ // Connection to database // A changer quand la base de données sera hébergée, comment masquer les var? - $dsn ="mysql:dbname=bobParty;host=127.0.0.1;port=8889"; - $username="root"; - $password="root"; + // ------ + // A mettre dans un fichier et .htaccess + // ------ + require('config.php'); // Initializing Database - $database = new DatabaseConnection($dsn,$username,$password); + try{ + $database = new DatabaseConnection($dsn,$username,$password); + } catch (PDOException $e) { + header("HTTP/1.0 ".$e->getMessage()); + http_response_code(600); // Quel code pour les erreurs PDO? + } + // Initializing Gateways + // ------ + // Passer en mode objet ou rester en mode comportemental mais assumé ??? + // ------ $usergw = new UserGateway($database); $matchgw = new MatchGateway($database); $conversationgw = new ConversationGateway($database); @@ -33,7 +43,7 @@ // ------ $requestMethod = $_SERVER['REQUEST_METHOD']; - $requestName = $_REQUEST['fname']; + $requestName = $_REQUEST['fname']; if(empty($requestName)){ header("HTTP/1.0 400 Request Name Empty"); @@ -142,8 +152,22 @@ http_response_code(600); // Quel code pour les erreurs PDO? } } + else{ + header("HTTP/1.0 405 Missing user to create"); + http_response_code(405); + } break; case 'postMatch': + if(!empty($_POST["id"])){ + $match = new Match($_POST["id"],false,$_POST["idGame"],$_POST["idUsr"]); + try{ + $matchgw->postMatch($match); + http_response_code(200); + } catch (PDOException $e) { + header("HTTP/1.0 ".$e->getMessage()); + http_response_code(600); // Quel code pour les erreurs PDO? + } + } break; case 'postMessage': From b0bd93a8dc08f5c93597f14a1a6256d0b8b82b5e Mon Sep 17 00:00:00 2001 From: Lucie Bedouret Date: Tue, 6 Dec 2022 10:01:23 +0100 Subject: [PATCH 11/14] MODIFY : revue de tout le code (sauf classe convGateway) et tables sql --- api-rest/.htaccess | 13 + api-rest/config.ini | 5 +- api-rest/gateways/.htaccess | 6 - api-rest/gateways/conversationGataway.php | 3 + api-rest/gateways/gameGateway.php | 16 +- api-rest/gateways/matchGateway.php | 92 +++--- api-rest/gateways/skinGateway.php | 37 +-- api-rest/gateways/userGateway.php | 229 +++++++------- api-rest/index.php | 364 +++++++++++----------- api-rest/model/game.php | 4 +- api-rest/model/skin.php | 6 +- api-rest/model/user.php | 10 +- db-config.sql | 152 +++++++++ 13 files changed, 567 insertions(+), 370 deletions(-) create mode 100644 api-rest/.htaccess delete mode 100644 api-rest/gateways/.htaccess create mode 100644 db-config.sql diff --git a/api-rest/.htaccess b/api-rest/.htaccess new file mode 100644 index 0000000..e833542 --- /dev/null +++ b/api-rest/.htaccess @@ -0,0 +1,13 @@ +# Rederection if URL not found + + RewriteEngine on + RewriteCond %{REQUEST_FILEANME} !-f + RewriteCond %{REQUEST_FILEANME} !-d + RewriteRule (.+) index.php?p=$1 [QSA,L] + + + +Order Allow,Deny +Allow From all + + diff --git a/api-rest/config.ini b/api-rest/config.ini index ef648d6..262c40f 100644 --- a/api-rest/config.ini +++ b/api-rest/config.ini @@ -1,5 +1,4 @@ -; Database connection informations -[database_section] -dsn = "mysql:dbname=bobParty;host=127.0.0.1;port=8889" +[database] +dsn = "mysql:host=localhost;port=8888;dbname=bobParty" username = "root" password = "root"; diff --git a/api-rest/gateways/.htaccess b/api-rest/gateways/.htaccess deleted file mode 100644 index f714801..0000000 --- a/api-rest/gateways/.htaccess +++ /dev/null @@ -1,6 +0,0 @@ - -order allow, deny -deny from all - -RewriteCond %{REQUEST_METHOD} ^(TRACE|TRACK) [NC] -RewriteRule (.*) - [F] \ No newline at end of file diff --git a/api-rest/gateways/conversationGataway.php b/api-rest/gateways/conversationGataway.php index b3e7ed0..0d42550 100644 --- a/api-rest/gateways/conversationGataway.php +++ b/api-rest/gateways/conversationGataway.php @@ -72,6 +72,7 @@ class ConversationGateway{ /// Brief : Adding a new conversation in database /// Parameters : * $c (Conversation): conversation we want to insert in database +/// ***** CRÉER DES TRIGGERS ***** /// public function postConversation(Conversation $c): void{ // Declare queries $convCreationQuery = "INSERT INTO T_E_CONVERSATION_COV VALUES(:idConv,:name)"; @@ -91,6 +92,7 @@ class ConversationGateway{ /// Brief : Modifying an EXISTING match in database /// Parameters : * $u (Matchs): match we want to update in database +/// ***** CRÉER DES TRIGGERS ***** /// public function putConversation(Conversation $c):void{ // Declare the queries $conversationInsertionQuery = "INSERT INTO T_E_CONVERSATION_COV VALUES (:id,:nom)"; @@ -125,6 +127,7 @@ class ConversationGateway{ /// Parameters : * $c (Conversation): conversation we want to delete from database // ---- // Ne pas oublier le on delete cascade dans la création des tables +// Créer des triggers // ---- public function deleteConversation(Conversation $c):void{ // Declare query and argument table diff --git a/api-rest/gateways/gameGateway.php b/api-rest/gateways/gameGateway.php index 53c0600..2875ab2 100644 --- a/api-rest/gateways/gameGateway.php +++ b/api-rest/gateways/gameGateway.php @@ -19,11 +19,13 @@ class GameGateway{ /// Brief : Returning all the games found in database public function getGames():?array{ $tabGames=null; - $query="SELECT * FROM Game"; - $this->connection->execQuery($query,[]); + $gamesQuery="SELECT * FROM T_E_GAME_GAM"; + $this->connection->execQuery($gamesQuery,[]); $res = $this->connection->getRes(); foreach($res as $row){ - $tabGames[]= new Game($row['id'],$row['name'],$row['image']); + $tabGames[]= new Game($row['PK_ID'], + $row['GAM_NAME'], + $row['GAM_IMAGE']); } return $tabGames; } @@ -32,12 +34,14 @@ class GameGateway{ /// Parameters : * $id (string): identifier of the game we are looking for public function getGameById(string $id):?Game{ $game=null; - $query="SELECT * FROM Game WHERE id=:id"; + $gameInfoQuery="SELECT * FROM T_E_GAME_GAM WHERE PK_ID=:id"; $arg=array('id'=>array($id,PDO::PARAM_STR)); - $this->connection->execQuery($query,$arg); + $this->connection->execQuery($gameInfoQuery,$arg); $res=$this->connection->getRes(); foreach($res as $row){ - $game= new Game($row['id'],$row['name'],$row['image']); + $game= new Game($row['PK_ID'], + $row['GAM_NAME'], + $row['GAM_IMAGE']); } return $game; } diff --git a/api-rest/gateways/matchGateway.php b/api-rest/gateways/matchGateway.php index b5f8eea..202a85f 100644 --- a/api-rest/gateways/matchGateway.php +++ b/api-rest/gateways/matchGateway.php @@ -13,7 +13,7 @@ class MatchGateway{ /* Functions implemented to manage matches' data from database - * getMatch : returning a match found in database with its id + * getMatchById : returning a match found in database with its id * postMatch : adding a NEW user in database * putMatch : modifying an EXISTING user in database * deleteMatch : deleting an user from database @@ -22,69 +22,75 @@ class MatchGateway{ /// Brief : Returning a match found in database with his id /// Parameters : * $id (string): identifier of the match we are looking for - public function getMatch(string $matchId):?Matchs{ + public function getMatchById(string $matchId):?Matchs{ $match=NULL; - $query1="SELECT id, inGame, idGame FROM Matchs WHERE id = :id"; - $query2="SELECT idUser FROM Play WHERE idMatch=:id"; - $arg=array('id' => array($matchId, PDO::PARAM_STR)); - $this->connection->execQuery($query2, $arg); + $matchInfoQuery="SELECT PK_ID, MTC_IN_GAME, FK_ID_GAME FROM T_E_MATCH_MTC WHERE PK_ID = :id"; + $playersInMatchQuery="SELECT FK_USER FROM T_J_PLAY_MATCH_PLM WHERE FK_MATCH=:id"; + $argId=array('id' => array($matchId, PDO::PARAM_INT)); + $this->connection->execQuery($playersInMatchQuery, $argId); $res=$this->connection->getRes(); foreach($res as $row){ - $tabUser[] = $row['idUser']; + $tabUser[] = $row['FK_USER']; } - - $this->connection->execQuery($query1, $arg); + $this->connection->execQuery($matchInfoQuery, $argId); $res=$this->connection->getRes(); foreach($res as $row){ - $match = new Matchs($row['id'],$row['inGame'],$row['idGame'],$tabUser); + $match = new Matchs($row['PK_ID'],$row['MTC_IN_GAME'],$row['FK_ID_GAME'],$tabUser); } return $match; } /// Brief : Adding a NEW match in database -/// Parameters : * $u (Matchs): match we want to insert in database - public function postMatch(Matchs $m){ - $query1="INSERT INTO Matchs VALUES(:idMatch,0,:idGame)"; - $query2="INSERT INTO Play VALUES(:idMatch,:idUser)"; - $arg1=array('idMatch'=>array($m->id, PDO::PARAM_STR), - 'idGame'=>array($m->idGame, PDO::PARAM_STR)); - $this->connection->execQuery($query1,$arg1); - foreach($m->listIdUsers as $idUsr){ - $arg2=array('idMatch'=>array($m->id, PDO::PARAM_STR), - 'idUser'=>array($idUsr, PDO::PARAM_STR)); - $this->connection->execQuery($query2,$arg2); + public function postMatch(int $idGame, int $idCreator){ + $insertMatchQuery="INSERT INTO T_E_MATCH_MTC VALUES(NULL,0,:idGame)"; + $insertPlayQuery = "INSERT INTO T_J_PLAY_MATCH_PLM VALUES(:idCreator,:id);"; + $argInsertMatch=array('idGame'=>array($idGame, PDO::PARAM_INT)); + $this->connection->execQuery($insertMatchQuery,$argInsertMatch); + $this->connection->execQuery("SELECT PK_ID + FROM T_E_MATCH_MTC + WHERE PK_ID >= ALL (SELECT max(m2.PK_ID) + FROM T_E_MATCH_MTC m2)",[]); + $res=$this->connection->getRes(); + foreach($res as $row){ + $id=$row['PK_ID']; } + $argInsertPlay= array('idCreator'=>array($idCreator,PDO::PARAM_INT), + 'id'=>array($id,PDO::PARAM_INT)); + $this->connection->execQuery($insertPlayQuery,$argInsertPlay); return; } /// Brief : Modifying an EXISTING match in database -/// Parameters : * $u (Matchs): match we want to update in database - public function putMatch(Matchs $m){ - $query1="UPDATE Matchs SET inGame= :inGame WHERE id=:id"; - //Peut-etre la possibilité de faire mieux??? - $query2="DELETE FROM Play WHERE idMatch=:idMatch"; - $query3="INSERT INTO Play VALUES(:idMatch,:idUser)"; - $arg1=array('inGame'=>array($m->inGame, PDO::PARAM_BOOL), - 'id'=>array($m->id,PDO::PARAM_STR)); - $arg2=array('idMatch'=>array($m->id,PDO::PARAM_STR)); - $this->connection->execQuery($query1,$arg1); - $this->connection->execQuery($query2,$arg2); - foreach($m->listIdUsers as $idUsr){ - $arg3=array('idMatch'=>array($m->id, PDO::PARAM_STR), - 'idUser'=>array($idUsr,PDO::PARAM_STR)); - $this->connection->execQuery($query3,$arg3); - } + public function putMatch(int $id){ + $updateQuery="UPDATE T_E_MATCH_MTC SET MTC_IN_GAME=1 WHERE PK_ID=:id"; + $argUpdate=array('id'=>array($id,PDO::PARAM_INT)); + $this->connection->execQuery($updateQuery,$argUpdate); + return; + } + +/// Brief : Adding an user into a match + public function addUserToMatch(int $idMatch, int $idUser){ + $insertQuery = "INSERT INTO T_J_PLAY_MATCH_PLM VALUES(:idUser,:idMatch)"; + $argInsert= array('idUser'=>array($idUser,PDO::PARAM_INT), + 'idMatch'=>array($idMatch,PDO::PARAM_INT)); + $this->connection->execQuery($insertQuery,$argInsert); + return; + } + +/// Brief : Deleting an user from a match + public function deleteUserFromMatch(int $idUser){ + $deleteQuery = "DELETE FROM T_J_PLAY_MATCH_PLM WHERE FK_USER=:idUser"; + $argDelete = array('idUser'=>array($idUser,PDO::PARAM_INT)); + $this->connection->execQuery($deleteQuery,$argDelete); return; } /// Brief : Deleting a match from database /// Parameters : * $u (Matchs): match we want to delete from database - public function deleteMatch(Matchs $m){ - $query1="DELETE FROM Play WHERE idMatch=:id"; - $query2="DELETE FROM Matchs WHERE id=:id"; - $arg=array('id'=>array($m->id, PDO::PARAM_STR)); - $this->connection->execQuery($query1,$arg); - $this->connection->execQuery($query2,$arg); + public function deleteMatch(int $id){ + $query="DELETE FROM T_J_PLAY_MATCH_PLM WHERE PK_ID=:id"; + $arg=array('id'=>array($id, PDO::PARAM_INT)); + $this->connection->execQuery($query,$arg); } } diff --git a/api-rest/gateways/skinGateway.php b/api-rest/gateways/skinGateway.php index cde2b6f..af417ad 100644 --- a/api-rest/gateways/skinGateway.php +++ b/api-rest/gateways/skinGateway.php @@ -15,31 +15,20 @@ class SkinGateway{ * getGameById : returning a skin found in database with its id */ -/// Brief : Returning all the skins found in database -public function getSkins():?array{ - $tabSkins=null; - $query="SELECT * FROM Skin"; - $this->connection->execQuery($query,[]); - $res = $this->connection->getRes(); - foreach($res as $row){ - $tabSkins[]= new Game($row['id'],$row['name'],$row['image']); + /// Brief : Returning all the skins found in database + public function getSkins():?array{ + $tabSkins=null; + $skinQuery="SELECT * FROM T_H_SKIN_SKI"; + $this->connection->execQuery($skinQuery,[]); + $res = $this->connection->getRes(); + foreach($res as $row){ + $tabSkins[]= new Skin($row['PK_ID'], + $row['SKI_NAME'], + $row['SKI_IMAGE'], + $row['SKI_PRICE']); + } + return $tabSkins; } - return $tabSkins; -} - -/// Brief : Returning a skin found in database with its id -/// Parameters : * $id (string): identifier of the skin we are looking for -public function getSkinById(string $id):?Game{ - $skin=null; - $query="SELECT * FROM Skin WHERE id=:id"; - $arg=array('id'=>array($id,PDO::PARAM_STR)); - $this->connection->execQuery($query,$arg); - $res=$this->connection->getRes(); - foreach($res as $row){ - $skin= new Game($row['id'],$row['name'],$row['image']); - } - return $skin; -} } ?> \ No newline at end of file diff --git a/api-rest/gateways/userGateway.php b/api-rest/gateways/userGateway.php index 4c87f64..6dff7cb 100644 --- a/api-rest/gateways/userGateway.php +++ b/api-rest/gateways/userGateway.php @@ -10,60 +10,90 @@ class UserGateway{ $this->connection=$con; } - /* Functions implemented to manage user's data from database - - * getUsers : returning an array of users containing all the user stored in database + /* CRUD methods * getUserById : returning an user found in database with its id * getUserByUsername : returning an user found in database with its username * getUserForConnection : returning an user if there is a correspondance between the username and the password, used for connection - * getLastId : returning the last Id of the users * postUser : adding a NEW user in database * putUser : modifying an EXISTING user in database + * putSkinList : adding a skin into the list of skins of the user * deleteUser : deleting an user from database + * addSkin : adding a skin to the list of skins bleonged by an user + */ + /* Other methods + * convertResToUser : converting the result of a PDO query into an instance of User + * getSkinList : search into database the list of skin the user have */ -/// Brief : Returning an user found in database with his id -/// Parameters : * $id (string): identifier of the user we are looking for - public function getUserById(string $id):?User{ - $usr=NULL; - $query= "SELECT * FROM User U WHERE id = :id "; - $query2="SELECT idSkin FROM Own WHERE idUser=:id"; - $arg= array('id'=> array($id,PDO::PARAM_STR)); - $this->connection->execQuery($query2,$arg); - $res=$this->connection->getRes(); + +/// Brief : Converting the result of a PDO query into an instance of User +/// Parameter : * $res : result of the PDO query + public function convertResToUser($res):?User{ + $usr=null; foreach($res as $row){ - $tabSkin[]=$row['idSkin']; + $usr= new User($row['PK_ID'], + $row['USR_USERNAME'], + $row['USR_PASSWORD'], + $row['USR_NATIONALITY'], + $row['USR_SEX'], + $row['USR_DATE_OF_BIRTH'], + $row['USR_CURRENT_NB_COINS'], + $row['USR_TOTAL_NB_COINS'], + $row['USR_NB_GAMES_PLAYED'], + $row['FK_CURRENT_SKIN'], + null); } - $this->connection->execQuery($query,$arg); + return $usr; + } + +/// Brief : Research into database the list of skin the user have +/// Parameter : * $id (int) : id of the user we want to get the list + public function getSkinList(int $id):?array{ + $tabSkin=null; + $skinsOfUserQuery="SELECT s.* + FROM T_H_SKIN_SKI s, T_J_OWN_SKIN_OWN o + WHERE o.FK_USER=:id"; + $argIdUser=array('id'=>array($id,PDO::PARAM_STR)); + $this->connection->execQuery($skinsOfUserQuery,$argIdUser); + $resSkin=$this->connection->getRes(); + foreach($resSkin as $row){ + $tabSkin[]= new Skin($row['PK_ID'], $row['SKI_NAME'], $row['SKI_IMAGE'],$row['SKI_PRICE']); + } + return $tabSkin; + } + + +/// Brief : Returning an user found in database with his id +/// Parameters : * $id (string): identifier of the user we are looking for + public function getUserById(int $id):?User{ + $userQuery="SELECT * + FROM T_S_USER_USR + WHERE PK_ID = :id"; + $argIdUser=array('id'=>array($id,PDO::PARAM_INT)); + $this->connection->execQuery($userQuery,$argIdUser); $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'],$row['currentSkin'],$tabSkin); + $usr=$this->convertResToUser($res); + if ($usr != null){ + $usr->listSkin=$this->getSkinList($usr->id); } return $usr; } /// Brief : Returning an user found in database with his username /// Parameters : * $username (string): username of the user we are looking for - public function getUserByUsername(string $username):?User{ - $usr=NULL; - - $query= "SELECT * FROM User U WHERE username = :username "; - $query2="SELECT idSkin FROM Own WHERE idUser=:id"; - $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'],$row['currentSkin'],null); - } - $arg2=array('id'=>array($usr->id, PDO::PARAM_STR)); - $this->connection->execQuery($query2,$arg2); + public function getUserByUsername (string $username):?User{ + $userQuery = "SELECT * + FROM T_S_USER_USR + WHERE USR_USERNAME=:username"; + $argUsername=array('username'=>array($username,PDO::PARAM_STR)); + $this->connection->execQuery($userQuery,$argUsername); $res=$this->connection->getRes(); - foreach($res as $row){ - $tabSkin[]=$row['idSkin']; + $usr=$this->convertResToUser($res); + if ($usr != null){ + $usr->listSkin=$this->getSkinList($usr->id); } - $usr->listIdSkin=$tabSkin; return $usr; } @@ -72,91 +102,82 @@ class UserGateway{ /// * $password (string): password of the user we are looking for /// Comment : this function returns an user if it finds a match between an username and password, /// if it doesn't, it means there are no corresponding user - public function getUserForConnection(string $username, string $password):?User{ - $usr=NULL; - $query= "SELECT * FROM User U WHERE username = :username AND password = :password"; - $query2="SELECT idSkin FROM Own WHERE idUser=:id"; - $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'],$row['currentSkin'],null); - } - $arg2=array('id'=>array($usr->id, PDO::PARAM_STR)); - $this->connection->execQuery($query2,$arg2); + public function getUserForConnection(string $username,string $password):?User{ + $userQuery = "SELECT * + FROM T_S_USER_USR + WHERE USR_USERNAME=:username + AND USR_PASSWORD=:password"; + $argUsernamePassword=(array('username'=>array($username,PDO::PARAM_STR), + 'password'=>array($password,PDO::PARAM_STR))); + $this->connection->execQuery($userQuery,$argUsernamePassword); $res=$this->connection->getRes(); - foreach($res as $row){ - $tabSkin[]=$row['idSkin']; + $usr=$this->convertResToUser($res); + if ($usr != null){ + $usr->listSkin=$this->getSkinList($usr->id); } - $usr->listIdSkin=$tabSkin; return $usr; } -/// Brief : Returning the last Id of the users - public function getLastId():string{ - $query = "SELECT id FROM User WHERE id >= ALL (SELECT max(id) FROM User)"; - $this->connection->execQuery($query,[]); - $res=$this->connection->getRes(); - foreach($res as $row){ - $lastId=$row['id']; - } - return $lastId; - } - /// Brief : Adding a NEW user in database /// Parameters : * $u (User): user we want to insert 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, 'S0001')"; - $query2 = "INSERT INTO Own VALUES(:id,'S0001')"; - $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)); - $arg2=array('id' => array($u->id, PDO::PARAM_STR)); - $this->connection->execQuery($query, $arg); - $this->connection->execQuery($query2,$arg2); +/// Returning TRUE if the user has been added succesfully, FALSE otherwise + public function postUser(string $username, string $password, string $nationality, string $sex, string $dateOfBirth) { + $insertUserQuery = "INSERT INTO T_S_USER_USR VALUES (NULL, :username, :password, :nationality, :sex, :dateOfBirth, 0, 0, 0, 1)"; + $argUser=array('username' => array($username, PDO::PARAM_STR), + 'password' => array($password, PDO::PARAM_STR), + 'nationality' => array($nationality, PDO::PARAM_STR), + 'sex' => array($sex, PDO::PARAM_STR), + 'dateOfBirth' => array($dateOfBirth, PDO::PARAM_STR)); + $this->connection->execQuery($insertUserQuery, $argUser); } /// Brief : Modifying an EXISTING user in database /// Parameters : * $u (User): user we want to update 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, currentSkin=:currentSkin WHERE id=:id"; - $query2="DELETE FROM Own WHERE idUser=:id"; - $query3="INSERT INTO Own VALUES(:idUsr,:idSkin)"; - $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), - ':currentSkin'=> array($u->currentSkin, PDO::PARAM_STR)); - $arg2=array('id'=>array($u->id,PDO::PARAM_STR)); - $this->connection->execQuery($query, $arg); - $this->connection->execQuery($query2,$arg2); - foreach($u->listIdSkin as $idSkin){ - $arg3=array('idUsr'=>array($u->id,PDO::PARAM_STR), - 'idSkin'=>array($idSkin,PDO::PARAM_STR)); - $this->connection->execQuery($query3,$arg3); - } - } +/// Returning TRUE if the modifications has been done succesfully, FALSE otherwise + public function putUser(int $id,string $username, string $password, int $currentBobCoins,int $totalBobCoins,int $nbGamesPlayed, int $currentSkin){ + $updateUserQuery="UPDATE T_S_USER_USR + SET USR_USERNAME = :username, + USR_PASSWORD=:password, + USR_CURRENT_NB_COINS=:currentBobCoins, + USR_TOTAL_NB_COINS=:totalBobCoins, + USR_NB_GAMES_PLAYED=:nbGamesPlayed, + FK_CURRENT_SKIN=:currentSkin + WHERE PK_ID=:id"; + $argUser=array('username' => array($username, PDO::PARAM_STR), + 'password' => array($password, PDO::PARAM_STR), + 'currentBobCoins' => array($currentBobCoins, PDO::PARAM_INT), + 'totalBobCoins' => array($totalBobCoins, PDO::PARAM_INT), + 'nbGamesPlayed' => array($nbGamesPlayed, PDO::PARAM_INT), + 'currentSkin'=> array($currentSkin, PDO::PARAM_INT), + 'id' => array($id, PDO::PARAM_INT)); + $this->connection->execQuery($updateUserQuery, $argUser); + } + +/// Brief : Adding a skin into the list of skins of the user +/// Parameter : * $u (User) : user + public function putSkinList(int $idUser, int $idSkin){ + $addSkinQuery = "INSERT INTO T_J_OWN_SKIN_OWN VALUES(:idUser,:idSkin)"; + $updateBobCoinsQuery = "UPDATE T_S_USER_USR + SET USR_CURRENT_NB_COINS = USR_CURRENT_NB_COINS - (SELECT SKI_PRICE + FROM T_H_SKIN_SKI + WHERE PK_ID=:idSkin) + WHERE PK_ID=:idUser"; + $argOwn = array('idUser'=>array($idUser,PDO::PARAM_INT), + 'idSkin'=>array($idSkin,PDO::PARAM_INT)); + $argUpdate = array('idSkin'=>array($idSkin,PDO::PARAM_INT), + 'idUser'=>array($idUser,PDO::PARAM_INT)); + $this->connection->execQuery($addSkinQuery, $argOwn); + $this->connection->execQuery($updateBobCoinsQuery,$argUpdate); + } /// Brief : Deleting an user from database -/// Parameters : * $u (User): user we want to delete from database - public function deleteUser(User $u): void{ - $query = "DELETE from User WHERE id = :id"; - $arg=array(':id' => array($u->id, PDO::PARAM_STR)); +/// Parameter : * $u (User): user we want to delete from database + public function deleteUser(int $id): void{ + $query = "DELETE from T_S_USER_USR WHERE PK_ID = :id"; + $arg=array('id' => array($id, PDO::PARAM_STR)); $this->connection->execQuery($query,$arg); } -} - -?> +} +?> \ No newline at end of file diff --git a/api-rest/index.php b/api-rest/index.php index 2862cec..05f21c5 100644 --- a/api-rest/index.php +++ b/api-rest/index.php @@ -1,5 +1,7 @@ getMessage()); http_response_code(600); // Quel code pour les erreurs PDO? } @@ -42,183 +43,196 @@ // RAPPEL POUR MOI MÊME : NE PAS OUBLIER DE FAIRE DES TRY CATCH !!!!!!! // ------ - $requestMethod = $_SERVER['REQUEST_METHOD']; - $requestName = $_REQUEST['fname']; - - if(empty($requestName)){ + $request_method = $_SERVER['REQUEST_METHOD']; + $request_uri = $_SERVER['REQUEST_URI']; + $url = rtrim($request_uri,"/"); + $url = filter_var($url, FILTER_SANITIZE_URL); + $url = explode('/', $url); + $method_name = !empty($url[2]) ? (string)$url[2] : null; + if($method_name == null){ 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 its id - $id = intval($_GET["id"]); - try{ - $res=$usergw->getUserById($id); - //retourner le résultat - } catch (PDOException $e){ - header("HTTP/1.0 ".$e->getMessage()); - http_response_code(600); // Quel code pour les erreurs PDO? - } - } - elseif (!empty($_GET["username"])){ - // read an user by his username - $username = intval($_GET["username"]); - try{ - $res=$usergw->getUserByUsername($username); - //retourner le résultat - } catch (PDOException $e){ - header("HTTP/1.0 ".$e->getMessage()); - http_response_code(600); // Quel code pour les erreurs PDO? - } - } - else{ - header("HTTP/1.0 405 Missing argument id or username"); - http_response_code(405); - } - case 'getMatch': - if(!empty($_GET["id"])){ - //read a match by its id - $id = intval($_GET["id"]); - try{ - $res=$matchgw->getMatchById($id); - //retourner le résultat - } catch (PDOException $e) { - header("HTTP/1.0 ".$e->getMessage()); - http_response_code(600); // Quel code pour les erreurs PDO? - } - } - else{ - header("HTTP/1.0 405 Missing argument id"); - http_response_code(405); - } - break; - case 'getConversation': - if(!emptyempty($_GET["id"])){ - // read conversations by the id of a user - $idUsr = intval($_GET["id"]); - try{ - $res=$conversationgw->getConversations($idUsr); - // retourner le résultat - } catch (PDOException $e) { - header("HTTP/1.0 ".$e->getMessage()); - http_response_code(600); // Quel code pour les erreurs PDO? - } - } - else{ - header("HTTP/1.0 405 Missing argument idUsr"); - http_response_code(405); - } - break; - case 'getSkin': - try{ - $res = $skingw->getSkins(); - //retourner le résultat - } catch (PDOException $e) { - header("HTTP/1.0 ".$e->getMessage()); - http_response_code(600); // Quel code pour les erreurs PDO? - } - break; - case 'getGames': - try{ - $res = $gamegw->getGames(); - //retourner le résultat - } catch (PDOException $e) { - header("HTTP/1.0 ".$e->getMessage()); - http_response_code(600); // Quel code pour les erreurs PDO? - } - break; - default: - header("HTTP/1.0 406 unknown method"); - http_response_code(406); // Le bon code ? - break; + switch ($request_method){ + case 'GET': + if($method_name === "getUserById"){ // test : OK + if(empty($url[3])){ + header("HTTP/1.0 400 Id not given"); + http_response_code(400); + } else{ + $id = (string)$url[3]; + $user = $usergw->getUserById($id); + echo json_encode($user); } - break; - - case 'POST': - switch ($requestName){ - case 'postUser': - if(!empty($_POST["id"])){ - $usr = new User($_POST["id"],$_POST["username"],$_POST["password"],$_POST["nationality"],$_POST["sex"],$_POST["dateOfBirth"],0,0,0,"S0001",[]); - try{ - $usergw->postUser($usr); - http_response_code(200); - } catch (PDOException $e) { - header("HTTP/1.0 ".$e->getMessage()); - http_response_code(600); // Quel code pour les erreurs PDO? - } - } - else{ - header("HTTP/1.0 405 Missing user to create"); - http_response_code(405); - } - break; - case 'postMatch': - if(!empty($_POST["id"])){ - $match = new Match($_POST["id"],false,$_POST["idGame"],$_POST["idUsr"]); - try{ - $matchgw->postMatch($match); - http_response_code(200); - } catch (PDOException $e) { - header("HTTP/1.0 ".$e->getMessage()); - http_response_code(600); // Quel code pour les erreurs PDO? - } - } - - break; - case 'postMessage': - - break; - case 'postConversation ': - - break; + } + elseif($method_name === "getUserByUsername"){ // test : OK + $username = !empty($url[3]) ? (string) $url[3] : null; + if ($username !== null){ + $user =$usergw->getUserByUsername($username); + echo json_encode($user); + } else{ + header("HTTP/1.0 400 Username not given"); + http_response_code(400); + } + } + elseif($method_name === "getUserForConnection"){ // test : OK + $username = !empty($url[3]) ? (string) $url[3] : null; + $password = !empty($url[4]) ? (string) $url[4] : null; + if ($username != null || $password != null){ + $user =$usergw->getUserForConnection($username,$password); + echo json_encode($user); + } else{ + header("HTTP/1.0 400 Username or password not given"); + http_response_code(400); } - break; - - case 'PUT': - switch ($requestName){ - case 'putUser': - - break; - case 'putMatch': - - break; - case 'putMessage': - - break; - case 'putConversation ': - - break; + } + elseif($method_name === "getSkins"){ // test : OK + $tabSkin = $skingw->getSkins(); + echo json_encode($tabSkin); + } + elseif($method_name === "getGames"){ // test : OK + $tabGame = $gamegw->getGames(); + echo json_encode($tabGame); + } + elseif($method_name === "getGameById"){ // test : OK + $id = !empty($url[3]) ? (string) $url[3] : null; + if ($id !== null){ + $game = $gamegw->getGameById($id); + echo json_encode($game); + } else{ + header("HTTP/1.0 400 Id not given"); + http_response_code(400); + } + } + elseif($method_name === "getMatchById"){ // test : OK + $id = !empty($url[3]) ? (string) $url[3] : null; + if ($id !== null){ + $match = $matchgw->getMatchById($id); + echo json_encode($match); + } else{ + header("HTTP/1.0 400 Id not given"); + http_response_code(400); + } + } + else{ + header("HTTP/1.0 401 UNAUTHORIZED REQUEST"); + http_response_code(401); + } + case 'POST': + if($method_name === "postUser"){ // test : OK + if (count($url)<8){ + header("HTTP/1.0 400 Invalid number of arguments"); + http_response_code(400); } - break; - - case 'DELETE': - switch ($requestName){ - case 'delUser': - - break; - case 'delMatch': - - break; - case 'delMessage': - - break; - case 'delConversation ': - - break; + $username = !empty($url[3]) ? (string) $url[3] : null; + $password = !empty($url[4]) ? (string) $url[4] : null; + $nationality = !empty($url[5]) ? (string) $url[5] : null; + $sex = !empty($url[6]) ? (string) $url[6] : null; + $dateOfBirth = !empty($url[7]) ? (string) $url[7] : null; + $usergw->postUser($username,$password,$nationality,$sex,$dateOfBirth); + } + elseif($method_name === "postMatch"){ // test : OK + $idGame = !empty($url[3]) ? (string) $url[3] : null; + $idCreator = !empty($url[4]) ? (string) $url[4] : null; + if ($idGame != null || $idCreator != null){ + $match =$matchgw->postMatch($idGame,$idCreator); + } else{ + header("HTTP/1.0 400 Username or password not given"); + http_response_code(400); } - break; - default : - // Invalid request - header("HTTP/1.0 405 Request Name Empty"); - http_response_code(405); - break; - } - + } + else{ + header("HTTP/1.0 401 UNAUTHORIZED REQUEST"); + http_response_code(401); + } + break; + case 'PUT': + if($method_name === "putUser"){ // test : OK + if (count($url)<9){ + header("HTTP/1.0 400 Invalid number of arguments"); + http_response_code(400); + } + $id = !empty($url[3]) ? (string) $url[3] : null; + $username = !empty($url[4]) ? (string) $url[4] : null; + $password = !empty($url[5]) ? (string) $url[5] : null; + $nbCurrentCoins = !empty($url[6]) ? (string) $url[6] : null; + $totalnbCoins = !empty($url[7]) ? (string) $url[7] : null; + $nbGames = !empty($url[8]) ? (string) $url[8] : null; + $currentSkin = !empty($url[9]) ? (string) $url[9] : null; + $usergw->putUser($id,$username,$password,$nbCurrentCoins,$totalnbCoins,$nbGames,$currentSkin); + } + elseif($method_name === "putSkinList"){ // test : OK + $idUser = !empty($url[3]) ? (string) $url[3] : null; + $idSkin = !empty($url[4]) ? (string) $url[4] : null; + if ($idUser != null || $idSkin != null){ + $usergw->putSkinList($idUser,$idSkin); + } else{ + header("HTTP/1.0 400 idSkin or idUser not given"); + http_response_code(400); + } + } + elseif($method_name === "putMatch"){ // test : OK + $id = !empty($url[3]) ? (string) $url[3] : null; + if ($id !== null){ + $matchgw->putMatch($id); + } else{ + header("HTTP/1.0 400 Id not given"); + http_response_code(400); + } + } + elseif($method_name === "addUserToMatch"){ // test : OK + $idMatch = !empty($url[3]) ? (string) $url[3] : null; + $idUser = !empty($url[4]) ? (string) $url[4] : null; + if ($idUser != null || $idMatch != null){ + $matchgw->addUserToMatch($idMatch,$idUser); + } else{ + header("HTTP/1.0 400 idSkin or idUser not given"); + http_response_code(400); + } + } + elseif($method_name === "deleteUserFromMatch"){ // test : OK + $idUser = !empty($url[3]) ? (string) $url[3] : null; + if ($idUser != null){ + $matchgw->deleteUserFromMatch($idUser); + } else{ + header("HTTP/1.0 400 idSkin or idUser not given"); + http_response_code(400); + } + } + else{ + header("HTTP/1.0 401 UNAUTHORIZED REQUEST"); + http_response_code(401); + } + break; + case 'DELETE': + if($method_name === "deleteUser"){ // test : OK + $id = !empty($url[3]) ? (string) $url[3] : null; + if($id!=null){ + $usergw->deleteUser($id); + }else{ + header("HTTP/1.0 400 Id not given"); + http_response_code(400); + } + } + elseif($method_name == "deleteMatch"){ // test : + $id = !empty($url[3]) ? (string) $url[3] : null; + if($id!=null){ + $matchgw->deleteMatch($id); + }else{ + header("HTTP/1.0 400 Id not given"); + http_response_code(400); + } + } + else{ + header("HTTP/1.0 401 UNAUTHORIZED REQUEST"); + http_response_code(401); + } + break; + default : + header("HTTP/1.0 405 Invalid request method"); + http_response_code(405); + break; } + ?> \ No newline at end of file diff --git a/api-rest/model/game.php b/api-rest/model/game.php index 64fedd3..3870ac2 100644 --- a/api-rest/model/game.php +++ b/api-rest/model/game.php @@ -1,12 +1,12 @@ id=$_id; $this->name=$_name; $this->image=$_image; diff --git a/api-rest/model/skin.php b/api-rest/model/skin.php index f4ce795..ac41c1d 100644 --- a/api-rest/model/skin.php +++ b/api-rest/model/skin.php @@ -2,14 +2,16 @@ class Skin{ - public string $id; + public int $id; public string $name; public string $image; + public int $price; - public function __construct(string $_id, string $_name, string $_image){ + public function __construct(int $_id, string $_name, string $_image, int $_price){ $this->id=$_id; $this->name=$_name; $this->image=$_image; + $this->price=$_price; } } diff --git a/api-rest/model/user.php b/api-rest/model/user.php index 2ef6275..d30e093 100644 --- a/api-rest/model/user.php +++ b/api-rest/model/user.php @@ -3,7 +3,7 @@ class User { // Object attributes - public string $id; + public int $id; public string $username; public string $password; public string $nationality; @@ -12,10 +12,10 @@ class User { public int $currentBobCoins; public int $totalBobCoins; public int $nbGamesPlayed; - public string $currentSkin; - public $listIdSkin; + public int $currentSkin; + public ?array $listSkin; - public function __construct(string $_id,string $_username,string $_password, string $_nationality,string $_sex, string $_dateOfBirth, int $_currentBobCoins, int $_totalBobCoins, int $_nbGamesPlayed, string $_currentSkin, $_listIdSkin){ + public function __construct(int $_id,string $_username,string $_password, string $_nationality,string $_sex, string $_dateOfBirth, int $_currentBobCoins, int $_totalBobCoins, int $_nbGamesPlayed, int $_currentSkin,?array $_listSkin){ $this->id=$_id; $this->username=$_username; $this->password=$_password; @@ -26,7 +26,7 @@ class User { $this->totalBobCoins=$_totalBobCoins; $this->nbGamesPlayed=$_nbGamesPlayed; $this->currentSkin=$_currentSkin; - $this->listIdSkin=$_listIdSkin; + $this->listSkin=$_listSkin; } } diff --git a/db-config.sql b/db-config.sql new file mode 100644 index 0000000..d8fe179 --- /dev/null +++ b/db-config.sql @@ -0,0 +1,152 @@ +/* This script does: + + * create tables of the database + * creates the sequences for the ids(with AUTO_INCREMENT) + * create the triggers and trigger functions + +*/ + +/* ----------------------------------- */ + +/* TABLES' CREATION */ + +/* ----------------------------------- */ + +/* ----- ENTITIES TABLES -----*/ + +/* -- Table User -- */ +CREATE TABLE T_S_USER_USR ( + PK_ID int AUTO_INCREMENT PRIMARY KEY, + USR_USERNAME varchar(50) UNIQUE NOT NULL, + USR_PASSWORD varchar(50) NOT NULL, + USR_NATIONALITY varchar(20) NOT NULL, + USR_SEX char(1) NOT NULL, + USR_DATE_OF_BIRTH date, + USR_CURRENT_NB_COINS int DEFAULT 0, + USR_TOTAL_NB_COINS int DEFAULT 0, + USR_NB_GAMES_PLAYED int DEFAULT 0, + FK_CURRENT_SKIN int + REFERENCES T_H_SKIN_SKI(PK_ID) +); + +/* -- Table Skin -- */ +CREATE TABLE T_H_SKIN_SKI ( + PK_ID int AUTO_INCREMENT PRIMARY KEY, + SKI_NAME varchar(50) UNIQUE NOT NULL, + SKI_IMAGE varchar(50) UNIQUE NOT NULL, + SKI_PRICE varchar(30) +); + +/* -- Table Game -- */ +CREATE TABLE T_E_GAME_GAM ( + PK_ID int AUTO_INCREMENT PRIMARY KEY, + GAM_NAME varchar(50) UNIQUE, + GAM_IMAGE varchar(50) UNIQUE +); + +/* -- Table Match -- */ +CREATE TABLE T_E_MATCH_MTC ( + PK_ID int AUTO_INCREMENT PRIMARY KEY, + MTC_IN_GAME boolean, + FK_ID_GAME int + REFERENCES T_E_GAME_GAM(PK_ID) +); + +/* -- Table Conversation -- */ +CREATE TABLE T_H_CONVERSATION_COV ( + PK_ID int AUTO_INCREMENT PRIMARY KEY, + COV_NAME varchar(20) +); + +/* -- Table Message -- */ +CREATE TABLE T_H_MESSAGE_MSG ( + PK_ID int AUTO_INCREMENT PRIMARY KEY, + MSG_MESSAGE text, + FK_SENDER int + REFERENCES T_S_USER_USR(PK_ID) +); + +/* ----- JUNCTURE TABLES ----- */ + +/* -- Juncture own skin -- */ +CREATE TABLE T_J_OWN_SKIN_OWN ( + FK_USER int , + FOREIGN KEY (FK_USER) + REFERENCES T_S_USER_USR(PK_ID) + ON DELETE CASCADE, + FK_SKIN int , + FOREIGN KEY (FK_SKIN) + REFERENCES T_H_SKIN_SKI(PK_ID), + PRIMARY KEY(FK_SKIN, FK_USER) +); + +/* -- Juncture play match -- */ +CREATE TABLE T_J_PLAY_MATCH_PLM ( + FK_USER int , + FOREIGN KEY (FK_USER ) + REFERENCES T_S_USER_USR(PK_ID) + ON DELETE CASCADE, + FK_MATCH int , + FOREIGN KEY (FK_MATCH) + REFERENCES T_E_MATCH_MTC(PK_ID) + ON DELETE CASCADE, + PRIMARY KEY (FK_USER,FK_MATCH) +); + +/* -- Juncture discuss -- */ +CREATE TABLE T_J_DISCUSS_DIS ( + FK_USER int , + FOREIGN KEY (FK_USER) + REFERENCES T_S_USER_USR(PK_ID) + ON DELETE CASCADE, + FK_CONVERSATION int , + FOREIGN KEY (FK_CONVERSATION) + REFERENCES T_H_CONVERSATION_COV(PK_ID) + ON DELETE CASCADE, + PRIMARY KEY(FK_USER,FK_CONVERSATION) +); + +/* -- Juncture contain message -- */ +CREATE TABLE T_J_CONTAIN_MESSAGE_CMG ( + FK_CONVERSATION int, + FOREIGN KEY (FK_CONVERSATION) + REFERENCES T_H_CONVERSATION_COV(PK_ID) + ON DELETE CASCADE, + FK_MESSAGE int, + FOREIGN KEY (FK_MESSAGE) + REFERENCES T_H_MESSAGE_MSG(PK_ID) + ON DELETE CASCADE, + PRIMARY KEY (FK_CONVERSATION,FK_MESSAGE) +); + + +/* ----------------------------------- */ + +/* TRIGGERS' CREATION */ + +/* ----------------------------------- */ + +/* ----- USER's trigger ----- */ + +/* -- after insert -> add basic skin into the list of skin -- */ +CREATE TRIGGER after_insert_user + AFTER INSERT + ON T_S_USER_USR +FOR EACH ROW + INSERT INTO T_J_OWN_SKIN_OWN VALUES(NEW.PK_ID,1); + + + +/* ----- CONVERSATION's trigger ----- */ + +CREATE TRIGGER before_delete_conversation + BEFORE DELETE + ON T_H_CONVERSATION_COV +FOR EACH ROW + r record; + FOR r in (SELECT c.PK_ID + FROM T_H_MESSAGE_MSG m, T_J_CONTAIN_MESSAGE c + WHERE m.PK_ID = c.FK_MESSAGE + AND c.FK_CONVERSATION=NEW.PK_ID) LOOP + DELETE FROM T_H_MESSAGE_MSG WHERE PK_ID = r.PK_ID; + END LOOP; From a21e2a93d8273752caf546097650d29f2f71c500 Mon Sep 17 00:00:00 2001 From: Lucie Bedouret Date: Tue, 6 Dec 2022 17:55:07 +0100 Subject: [PATCH 12/14] =?UTF-8?q?ADD=20:=20toutes=20les=20m=C3=A9thodes=20?= =?UTF-8?q?et=20routing=20fonctionnels?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- api-rest/gateways/conversationGataway.php | 159 ++++++++++++---------- api-rest/gateways/matchGateway.php | 2 +- api-rest/index.php | 76 ++++++++++- db-config.sql | 10 +- 4 files changed, 161 insertions(+), 86 deletions(-) diff --git a/api-rest/gateways/conversationGataway.php b/api-rest/gateways/conversationGataway.php index 0d42550..51d6ef7 100644 --- a/api-rest/gateways/conversationGataway.php +++ b/api-rest/gateways/conversationGataway.php @@ -31,38 +31,43 @@ class ConversationGateway{ $tabConversations=NULL; $tabUsers=NULL; $tabMessages=NULL; - $conversationQuery = "SELECT c.id, c.nom - FROM T_E_CONVERSATION_COV c, T_J_DISCUTE_DIS d - WHERE c.id=d.idConv - AND d.idUser=:idUser"; - $messagesQuery = "SELECT m.id, m.message, m.idSender - FROM T_R_MESSAGE_MSG m, T_J_DISCUTE_DIS d - WHERE m.id=h.idMessage - AND h.idConv=:idConv"; - $usersQuery = "SELECT d.idUser - FROM T_J_DISCUTE_DIS d - WHERE d.idConv = :idConv"; + $conversationQuery = "SELECT c.PK_ID, c.COV_NAME + FROM T_H_CONVERSATION_COV c, T_J_DISCUSS_DIS d + WHERE c.PK_ID=d.FK_CONVERSATION + AND d.FK_USER=:idUser"; + $messagesQuery = "SELECT m.PK_ID, m.MSG_MESSAGE, m.FK_SENDER + FROM T_H_MESSAGE_MSG m, T_J_CONTAIN_MESSAGE_CMG c + WHERE m.PK_ID=c.FK_MESSAGE + AND c.FK_CONVERSATION=:idConv"; + $usersQuery = "SELECT d.FK_USER + FROM T_J_DISCUSS_DIS d + WHERE d.FK_CONVERSATION = :idConv"; //Find all the conversations where the user belong - $argIdUser=array('idUser'=>array($_idUser, PDO::PARAM_STR)); + $argIdUser=array('idUser'=>array($_idUser, PDO::PARAM_INT)); $this->connection->execQuery($conversationQuery,$argIdUser); $res=$this->connection->getRes(); foreach($res as $row){ - $argIdConv= array('idConv'=>array($row['idConversation'], PDO::PARAM_STR)); + $argIdConv= array('idConv'=>array($row['PK_ID'], PDO::PARAM_INT)); // Find all messages of the conversation $this->connection->execQuery($messagesQuery,$argIdConv); $resMessages=$this->connection->getRes(); foreach($resMessages as $rowMessages){ - $tabUsers[] = new Message($rowMessages['id'],$rowMessages['message'],$rowMessages['idSender']); + $tabUsers[] = new Message($rowMessages['PK_ID'], + $rowMessages['MSG_MESSAGE'], + $rowMessages['FK_SENDER']); } // Find all the users in the conversation $this->connection->execQuery($usersQuery,$argIdConv); $resUsers=$this->connection->getRes(); foreach($resUsers as $rowUsers){ - $tabUsers[] = $rowUsers['idUser']; + $tabUsers[] = $rowUsers['FK_USER']; } // Add the conversation into the array - $tabConversations = new Conversation($row['id'],$row['nom'],$tabMessages,$tabUsers); + $tabConversations[] = new Conversation($row['PK_ID'], + $row['COV_NAME'], + $tabMessages, + $tabUsers); // Restore the arrays $tabUsers=array(); $tabMessages=array(); @@ -71,76 +76,80 @@ class ConversationGateway{ } /// Brief : Adding a new conversation in database -/// Parameters : * $c (Conversation): conversation we want to insert in database -/// ***** CRÉER DES TRIGGERS ***** /// - public function postConversation(Conversation $c): void{ + public function postConversation(string $name, int $idUser): void{ // Declare queries - $convCreationQuery = "INSERT INTO T_E_CONVERSATION_COV VALUES(:idConv,:name)"; - $addUserInConvQuery = "INSERT INTO T_J_DISCUTE_DIS VALUES(:idUser,:idConv)"; - $argconvCreationQuery = array('idConv'=>array($c->id,PDO::PARAM_STR), - 'name'=>array($c->name, PDO::PARAM_STR)); + $convCreationQuery = "INSERT INTO T_H_CONVERSATION_COV VALUES(NULL,:name)"; + $addUserInConvQuery = "INSERT INTO T_J_DISCUSS_DIS VALUES(:idUser,:idConv)"; + $argconvCreationQuery = array('name'=>array($name, PDO::PARAM_STR)); // Create a new conversation $this->connection->execQuery($convCreationQuery,$argconvCreationQuery); - // Add users of the conversation in the conversation - foreach($c->listIdUsers as $idUsr){ - $argUserInConvQuery = array('idUser'=>array($idUsr, PDO::PARAM_STR), - 'idConv'=>array($c->id, PDO::PARAM_STR)); - $this->connection->execQuery($query2,$arg2); + $this->connection->execQuery("SELECT PK_ID + FROM T_H_CONVERSATION_COV + WHERE PK_ID >= ALL (SELECT max(c2.PK_ID) + FROM T_H_CONVERSATION_COV c2)",[]); + $res=$this->connection->getRes(); + foreach($res as $row){ + $id=$row['PK_ID']; + } + $argUserInConvQuery = array('idUser'=>array($idUser, PDO::PARAM_INT), + 'idConv'=>array($id, PDO::PARAM_INT)); + $this->connection->execQuery($addUserInConvQuery,$argUserInConvQuery); } + + +/// Brief : Modifying an EXISTING conversation in database + public function putConversation(int $id, string $name):void{ + $conversationUpdateQuery = "UPDATE T_H_CONVERSATION_COV + SET COV_NAME=:name + WHERE PK_ID=:id"; + $argConversationUpdate = array('name'=>array($name, PDO::PARAM_STR), + 'id'=>array($id,PDO::PARAM_INT)); + $this->connection->execQuery($conversationUpdateQuery,$argConversationUpdate); } -/// Brief : Modifying an EXISTING match in database -/// Parameters : * $u (Matchs): match we want to update in database -/// ***** CRÉER DES TRIGGERS ***** /// - public function putConversation(Conversation $c):void{ - // Declare the queries - $conversationInsertionQuery = "INSERT INTO T_E_CONVERSATION_COV VALUES (:id,:nom)"; - $messageInsertionQuery = "INSERT INTO T_R_MESSAGE_MSG VALUES(:id,:message,:idSender)"; - $discuteInsertionQuery = "INSERT INTO T_J_DISCUTE_DIS VALUES(:idUser,:idConv)"; - $containInsertionQuery = "INSERT INTO T_J_CONTAIN_MESSAGE_CTN VALUES(:idConv,:idMessage)"; - $argConversationInsertion = array('id'=>array($c->id, PDO::PARAM_STR), - 'nom'=>array($c->name,PDO::PARAM_STR)); - // Delete current data from database - deleteConversation($c); - // Add conversation - $this->connection->execQuery($conversationInsertionQuery,$argConversationInsertion); - // Add messages to conversation - foreach($c->listMessages as $msg){ - $argContainInsertion = array('idConv'=>array($c->id,PDO::PARAM_STR), - 'idMessage'=>array($msg->id,PDO::PARAM_STR)); - $argMessageInsertion = array('id'=>array($msg->id,PDO::PARAM_STR), - 'message'=>array($msg->message,PDO::PARAM_STR), - 'idSender'=>array($msg->idSender,PDO::PARAM_STR)); - $this->connection->execQuery($containInsertionQuery,$argContainInsertion); - $this->connection->execQuery($messageInsertionQuery,$argMessageInsertion); - } - // Add user to conversation - foreach($c->listIdUsers as $idUsr){ - $argDiscuteInsertion = array('idUsr'=>array($idUsr,PDO::PARAM_STR), - 'idConv'=>array($c->id,PDO::PARAM_STR)); - $this->connection->execQuery($discuteInsertionQuery,$argDiscuteInsertion); +/// Brief : Adding an user to a conversation + public function addUserToConversation(int $idConv, int $idUser){ + $insertUserQuery = "INSERT INTO T_J_DISCUSS_DIS VALUES(:idUser,:idConv)"; + $argQuery = array('idUser'=>array($idUser,PDO::PARAM_INT), + 'idConv'=>array($idConv,PDO::PARAM_INT)); + $this->connection->execQuery($insertUserQuery,$argQuery); + } + +/// Brief : Deleting an user from a conversation +public function deleteUserFromConversation(int $idConv, int $idUser){ + $insertUserQuery = "DELETE FROM T_J_DISCUSS_DIS WHERE FK_USER=:idUser AND FK_CONVERSATION=:idConv"; + $argQuery = array('idUser'=>array($idUser,PDO::PARAM_INT), + 'idConv'=>array($idConv,PDO::PARAM_INT)); + $this->connection->execQuery($insertUserQuery,$argQuery); +} + +/// Brief : adding a new message into a conversation + public function addMessageToConversation(string $message, int $idSender, int $idConv){ + $insertMessageQuery = "INSERT INTO T_H_MESSAGE_MSG VALUES(NULL,:message,:idSender)"; + $insertMsgInConvQuery = "INSERT INTO T_J_CONTAIN_MESSAGE_CMG VALUES(:idConv,:idMessage)"; + + $argInsertMessage= array('message'=>array($message,PDO::PARAM_STR), + 'idSender'=>array($idSender,PDO::PARAM_INT)); + $this->connection->execQuery($insertMessageQuery,$argInsertMessage); + $this->connection->execQuery("SELECT PK_ID + FROM T_H_MESSAGE_MSG + WHERE PK_ID >= ALL (SELECT max(m2.PK_ID) + FROM T_H_MESSAGE_MSG m2)",[]); + $res=$this->connection->getRes(); + foreach($res as $row){ + $idMsg=$row['PK_ID']; } + $argMsgInConv = array('idConv'=>array($idConv,PDO::PARAM_INT), + 'idMessage'=>array($idMsg,PDO::PARAM_INT)); + $this->connection->execQuery($insertMsgInConvQuery,$argMsgInConv); } /// Brief : Deleting a conversation and its messages from database -/// Parameters : * $c (Conversation): conversation we want to delete from database -// ---- -// Ne pas oublier le on delete cascade dans la création des tables -// Créer des triggers -// ---- - public function deleteConversation(Conversation $c):void{ - // Declare query and argument table - $deleteMessagesQuery = "DELETE FROM T_R_MESSAGE_MSG - WHERE id=(SELECT id - FROM T_R_MESSAGE_MSG m, T_J_CONTAIN_MESSAGE_CTN c - WHERE m.id = c.idConversation - AND c.idConversation=:idConv"; - $deleteConv = "DELETE FROM T_E_CONVERSATION_COV - WHERE id=:idConv"; // Suffisant grâce au on delete cascade (à ne pas oublier) - $argIdConv = array('idConv'=>array($c->id,PDO::PARAM_STR)); - // Executing queries - $this->connection->execQuery($deleteMessagesQuery,$argIdConv); + public function deleteConversation(int $id):void{ + $deleteConv = "DELETE FROM T_H_CONVERSATION_COV + WHERE PK_ID=:idConv"; + $argIdConv = array('idConv'=>array($id,PDO::PARAM_INT)); $this->connection->execQuery($deleteConv,$argIdConv); } } diff --git a/api-rest/gateways/matchGateway.php b/api-rest/gateways/matchGateway.php index 202a85f..3a798b9 100644 --- a/api-rest/gateways/matchGateway.php +++ b/api-rest/gateways/matchGateway.php @@ -88,7 +88,7 @@ class MatchGateway{ /// Brief : Deleting a match from database /// Parameters : * $u (Matchs): match we want to delete from database public function deleteMatch(int $id){ - $query="DELETE FROM T_J_PLAY_MATCH_PLM WHERE PK_ID=:id"; + $query="DELETE FROM T_E_MATCH_MTC WHERE PK_ID=:id"; $arg=array('id'=>array($id, PDO::PARAM_INT)); $this->connection->execQuery($query,$arg); } diff --git a/api-rest/index.php b/api-rest/index.php index 05f21c5..ea0c8a1 100644 --- a/api-rest/index.php +++ b/api-rest/index.php @@ -114,6 +114,16 @@ http_response_code(400); } } + elseif($method_name === "getConversations"){ // tests : OK + $id = !empty($url[3]) ? (string) $url[3] : null; + if ($id !== null){ + $conversations = $conversationgw->getConversations($id); + echo json_encode($conversations); + } else{ + header("HTTP/1.0 400 Id not given"); + http_response_code(400); + } + } else{ header("HTTP/1.0 401 UNAUTHORIZED REQUEST"); http_response_code(401); @@ -137,7 +147,17 @@ if ($idGame != null || $idCreator != null){ $match =$matchgw->postMatch($idGame,$idCreator); } else{ - header("HTTP/1.0 400 Username or password not given"); + header("HTTP/1.0 400 idGame or idCreator not given"); + http_response_code(400); + } + } + elseif($method_name === "postConversation"){ // test : OK + $name = !empty($url[3]) ? (string) $url[3] : null; + $idCreator = !empty($url[4]) ? (string) $url[4] : null; + if ($name != null || $idCreator != null){ + $conversationgw->postConversation($name,$idCreator); + } else{ + header("HTTP/1.0 400 name or creator not given"); http_response_code(400); } } @@ -195,7 +215,48 @@ if ($idUser != null){ $matchgw->deleteUserFromMatch($idUser); } else{ - header("HTTP/1.0 400 idSkin or idUser not given"); + header("HTTP/1.0 400 idUser not given"); + http_response_code(400); + } + } + elseif($method_name === "putConversation"){ // test : OK + $id = !empty($url[3]) ? (string) $url[3] : null; + $newName = !empty($url[4]) ? (string) $url[4] : null; + if ($id != null && $newName != null){ + $conversationgw->putConversation($id,$newName); + } else{ + header("HTTP/1.0 400 id or new name not given"); + http_response_code(400); + } + } + elseif($method_name === "addUserToConversation"){ // test : OK + $idConv = !empty($url[3]) ? (string) $url[3] : null; + $idUser = !empty($url[4]) ? (string) $url[4] : null; + if ($idConv != null && $idUser != null){ + $conversationgw->addUserToConversation($idConv,$idUser); + } else{ + header("HTTP/1.0 400 id conv or id user not given"); + http_response_code(400); + } + } + elseif($method_name === "deleteUserFromConversation"){ // test : OK + $idConv = !empty($url[3]) ? (string) $url[3] : null; + $idUser = !empty($url[4]) ? (string) $url[4] : null; + if ($idConv != null && $idUser != null){ + $conversationgw->deleteUserFromConversation($idConv,$idUser); + } else{ + header("HTTP/1.0 400 id conv or id user not given"); + http_response_code(400); + } + } + elseif($method_name === "addMessageToConversation"){ // test : OK + $msg=!empty($url[3]) ? (string) $url[3] : null; + $idSender=!empty($url[4]) ? (int) $url[4] : null; + $idConv=!empty($url[5]) ? (int) $url[5] : null; + if ($msg != null && $idSender != null && $idConv != null){ + $conversationgw->addMessageToConversation($msg,$idSender,$idConv); + } else{ + header("HTTP/1.0 400 id conv or message or sender not given"); http_response_code(400); } } @@ -214,7 +275,7 @@ http_response_code(400); } } - elseif($method_name == "deleteMatch"){ // test : + elseif($method_name == "deleteMatch"){ // test : OK $id = !empty($url[3]) ? (string) $url[3] : null; if($id!=null){ $matchgw->deleteMatch($id); @@ -223,6 +284,15 @@ http_response_code(400); } } + elseif($method_name === "deleteConversation"){ // test : OK + $id = !empty($url[3]) ? (string) $url[3] : null; + if($id!=null){ + $conversationgw->deleteConversation($id); + }else{ + header("HTTP/1.0 400 Id not given"); + http_response_code(400); + } + } else{ header("HTTP/1.0 401 UNAUTHORIZED REQUEST"); http_response_code(401); diff --git a/db-config.sql b/db-config.sql index d8fe179..0f2978c 100644 --- a/db-config.sql +++ b/db-config.sql @@ -143,10 +143,6 @@ CREATE TRIGGER before_delete_conversation BEFORE DELETE ON T_H_CONVERSATION_COV FOR EACH ROW - r record; - FOR r in (SELECT c.PK_ID - FROM T_H_MESSAGE_MSG m, T_J_CONTAIN_MESSAGE c - WHERE m.PK_ID = c.FK_MESSAGE - AND c.FK_CONVERSATION=NEW.PK_ID) LOOP - DELETE FROM T_H_MESSAGE_MSG WHERE PK_ID = r.PK_ID; - END LOOP; + DELETE FROM T_H_MESSAGE_MSG WHERE PK_ID = (SELECT FK_MESSAGE + FROM T_J_CONTAIN_MESSAGE_CMG + WHERE FK_CONVERSATION=OLD.PK_ID); From f780e796cbac4e471bd5e41f42bbb24fbb7e194d Mon Sep 17 00:00:00 2001 From: Lucie Bedouret Date: Tue, 6 Dec 2022 22:34:18 +0100 Subject: [PATCH 13/14] Changements sur l'API --- api-rest/index.php | 99 ++++++++++++++++++++++++++++++---------------- 1 file changed, 65 insertions(+), 34 deletions(-) diff --git a/api-rest/index.php b/api-rest/index.php index ea0c8a1..bb41cb1 100644 --- a/api-rest/index.php +++ b/api-rest/index.php @@ -52,6 +52,7 @@ if($method_name == null){ header("HTTP/1.0 400 Request Name Empty"); http_response_code(400); + } switch ($request_method){ case 'GET': @@ -60,16 +61,19 @@ header("HTTP/1.0 400 Id not given"); http_response_code(400); } else{ - $id = (string)$url[3]; + $id = (int)$url[3]; $user = $usergw->getUserById($id); - echo json_encode($user); + header('Content-Type: application/json'); + echo json_encode($user, JSON_PRETTY_PRINT); + http_response_code(200); } } elseif($method_name === "getUserByUsername"){ // test : OK $username = !empty($url[3]) ? (string) $url[3] : null; if ($username !== null){ $user =$usergw->getUserByUsername($username); - echo json_encode($user); + header('Content-Type: application/json'); + echo json_encode($user, JSON_PRETTY_PRINT); } else{ header("HTTP/1.0 400 Username not given"); http_response_code(400); @@ -80,7 +84,9 @@ $password = !empty($url[4]) ? (string) $url[4] : null; if ($username != null || $password != null){ $user =$usergw->getUserForConnection($username,$password); - echo json_encode($user); + header('Content-Type: application/json'); + echo json_encode($user, JSON_PRETTY_PRINT); + http_response_code(200); } else{ header("HTTP/1.0 400 Username or password not given"); http_response_code(400); @@ -88,37 +94,47 @@ } elseif($method_name === "getSkins"){ // test : OK $tabSkin = $skingw->getSkins(); - echo json_encode($tabSkin); + header('Content-Type: application/json'); + echo json_encode($tabSkin, JSON_PRETTY_PRINT); + http_response_code(200); } elseif($method_name === "getGames"){ // test : OK $tabGame = $gamegw->getGames(); - echo json_encode($tabGame); + header('Content-Type: application/json'); + echo json_encode($tabGame, JSON_PRETTY_PRINT); + http_response_code(200); } elseif($method_name === "getGameById"){ // test : OK - $id = !empty($url[3]) ? (string) $url[3] : null; + $id = !empty($url[3]) ? (int) $url[3] : null; if ($id !== null){ $game = $gamegw->getGameById($id); - echo json_encode($game); + header('Content-Type: application/json'); + echo json_encode($game, JSON_PRETTY_PRINT); + http_response_code(200); } else{ header("HTTP/1.0 400 Id not given"); http_response_code(400); } } elseif($method_name === "getMatchById"){ // test : OK - $id = !empty($url[3]) ? (string) $url[3] : null; + $id = !empty($url[3]) ? (int) $url[3] : null; if ($id !== null){ $match = $matchgw->getMatchById($id); - echo json_encode($match); + header('Content-Type: application/json'); + echo json_encode($match, JSON_PRETTY_PRINT); + http_response_code(200); } else{ header("HTTP/1.0 400 Id not given"); http_response_code(400); } } elseif($method_name === "getConversations"){ // tests : OK - $id = !empty($url[3]) ? (string) $url[3] : null; + $id = !empty($url[3]) ? (int) $url[3] : null; if ($id !== null){ $conversations = $conversationgw->getConversations($id); - echo json_encode($conversations); + header('Content-Type: application/json'); + echo json_encode($conversations, JSON_PRETTY_PRINT); + http_response_code(200); } else{ header("HTTP/1.0 400 Id not given"); http_response_code(400); @@ -140,12 +156,14 @@ $sex = !empty($url[6]) ? (string) $url[6] : null; $dateOfBirth = !empty($url[7]) ? (string) $url[7] : null; $usergw->postUser($username,$password,$nationality,$sex,$dateOfBirth); + http_response_code(200); } elseif($method_name === "postMatch"){ // test : OK - $idGame = !empty($url[3]) ? (string) $url[3] : null; - $idCreator = !empty($url[4]) ? (string) $url[4] : null; + $idGame = !empty($url[3]) ? (int) $url[3] : null; + $idCreator = !empty($url[4]) ? (int) $url[4] : null; if ($idGame != null || $idCreator != null){ $match =$matchgw->postMatch($idGame,$idCreator); + http_response_code(200); } else{ header("HTTP/1.0 400 idGame or idCreator not given"); http_response_code(400); @@ -153,9 +171,10 @@ } elseif($method_name === "postConversation"){ // test : OK $name = !empty($url[3]) ? (string) $url[3] : null; - $idCreator = !empty($url[4]) ? (string) $url[4] : null; + $idCreator = !empty($url[4]) ? (int) $url[4] : null; if ($name != null || $idCreator != null){ $conversationgw->postConversation($name,$idCreator); + http_response_code(200); } else{ header("HTTP/1.0 400 name or creator not given"); http_response_code(400); @@ -172,78 +191,86 @@ header("HTTP/1.0 400 Invalid number of arguments"); http_response_code(400); } - $id = !empty($url[3]) ? (string) $url[3] : null; + $id = !empty($url[3]) ? (int) $url[3] : null; $username = !empty($url[4]) ? (string) $url[4] : null; $password = !empty($url[5]) ? (string) $url[5] : null; - $nbCurrentCoins = !empty($url[6]) ? (string) $url[6] : null; - $totalnbCoins = !empty($url[7]) ? (string) $url[7] : null; - $nbGames = !empty($url[8]) ? (string) $url[8] : null; - $currentSkin = !empty($url[9]) ? (string) $url[9] : null; + $nbCurrentCoins = !empty($url[6]) ? (int) $url[6] : null; + $totalnbCoins = !empty($url[7]) ? (int) $url[7] : null; + $nbGames = !empty($url[8]) ? (int) $url[8] : null; + $currentSkin = !empty($url[9]) ? (int) $url[9] : null; $usergw->putUser($id,$username,$password,$nbCurrentCoins,$totalnbCoins,$nbGames,$currentSkin); + http_response_code(200); } elseif($method_name === "putSkinList"){ // test : OK - $idUser = !empty($url[3]) ? (string) $url[3] : null; - $idSkin = !empty($url[4]) ? (string) $url[4] : null; + $idUser = !empty($url[3]) ? (int) $url[3] : null; + $idSkin = !empty($url[4]) ? (int) $url[4] : null; if ($idUser != null || $idSkin != null){ $usergw->putSkinList($idUser,$idSkin); + http_response_code(200); } else{ header("HTTP/1.0 400 idSkin or idUser not given"); http_response_code(400); } } elseif($method_name === "putMatch"){ // test : OK - $id = !empty($url[3]) ? (string) $url[3] : null; + $id = !empty($url[3]) ? (int) $url[3] : null; if ($id !== null){ $matchgw->putMatch($id); + http_response_code(200); } else{ header("HTTP/1.0 400 Id not given"); http_response_code(400); } } elseif($method_name === "addUserToMatch"){ // test : OK - $idMatch = !empty($url[3]) ? (string) $url[3] : null; - $idUser = !empty($url[4]) ? (string) $url[4] : null; + $idMatch = !empty($url[3]) ? (int) $url[3] : null; + $idUser = !empty($url[4]) ? (int) $url[4] : null; if ($idUser != null || $idMatch != null){ $matchgw->addUserToMatch($idMatch,$idUser); + http_response_code(200); } else{ header("HTTP/1.0 400 idSkin or idUser not given"); http_response_code(400); } } elseif($method_name === "deleteUserFromMatch"){ // test : OK - $idUser = !empty($url[3]) ? (string) $url[3] : null; + $idUser = !empty($url[3]) ? (int) $url[3] : null; if ($idUser != null){ $matchgw->deleteUserFromMatch($idUser); + http_response_code(200); } else{ header("HTTP/1.0 400 idUser not given"); http_response_code(400); } } elseif($method_name === "putConversation"){ // test : OK - $id = !empty($url[3]) ? (string) $url[3] : null; + $id = !empty($url[3]) ? (int) $url[3] : null; $newName = !empty($url[4]) ? (string) $url[4] : null; if ($id != null && $newName != null){ $conversationgw->putConversation($id,$newName); + http_response_code(200); } else{ header("HTTP/1.0 400 id or new name not given"); http_response_code(400); } } elseif($method_name === "addUserToConversation"){ // test : OK - $idConv = !empty($url[3]) ? (string) $url[3] : null; - $idUser = !empty($url[4]) ? (string) $url[4] : null; + $idConv = !empty($url[3]) ? (int) $url[3] : null; + $idUser = !empty($url[4]) ? (int) $url[4] : null; if ($idConv != null && $idUser != null){ $conversationgw->addUserToConversation($idConv,$idUser); + http_response_code(200); } else{ header("HTTP/1.0 400 id conv or id user not given"); http_response_code(400); } } elseif($method_name === "deleteUserFromConversation"){ // test : OK - $idConv = !empty($url[3]) ? (string) $url[3] : null; - $idUser = !empty($url[4]) ? (string) $url[4] : null; + $idConv = !empty($url[3]) ? (int) $url[3] : null; + $idUser = !empty($url[4]) ? (int) $url[4] : null; if ($idConv != null && $idUser != null){ $conversationgw->deleteUserFromConversation($idConv,$idUser); + http_response_code(200); } else{ header("HTTP/1.0 400 id conv or id user not given"); http_response_code(400); @@ -255,6 +282,7 @@ $idConv=!empty($url[5]) ? (int) $url[5] : null; if ($msg != null && $idSender != null && $idConv != null){ $conversationgw->addMessageToConversation($msg,$idSender,$idConv); + http_response_code(200); } else{ header("HTTP/1.0 400 id conv or message or sender not given"); http_response_code(400); @@ -267,27 +295,30 @@ break; case 'DELETE': if($method_name === "deleteUser"){ // test : OK - $id = !empty($url[3]) ? (string) $url[3] : null; + $id = !empty($url[3]) ? (int) $url[3] : null; if($id!=null){ $usergw->deleteUser($id); + http_response_code(200); }else{ header("HTTP/1.0 400 Id not given"); http_response_code(400); } } elseif($method_name == "deleteMatch"){ // test : OK - $id = !empty($url[3]) ? (string) $url[3] : null; + $id = !empty($url[3]) ? (int) $url[3] : null; if($id!=null){ $matchgw->deleteMatch($id); + http_response_code(200); }else{ header("HTTP/1.0 400 Id not given"); http_response_code(400); } } elseif($method_name === "deleteConversation"){ // test : OK - $id = !empty($url[3]) ? (string) $url[3] : null; + $id = !empty($url[3]) ? (int) $url[3] : null; if($id!=null){ $conversationgw->deleteConversation($id); + http_response_code(200); }else{ header("HTTP/1.0 400 Id not given"); http_response_code(400); From 8eb9458caea81464acbf7a4552e564db55457549 Mon Sep 17 00:00:00 2001 From: Lucie Bedouret Date: Fri, 9 Dec 2022 09:22:42 +0100 Subject: [PATCH 14/14] MODIF: better .htaccess --- api-rest/.htaccess | 12 +++-- api-rest/gateways/gameGateway.php | 8 ++- api-rest/index.php | 87 ++++++++++++++++--------------- db-config.sql | 4 +- 4 files changed, 62 insertions(+), 49 deletions(-) diff --git a/api-rest/.htaccess b/api-rest/.htaccess index e833542..7308adc 100644 --- a/api-rest/.htaccess +++ b/api-rest/.htaccess @@ -6,8 +6,14 @@ RewriteRule (.+) index.php?p=$1 [QSA,L] - -Order Allow,Deny -Allow From all +# Redirecting 403 errors to index.php (does not work) +ErrorDocument 403 http://localhost:8888/api-rest/index.php + +# Refusing access to all files ending with php +Require all denied + +# Allowing access to index.php + + Require all granted diff --git a/api-rest/gateways/gameGateway.php b/api-rest/gateways/gameGateway.php index 2875ab2..299bc1c 100644 --- a/api-rest/gateways/gameGateway.php +++ b/api-rest/gateways/gameGateway.php @@ -25,7 +25,9 @@ class GameGateway{ foreach($res as $row){ $tabGames[]= new Game($row['PK_ID'], $row['GAM_NAME'], - $row['GAM_IMAGE']); + $row['GAM_IMAGE'], + $row['GAM_NB_PLAYER_MIN'], + $row['GAM_NB_PLAYER_MAX']); } return $tabGames; } @@ -41,7 +43,9 @@ class GameGateway{ foreach($res as $row){ $game= new Game($row['PK_ID'], $row['GAM_NAME'], - $row['GAM_IMAGE']); + $row['GAM_IMAGE'], + $row['GAM_NB_PLAYER_MIN'], + $row['GAM_NB_PLAYER_MAX']); } return $game; } diff --git a/api-rest/index.php b/api-rest/index.php index bb41cb1..c1213d6 100644 --- a/api-rest/index.php +++ b/api-rest/index.php @@ -1,4 +1,5 @@ getUserById($id); header('Content-Type: application/json'); echo json_encode($user, JSON_PRETTY_PRINT); @@ -69,7 +70,7 @@ } } elseif($method_name === "getUserByUsername"){ // test : OK - $username = !empty($url[3]) ? (string) $url[3] : null; + $username = !empty($url[4]) ? (string) $url[4] : null; if ($username !== null){ $user =$usergw->getUserByUsername($username); header('Content-Type: application/json'); @@ -80,8 +81,8 @@ } } elseif($method_name === "getUserForConnection"){ // test : OK - $username = !empty($url[3]) ? (string) $url[3] : null; - $password = !empty($url[4]) ? (string) $url[4] : null; + $username = !empty($url[4]) ? (string) $url[4] : null; + $password = !empty($url[5]) ? (string) $url[5] : null; if ($username != null || $password != null){ $user =$usergw->getUserForConnection($username,$password); header('Content-Type: application/json'); @@ -105,7 +106,7 @@ http_response_code(200); } elseif($method_name === "getGameById"){ // test : OK - $id = !empty($url[3]) ? (int) $url[3] : null; + $id = !empty($url[4]) ? (int) $url[4] : null; if ($id !== null){ $game = $gamegw->getGameById($id); header('Content-Type: application/json'); @@ -117,7 +118,7 @@ } } elseif($method_name === "getMatchById"){ // test : OK - $id = !empty($url[3]) ? (int) $url[3] : null; + $id = !empty($url[4]) ? (int) $url[4] : null; if ($id !== null){ $match = $matchgw->getMatchById($id); header('Content-Type: application/json'); @@ -129,7 +130,7 @@ } } elseif($method_name === "getConversations"){ // tests : OK - $id = !empty($url[3]) ? (int) $url[3] : null; + $id = !empty($url[4]) ? (int) $url[4] : null; if ($id !== null){ $conversations = $conversationgw->getConversations($id); header('Content-Type: application/json'); @@ -150,17 +151,17 @@ header("HTTP/1.0 400 Invalid number of arguments"); http_response_code(400); } - $username = !empty($url[3]) ? (string) $url[3] : null; - $password = !empty($url[4]) ? (string) $url[4] : null; + $username = !empty($url[4]) ? (string) $url[4] : null; + $password = !empty($url[5]) ? (string) $url[5] : null; $nationality = !empty($url[5]) ? (string) $url[5] : null; - $sex = !empty($url[6]) ? (string) $url[6] : null; - $dateOfBirth = !empty($url[7]) ? (string) $url[7] : null; + $sex = !empty($url[7]) ? (string) $url[7] : null; + $dateOfBirth = !empty($url[8]) ? (string) $url[8] : null; $usergw->postUser($username,$password,$nationality,$sex,$dateOfBirth); http_response_code(200); } elseif($method_name === "postMatch"){ // test : OK - $idGame = !empty($url[3]) ? (int) $url[3] : null; - $idCreator = !empty($url[4]) ? (int) $url[4] : null; + $idGame = !empty($url[4]) ? (int) $url[4] : null; + $idCreator = !empty($url[5]) ? (int) $url[5] : null; if ($idGame != null || $idCreator != null){ $match =$matchgw->postMatch($idGame,$idCreator); http_response_code(200); @@ -170,8 +171,8 @@ } } elseif($method_name === "postConversation"){ // test : OK - $name = !empty($url[3]) ? (string) $url[3] : null; - $idCreator = !empty($url[4]) ? (int) $url[4] : null; + $name = !empty($url[4]) ? (string) $url[4] : null; + $idCreator = !empty($url[5]) ? (int) $url[5] : null; if ($name != null || $idCreator != null){ $conversationgw->postConversation($name,$idCreator); http_response_code(200); @@ -187,23 +188,23 @@ break; case 'PUT': if($method_name === "putUser"){ // test : OK - if (count($url)<9){ + if (count($url)<10){ header("HTTP/1.0 400 Invalid number of arguments"); http_response_code(400); } - $id = !empty($url[3]) ? (int) $url[3] : null; - $username = !empty($url[4]) ? (string) $url[4] : null; - $password = !empty($url[5]) ? (string) $url[5] : null; - $nbCurrentCoins = !empty($url[6]) ? (int) $url[6] : null; - $totalnbCoins = !empty($url[7]) ? (int) $url[7] : null; - $nbGames = !empty($url[8]) ? (int) $url[8] : null; - $currentSkin = !empty($url[9]) ? (int) $url[9] : null; + $id = !empty($url[4]) ? (int) $url[4] : null; + $username = !empty($url[5]) ? (string) $url[5] : null; + $password = !empty($url[6]) ? (string) $url[6] : null; + $nbCurrentCoins = !empty($url[7]) ? (int) $url[7] : null; + $totalnbCoins = !empty($url[8]) ? (int) $url[8] : null; + $nbGames = !empty($url[9]) ? (int) $url[9] : null; + $currentSkin = !empty($url[10]) ? (int) $url[10] : null; $usergw->putUser($id,$username,$password,$nbCurrentCoins,$totalnbCoins,$nbGames,$currentSkin); http_response_code(200); } elseif($method_name === "putSkinList"){ // test : OK - $idUser = !empty($url[3]) ? (int) $url[3] : null; - $idSkin = !empty($url[4]) ? (int) $url[4] : null; + $idUser = !empty($url[4]) ? (int) $url[4] : null; + $idSkin = !empty($url[5]) ? (int) $url[5] : null; if ($idUser != null || $idSkin != null){ $usergw->putSkinList($idUser,$idSkin); http_response_code(200); @@ -213,7 +214,7 @@ } } elseif($method_name === "putMatch"){ // test : OK - $id = !empty($url[3]) ? (int) $url[3] : null; + $id = !empty($url[4]) ? (int) $url[4] : null; if ($id !== null){ $matchgw->putMatch($id); http_response_code(200); @@ -223,8 +224,8 @@ } } elseif($method_name === "addUserToMatch"){ // test : OK - $idMatch = !empty($url[3]) ? (int) $url[3] : null; - $idUser = !empty($url[4]) ? (int) $url[4] : null; + $idMatch = !empty($url[4]) ? (int) $url[4] : null; + $idUser = !empty($url[5]) ? (int) $url[5] : null; if ($idUser != null || $idMatch != null){ $matchgw->addUserToMatch($idMatch,$idUser); http_response_code(200); @@ -234,7 +235,7 @@ } } elseif($method_name === "deleteUserFromMatch"){ // test : OK - $idUser = !empty($url[3]) ? (int) $url[3] : null; + $idUser = !empty($url[4]) ? (int) $url[4] : null; if ($idUser != null){ $matchgw->deleteUserFromMatch($idUser); http_response_code(200); @@ -244,8 +245,8 @@ } } elseif($method_name === "putConversation"){ // test : OK - $id = !empty($url[3]) ? (int) $url[3] : null; - $newName = !empty($url[4]) ? (string) $url[4] : null; + $id = !empty($url[4]) ? (int) $url[4] : null; + $newName = !empty($url[5]) ? (string) $url[5] : null; if ($id != null && $newName != null){ $conversationgw->putConversation($id,$newName); http_response_code(200); @@ -255,8 +256,8 @@ } } elseif($method_name === "addUserToConversation"){ // test : OK - $idConv = !empty($url[3]) ? (int) $url[3] : null; - $idUser = !empty($url[4]) ? (int) $url[4] : null; + $idConv = !empty($url[4]) ? (int) $url[4] : null; + $idUser = !empty($url[5]) ? (int) $url[5] : null; if ($idConv != null && $idUser != null){ $conversationgw->addUserToConversation($idConv,$idUser); http_response_code(200); @@ -266,8 +267,8 @@ } } elseif($method_name === "deleteUserFromConversation"){ // test : OK - $idConv = !empty($url[3]) ? (int) $url[3] : null; - $idUser = !empty($url[4]) ? (int) $url[4] : null; + $idConv = !empty($url[4]) ? (int) $url[4] : null; + $idUser = !empty($url[5]) ? (int) $url[5] : null; if ($idConv != null && $idUser != null){ $conversationgw->deleteUserFromConversation($idConv,$idUser); http_response_code(200); @@ -277,9 +278,9 @@ } } elseif($method_name === "addMessageToConversation"){ // test : OK - $msg=!empty($url[3]) ? (string) $url[3] : null; - $idSender=!empty($url[4]) ? (int) $url[4] : null; - $idConv=!empty($url[5]) ? (int) $url[5] : null; + $msg=!empty($url[4]) ? (string) $url[4] : null; + $idSender=!empty($url[5]) ? (int) $url[5] : null; + $idConv=!empty($url[6]) ? (int) $url[6] : null; if ($msg != null && $idSender != null && $idConv != null){ $conversationgw->addMessageToConversation($msg,$idSender,$idConv); http_response_code(200); @@ -295,7 +296,7 @@ break; case 'DELETE': if($method_name === "deleteUser"){ // test : OK - $id = !empty($url[3]) ? (int) $url[3] : null; + $id = !empty($url[4]) ? (int) $url[4] : null; if($id!=null){ $usergw->deleteUser($id); http_response_code(200); @@ -305,7 +306,7 @@ } } elseif($method_name == "deleteMatch"){ // test : OK - $id = !empty($url[3]) ? (int) $url[3] : null; + $id = !empty($url[4]) ? (int) $url[4] : null; if($id!=null){ $matchgw->deleteMatch($id); http_response_code(200); @@ -315,7 +316,7 @@ } } elseif($method_name === "deleteConversation"){ // test : OK - $id = !empty($url[3]) ? (int) $url[3] : null; + $id = !empty($url[4]) ? (int) $url[4] : null; if($id!=null){ $conversationgw->deleteConversation($id); http_response_code(200); diff --git a/db-config.sql b/db-config.sql index 0f2978c..8417830 100644 --- a/db-config.sql +++ b/db-config.sql @@ -41,7 +41,9 @@ CREATE TABLE T_H_SKIN_SKI ( CREATE TABLE T_E_GAME_GAM ( PK_ID int AUTO_INCREMENT PRIMARY KEY, GAM_NAME varchar(50) UNIQUE, - GAM_IMAGE varchar(50) UNIQUE + GAM_IMAGE varchar(50) UNIQUE, + GAM_NB_PLAYER_MIN int, + GAM_NB_PLAYER_MAX int ); /* -- Table Match -- */