From fb0d6ec69a0356fd93be777a64605fe1c2dac43e Mon Sep 17 00:00:00 2001 From: Lucie Bedouret Date: Mon, 21 Nov 2022 17:12:09 +0100 Subject: [PATCH] ADD : methodes pour les conversations et les messages --- api-rest/gateways/conversationGataway.php | 169 ++++++++++++++++++++++ api-rest/gateways/matchGateway.php | 5 +- api-rest/gateways/userGateway.php | 28 ++-- api-rest/index.php | 13 +- api-rest/model/conversation.php | 19 +++ api-rest/model/match.php | 2 +- api-rest/model/message.php | 17 +++ 7 files changed, 230 insertions(+), 23 deletions(-) create mode 100644 api-rest/gateways/conversationGataway.php create mode 100644 api-rest/model/conversation.php create mode 100644 api-rest/model/message.php diff --git a/api-rest/gateways/conversationGataway.php b/api-rest/gateways/conversationGataway.php new file mode 100644 index 0000000..c49e74c --- /dev/null +++ b/api-rest/gateways/conversationGataway.php @@ -0,0 +1,169 @@ +connection=$_connection; + } + + /* Functions implemented to manage conversations' data from database + + * getConversations : returning all the ids of the conversations of an user + (with all the id of the messages and the users in the conversation) + * postConversation : adding a NEW conversation in database + * putMatch : modifying an EXISTING conversation in database + * deleteMatch : deleting an conversation from database + + */ + + +/// Brief : Returning all the ids of the conversations where an user belongs + ///(with all the id of the messages and the users in the conversation) +/// Parameters : * $idUser (string): identifier of the user we want to get the conversations + public function getConversations(string $_idUser):?array{ + $tabIdConversation=NULL; + $tabConversations=NULL; + $tabUsers=NULL; + $tabIdMessages=NULL; + $tabMessages=NULL; + + $query1 = "SELECT idConversation FROM InConversation WHERE idUser=:idUser"; + $query2 = "SELECT idUser FROM InConversation WHERE idConversation=:idConv"; + $query3 = "SELECT idMessage FROM Have WHERE idConversation=:idConv"; + $query4 = "SELECT id, message, idSender FROM Message WHERE id=:id"; + $query5 = "SELECT id, nom FROM Conversation WHERE id=:idConv"; + + $arg1=array('idUser'=>array($_idUser, PDO::PARAM_STR)); + + $this->connection->execQuery($query1,$arg1); + $res=$this->connection->getRes(); + foreach($res as $row){ + $tabIdConversation[] = $row['idConversation']; + } + + foreach($tabIdConversation as $idConv){ + + $arg2 = array('idConv'=>array($idConv, PDO::PARAM_STR)); + $this->connection->execQuery($query2,$arg2); + $res=$this->connection->getRes(); + foreach($res as $row){ + $tabUsers[] = $row['idUser']; + } + + $this->connection->execQuery($query3,$arg2); + $res=$this->connection->getRes(); + foreach($res as $row){ + $tabIdMessages[] = $row['idMessage']; + } + + foreach($tabIdMessages as $idMessage){ + $arg3=array('id'=>array($idMessage,PDO::PARAM_STR)); + $this->connection->execQuery($query4,$arg3); + $res=$this->connection->getRes(); + foreach($res as $row){ + $tabMessages[] = new Message($row['id'],$row['message'],$row['idSender']); + } + } + + $this->connection->execQuery($query5,$arg2); + $res=$this->connection->getRes(); + foreach($res as $row){ + $tabConversations[]= new Conversation($row['id'], $row['nom'],$tabMessages,$tabUsers); + } + + $tabUsers=array(); + $tabIdMessages=array(); + $tabMessages=array(); + } + return $tabConversations; + } + +/// Brief : Adding a new conversation in database +/// Parameters : * $c (Conversation): conversation we want to insert in database + public function postConversation(Conversation $c): void{ + $query1 = "INSERT INTO Conversation VALUES(:idConv,:name)"; + $query2 = "INSERT INTO InConversation VALUES(:idUser,:idConv)"; + + $arg1 = array('idConv'=>array($c->id,PDO::PARAM_STR), + 'name'=>array($c->name, PDO::PARAM_STR)); + + $this->connection->execQuery($query1,$arg1); + + foreach($c->listIdUsers as $idUsr){ + $arg2 = array('idUser'=>array($idUsr, PDO::PARAM_STR), + 'idConv'=>array($c->id, PDO::PARAM_STR)); + $this->connection->execQuery($query2,$arg2); + } + } + +/// Brief : Modifying an EXISTING match in database +/// Parameters : * $u (Matchs): match we want to update in database + public function putConversation(Conversation $c):void{ + $query7 = "SELECT idMessage FROM Have WHERE idConversation=:idConv"; + $query8 = "DELETE FROM Message WHERE id = :id"; + $query1 = "DELETE FROM Have WHERE idConversation = :idConv"; + $query2 = "DELETE FROM InConversation WHERE idConversation = :idConv"; + $query3 = "UPDATE Conversation SET nom=:nom WHERE id=:id"; + $query4 = "INSERT INTO Have VALUES (:idConv,:idMessage)"; + $query5 = "INSERT INTO Message VALUES(:id,:message,:idSender)"; + $query6 = "INSERT INTO InConversation VALUES(:idUsr,:idConv)"; + + + $arg1 = array('idConv'=>array($c->id,PDO::PARAM_STR)); + $arg2 = array('nom'=>array($c->name, PDO::PARAM_STR), + 'id'=>array($c->id,PDO::PARAM_STR)); + + $this->connection->execQuery($query7,$arg1); + $res = $this->connection->getRes(); + foreach($res as $idMsg){ + $arg6 = array('id'=>array($idMsg['idMessage'],PDO::PARAM_STR)); + $this->connection->execQuery($query8,$arg6); + } + + $this->connection->execQuery($query1,$arg1); + $this->connection->execQuery($query2, $arg1); + $this->connection->execQuery($query3,$arg2); + + foreach($c->listMessages as $msg){ + $arg3 = array('idConv'=>array($c->id,PDO::PARAM_STR), + 'idMessage'=>array($msg->id,PDO::PARAM_STR)); + $arg4 = array('id'=>array($msg->id,PDO::PARAM_STR), + 'message'=>array($msg->message,PDO::PARAM_STR), + 'idSender'=>array($msg->idSender,PDO::PARAM_STR)); + $this->connection->execQuery($query4,$arg3); + $this->connection->execQuery($query5,$arg4); + } + + foreach($c->listIdUsers as $idUsr){ + $arg5 = array('idUsr'=>array($idUsr,PDO::PARAM_STR), + 'idConv'=>array($c->id,PDO::PARAM_STR)); + $this->connection->execQuery($query6,$arg5); + } + } + +/// Brief : Deleting a conversation and its messages from database +/// Parameters : * $c (Conversation): conversation we want to delete from database + public function deleteConversation(Conversation $c):void{ + $query1 = "DELETE FROM Message WHERE id=:id"; + $query2 = "DELETE FROM Have WHERE idConversation = :idConv"; + $query3 = "DELETE FROM InConversation WHERE idConversation = :idConv"; + $query4 = "DELETE FROM Conversation WHERE id = :idConv"; + + foreach($c->listMessages as $msg){ + $arg1 = array('id'=>array($msg->id,PDO::PARAM_STR)); + $this->connection->execQuery($query1,$arg1); + } + $arg2 = array('idConv'=>array($c->id,PDO::PARAM_STR)); + $this->connection->execQuery($query2,$arg2); + $this->connection->execQuery($query3,$arg2); + $this->connection->execQuery($query4,$arg2); + } +} + +?> \ No newline at end of file diff --git a/api-rest/gateways/matchGateway.php b/api-rest/gateways/matchGateway.php index 462fba3..638b3b0 100644 --- a/api-rest/gateways/matchGateway.php +++ b/api-rest/gateways/matchGateway.php @@ -1,10 +1,9 @@ connection->execQuery($query,[]); - $res=$this->connection->getRes(); - foreach($res as $row){ - $tabUser[] = new User ($row['id'],$row['username'],$row['password'],$row['nationality'],$row['sex'],$row['dateOfBirth'],$row['currentBobCoins'],$row['totalBobCoins'],$row['nbGamesPlayed'],$row['currentIdSkin']); - } - - return $tabUser; - } -*/ - /// Brief : Returning an user found in database with his id /// Parameters : * $id (string): identifier of the user we are looking for public function getUserById(string $id):?User{ @@ -107,6 +92,17 @@ class UserGateway{ return $usr; } +/// Brief : Returning the last Id of the users + public function getLastId():string{ + $query = "SELECT id FROM User WHERE id >= ALL (SELECT max(id) FROM User)"; + $this->connection->execQuery($query,[]); + $res=$this->connection->getRes(); + foreach($res as $row){ + $lastId=$row['id']; + } + return $lastId; + } + /// Brief : Adding a NEW user in database /// Parameters : * $u (User): user we want to insert in database public function postUser(User $u): void{ diff --git a/api-rest/index.php b/api-rest/index.php index cfd3aa4..7e1fe64 100644 --- a/api-rest/index.php +++ b/api-rest/index.php @@ -7,6 +7,7 @@ include ('dbConnection.php'); include ('gateways/userGateway.php'); include ('gateways/matchGateway.php'); + include ('gateways/conversationGataway.php'); // Connection to database // A changer quand la base de données sera hébergée, comment masquer les var? @@ -20,6 +21,14 @@ // Initializing Gateways $usergw = new UserGateway($database); $matchgw = new MatchGateway($database); + $conversationgw = new ConversationGateway($database); + + // Testing conversation/messages methods + $listUser=array("U0001","U0004","U0005"); + $listMessages= array(new Message("ME005","coucou","U0001"), new Message("ME006","ca va","U0004"), new Message("ME007","bien et toi?","U0001")); + $newConv = new Conversation("C0006","yémen",$listMessages,$listUser); + $conversationgw->deleteConversation($newConv); + // Managing request, routing and sending responses /* @@ -47,9 +56,7 @@ $res=$usergw->getUserByUsername($username); } else{ - // read all users - $res= $usergw->getUsers(); - echo json_encode($res); + } break; case 'getMatch': diff --git a/api-rest/model/conversation.php b/api-rest/model/conversation.php new file mode 100644 index 0000000..5fc5018 --- /dev/null +++ b/api-rest/model/conversation.php @@ -0,0 +1,19 @@ +id=$_id; + $this->name=$_name; + $this->listMessages=$_listMessages; + $this->listIdUsers=$_listIdUsers; + } +} + +?> \ No newline at end of file diff --git a/api-rest/model/match.php b/api-rest/model/match.php index b8921f7..1853209 100644 --- a/api-rest/model/match.php +++ b/api-rest/model/match.php @@ -14,7 +14,7 @@ class Matchs{ $this->id=$_id; $this->inGame=$_inGame; $this->idGame=$_idGame; - // Only one user at the moment of the creation + // Only one user at creation $this->listIdUsers=$_listIdUsers; } } diff --git a/api-rest/model/message.php b/api-rest/model/message.php new file mode 100644 index 0000000..c36dfa0 --- /dev/null +++ b/api-rest/model/message.php @@ -0,0 +1,17 @@ +id=$_id; + $this->message=$_message; + $this->idSender=$_idSender; + } +} + +?> \ No newline at end of file