From a21e2a93d8273752caf546097650d29f2f71c500 Mon Sep 17 00:00:00 2001 From: Lucie Bedouret Date: Tue, 6 Dec 2022 17:55:07 +0100 Subject: [PATCH] =?UTF-8?q?ADD=20:=20toutes=20les=20m=C3=A9thodes=20et=20r?= =?UTF-8?q?outing=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);