From b0bd93a8dc08f5c93597f14a1a6256d0b8b82b5e Mon Sep 17 00:00:00 2001 From: Lucie Bedouret Date: Tue, 6 Dec 2022 10:01:23 +0100 Subject: [PATCH] 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;