diff --git a/api-rest/.htaccess b/api-rest/.htaccess new file mode 100644 index 0000000..7308adc --- /dev/null +++ b/api-rest/.htaccess @@ -0,0 +1,19 @@ +# Rederection if URL not found + + RewriteEngine on + RewriteCond %{REQUEST_FILEANME} !-f + RewriteCond %{REQUEST_FILEANME} !-d + RewriteRule (.+) index.php?p=$1 [QSA,L] + + +# 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/config.ini b/api-rest/config.ini new file mode 100644 index 0000000..262c40f --- /dev/null +++ b/api-rest/config.ini @@ -0,0 +1,4 @@ +[database] +dsn = "mysql:host=localhost;port=8888;dbname=bobParty" +username = "root" +password = "root"; diff --git a/api-rest/dbConnection.php b/api-rest/dbConnection.php new file mode 100644 index 0000000..615fc11 --- /dev/null +++ b/api-rest/dbConnection.php @@ -0,0 +1,24 @@ +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->stmt->execute(); + } + + public function getRes():array{ + return $this->stmt->fetchall(); + } + } + +?> \ No newline at end of file diff --git a/api-rest/gateways/conversationGataway.php b/api-rest/gateways/conversationGataway.php new file mode 100644 index 0000000..51d6ef7 --- /dev/null +++ b/api-rest/gateways/conversationGataway.php @@ -0,0 +1,157 @@ +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{ + // Declaration of arrays (NULL) and queries + $tabConversations=NULL; + $tabUsers=NULL; + $tabMessages=NULL; + $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_INT)); + $this->connection->execQuery($conversationQuery,$argIdUser); + $res=$this->connection->getRes(); + + foreach($res as $row){ + $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['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['FK_USER']; + } + // Add the conversation into the array + $tabConversations[] = new Conversation($row['PK_ID'], + $row['COV_NAME'], + $tabMessages, + $tabUsers); + // Restore the arrays + $tabUsers=array(); + $tabMessages=array(); + } + return $tabConversations; + } + +/// Brief : Adding a new conversation in database + public function postConversation(string $name, int $idUser): void{ + // Declare queries + $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); + $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 : 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 + 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); + } +} + +?> \ No newline at end of file diff --git a/api-rest/gateways/gameGateway.php b/api-rest/gateways/gameGateway.php new file mode 100644 index 0000000..299bc1c --- /dev/null +++ b/api-rest/gateways/gameGateway.php @@ -0,0 +1,54 @@ +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; + $gamesQuery="SELECT * FROM T_E_GAME_GAM"; + $this->connection->execQuery($gamesQuery,[]); + $res = $this->connection->getRes(); + foreach($res as $row){ + $tabGames[]= new Game($row['PK_ID'], + $row['GAM_NAME'], + $row['GAM_IMAGE'], + $row['GAM_NB_PLAYER_MIN'], + $row['GAM_NB_PLAYER_MAX']); + } + 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; + $gameInfoQuery="SELECT * FROM T_E_GAME_GAM WHERE PK_ID=:id"; + $arg=array('id'=>array($id,PDO::PARAM_STR)); + $this->connection->execQuery($gameInfoQuery,$arg); + $res=$this->connection->getRes(); + foreach($res as $row){ + $game= new Game($row['PK_ID'], + $row['GAM_NAME'], + $row['GAM_IMAGE'], + $row['GAM_NB_PLAYER_MIN'], + $row['GAM_NB_PLAYER_MAX']); + } + return $game; + } +} + +?> \ No newline at end of file diff --git a/api-rest/gateways/matchGateway.php b/api-rest/gateways/matchGateway.php new file mode 100644 index 0000000..3a798b9 --- /dev/null +++ b/api-rest/gateways/matchGateway.php @@ -0,0 +1,98 @@ +connection=$con; + } + + /* Functions implemented to manage matches' data from database + + * 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 + + */ + +/// Brief : Returning a match found in database with his id +/// Parameters : * $id (string): identifier of the match we are looking for + public function getMatchById(string $matchId):?Matchs{ + $match=NULL; + $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['FK_USER']; + } + $this->connection->execQuery($matchInfoQuery, $argId); + $res=$this->connection->getRes(); + foreach($res as $row){ + $match = new Matchs($row['PK_ID'],$row['MTC_IN_GAME'],$row['FK_ID_GAME'],$tabUser); + } + return $match; + } + +/// Brief : Adding a NEW match in database + 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 + 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(int $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); + } + +} + +?> \ No newline at end of file diff --git a/api-rest/gateways/skinGateway.php b/api-rest/gateways/skinGateway.php new file mode 100644 index 0000000..af417ad --- /dev/null +++ b/api-rest/gateways/skinGateway.php @@ -0,0 +1,34 @@ +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; + $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; + } +} + +?> \ 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..6dff7cb --- /dev/null +++ b/api-rest/gateways/userGateway.php @@ -0,0 +1,183 @@ +connection=$con; + } + + /* 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 + * 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 : 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){ + $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); + } + 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(); + $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{ + $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(); + $usr=$this->convertResToUser($res); + if ($usr != null){ + $usr->listSkin=$this->getSkinList($usr->id); + } + return $usr; + } + +/// 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{ + $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(); + $usr=$this->convertResToUser($res); + if ($usr != null){ + $usr->listSkin=$this->getSkinList($usr->id); + } + return $usr; + } + +/// Brief : Adding a NEW user in database +/// Parameters : * $u (User): user we want to insert in database +/// 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 +/// 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 +/// 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 new file mode 100644 index 0000000..c1213d6 --- /dev/null +++ b/api-rest/index.php @@ -0,0 +1,340 @@ + 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); + $gamegw = new GameGateway($database); + $skingw = new SkinGateway($database); + + // Managing request, routing and sending + // ------ + // RAPPEL POUR MOI MÊME : NE PAS OUBLIER DE FAIRE DES TRY CATCH !!!!!!! + // ------ + + $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[3]) ? (string)$url[3] : null; + if($method_name == null){ + header("HTTP/1.0 400 Request Name Empty"); + http_response_code(400); + + } + switch ($request_method){ + case 'GET': + if($method_name === "getUserById"){ // test : OK + if(empty($url[4])){ + header("HTTP/1.0 400 Id not given"); + http_response_code(400); + } else{ + $id = (int)$url[4]; + $user = $usergw->getUserById($id); + 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[4]) ? (string) $url[4] : null; + if ($username !== null){ + $user =$usergw->getUserByUsername($username); + 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); + } + } + elseif($method_name === "getUserForConnection"){ // test : OK + $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'); + 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); + } + } + elseif($method_name === "getSkins"){ // test : OK + $tabSkin = $skingw->getSkins(); + 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(); + 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[4]) ? (int) $url[4] : null; + if ($id !== null){ + $game = $gamegw->getGameById($id); + 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[4]) ? (int) $url[4] : null; + if ($id !== null){ + $match = $matchgw->getMatchById($id); + 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[4]) ? (int) $url[4] : null; + if ($id !== null){ + $conversations = $conversationgw->getConversations($id); + 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); + } + } + 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); + } + $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[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[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); + } else{ + header("HTTP/1.0 400 idGame or idCreator not given"); + http_response_code(400); + } + } + elseif($method_name === "postConversation"){ // test : OK + $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); + } else{ + header("HTTP/1.0 400 name or creator not given"); + http_response_code(400); + } + } + else{ + header("HTTP/1.0 401 UNAUTHORIZED REQUEST"); + http_response_code(401); + } + break; + case 'PUT': + if($method_name === "putUser"){ // test : OK + if (count($url)<10){ + header("HTTP/1.0 400 Invalid number of arguments"); + http_response_code(400); + } + $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[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); + } else{ + header("HTTP/1.0 400 idSkin or idUser not given"); + http_response_code(400); + } + } + elseif($method_name === "putMatch"){ // test : OK + $id = !empty($url[4]) ? (int) $url[4] : 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[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); + } else{ + header("HTTP/1.0 400 idSkin or idUser not given"); + http_response_code(400); + } + } + elseif($method_name === "deleteUserFromMatch"){ // test : OK + $idUser = !empty($url[4]) ? (int) $url[4] : 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[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); + } 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[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); + } 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[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); + } 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[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); + } else{ + header("HTTP/1.0 400 id conv or message or sender 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[4]) ? (int) $url[4] : 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[4]) ? (int) $url[4] : 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[4]) ? (int) $url[4] : null; + if($id!=null){ + $conversationgw->deleteConversation($id); + http_response_code(200); + }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/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/game.php b/api-rest/model/game.php new file mode 100644 index 0000000..3870ac2 --- /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/match.php b/api-rest/model/match.php new file mode 100644 index 0000000..1853209 --- /dev/null +++ b/api-rest/model/match.php @@ -0,0 +1,22 @@ +id=$_id; + $this->inGame=$_inGame; + $this->idGame=$_idGame; + // Only one user at creation + $this->listIdUsers=$_listIdUsers; + } +} + +?> \ No newline at end of file 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 diff --git a/api-rest/model/skin.php b/api-rest/model/skin.php new file mode 100644 index 0000000..ac41c1d --- /dev/null +++ b/api-rest/model/skin.php @@ -0,0 +1,18 @@ +id=$_id; + $this->name=$_name; + $this->image=$_image; + $this->price=$_price; + } +} + +?> \ 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..d30e093 --- /dev/null +++ b/api-rest/model/user.php @@ -0,0 +1,34 @@ +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; + $this->currentSkin=$_currentSkin; + $this->listSkin=$_listSkin; + } + +} + +?> \ No newline at end of file diff --git a/db-config.sql b/db-config.sql new file mode 100644 index 0000000..8417830 --- /dev/null +++ b/db-config.sql @@ -0,0 +1,150 @@ +/* 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, + GAM_NB_PLAYER_MIN int, + GAM_NB_PLAYER_MAX int +); + +/* -- 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 + DELETE FROM T_H_MESSAGE_MSG WHERE PK_ID = (SELECT FK_MESSAGE + FROM T_J_CONTAIN_MESSAGE_CMG + WHERE FK_CONVERSATION=OLD.PK_ID);