From edfa17394019d02ffae3e5a894e9cedda86b7f6c Mon Sep 17 00:00:00 2001 From: Lucie Bedouret Date: Tue, 29 Nov 2022 17:44:46 +0100 Subject: [PATCH] =?UTF-8?q?MODIFY=20:=20d=C3=A9but=20des=20corrections?= 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':