ADD : toutes les méthodes et routing fonctionnels
continuous-integration/drone/push Build was killed Details

stub-api
Lucie Bedouret 2 years ago
parent b0bd93a8dc
commit a21e2a93d8

@ -31,38 +31,43 @@ class ConversationGateway{
$tabConversations=NULL; $tabConversations=NULL;
$tabUsers=NULL; $tabUsers=NULL;
$tabMessages=NULL; $tabMessages=NULL;
$conversationQuery = "SELECT c.id, c.nom $conversationQuery = "SELECT c.PK_ID, c.COV_NAME
FROM T_E_CONVERSATION_COV c, T_J_DISCUTE_DIS d FROM T_H_CONVERSATION_COV c, T_J_DISCUSS_DIS d
WHERE c.id=d.idConv WHERE c.PK_ID=d.FK_CONVERSATION
AND d.idUser=:idUser"; AND d.FK_USER=:idUser";
$messagesQuery = "SELECT m.id, m.message, m.idSender $messagesQuery = "SELECT m.PK_ID, m.MSG_MESSAGE, m.FK_SENDER
FROM T_R_MESSAGE_MSG m, T_J_DISCUTE_DIS d FROM T_H_MESSAGE_MSG m, T_J_CONTAIN_MESSAGE_CMG c
WHERE m.id=h.idMessage WHERE m.PK_ID=c.FK_MESSAGE
AND h.idConv=:idConv"; AND c.FK_CONVERSATION=:idConv";
$usersQuery = "SELECT d.idUser $usersQuery = "SELECT d.FK_USER
FROM T_J_DISCUTE_DIS d FROM T_J_DISCUSS_DIS d
WHERE d.idConv = :idConv"; WHERE d.FK_CONVERSATION = :idConv";
//Find all the conversations where the user belong //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); $this->connection->execQuery($conversationQuery,$argIdUser);
$res=$this->connection->getRes(); $res=$this->connection->getRes();
foreach($res as $row){ 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 // Find all messages of the conversation
$this->connection->execQuery($messagesQuery,$argIdConv); $this->connection->execQuery($messagesQuery,$argIdConv);
$resMessages=$this->connection->getRes(); $resMessages=$this->connection->getRes();
foreach($resMessages as $rowMessages){ 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 // Find all the users in the conversation
$this->connection->execQuery($usersQuery,$argIdConv); $this->connection->execQuery($usersQuery,$argIdConv);
$resUsers=$this->connection->getRes(); $resUsers=$this->connection->getRes();
foreach($resUsers as $rowUsers){ foreach($resUsers as $rowUsers){
$tabUsers[] = $rowUsers['idUser']; $tabUsers[] = $rowUsers['FK_USER'];
} }
// Add the conversation into the array // 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 // Restore the arrays
$tabUsers=array(); $tabUsers=array();
$tabMessages=array(); $tabMessages=array();
@ -71,76 +76,80 @@ class ConversationGateway{
} }
/// Brief : Adding a new conversation in database /// Brief : Adding a new conversation in database
/// Parameters : * $c (Conversation): conversation we want to insert in database public function postConversation(string $name, int $idUser): void{
/// ***** CRÉER DES TRIGGERS ***** ///
public function postConversation(Conversation $c): void{
// Declare queries // Declare queries
$convCreationQuery = "INSERT INTO T_E_CONVERSATION_COV VALUES(:idConv,:name)"; $convCreationQuery = "INSERT INTO T_H_CONVERSATION_COV VALUES(NULL,:name)";
$addUserInConvQuery = "INSERT INTO T_J_DISCUTE_DIS VALUES(:idUser,:idConv)"; $addUserInConvQuery = "INSERT INTO T_J_DISCUSS_DIS VALUES(:idUser,:idConv)";
$argconvCreationQuery = array('idConv'=>array($c->id,PDO::PARAM_STR), $argconvCreationQuery = array('name'=>array($name, PDO::PARAM_STR));
'name'=>array($c->name, PDO::PARAM_STR));
// Create a new conversation // Create a new conversation
$this->connection->execQuery($convCreationQuery,$argconvCreationQuery); $this->connection->execQuery($convCreationQuery,$argconvCreationQuery);
// Add users of the conversation in the conversation $this->connection->execQuery("SELECT PK_ID
foreach($c->listIdUsers as $idUsr){ FROM T_H_CONVERSATION_COV
$argUserInConvQuery = array('idUser'=>array($idUsr, PDO::PARAM_STR), WHERE PK_ID >= ALL (SELECT max(c2.PK_ID)
'idConv'=>array($c->id, PDO::PARAM_STR)); FROM T_H_CONVERSATION_COV c2)",[]);
$this->connection->execQuery($query2,$arg2); $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 /// Brief : Adding an user to a conversation
/// Parameters : * $u (Matchs): match we want to update in database public function addUserToConversation(int $idConv, int $idUser){
/// ***** CRÉER DES TRIGGERS ***** /// $insertUserQuery = "INSERT INTO T_J_DISCUSS_DIS VALUES(:idUser,:idConv)";
public function putConversation(Conversation $c):void{ $argQuery = array('idUser'=>array($idUser,PDO::PARAM_INT),
// Declare the queries 'idConv'=>array($idConv,PDO::PARAM_INT));
$conversationInsertionQuery = "INSERT INTO T_E_CONVERSATION_COV VALUES (:id,:nom)"; $this->connection->execQuery($insertUserQuery,$argQuery);
$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)"; /// Brief : Deleting an user from a conversation
$argConversationInsertion = array('id'=>array($c->id, PDO::PARAM_STR), public function deleteUserFromConversation(int $idConv, int $idUser){
'nom'=>array($c->name,PDO::PARAM_STR)); $insertUserQuery = "DELETE FROM T_J_DISCUSS_DIS WHERE FK_USER=:idUser AND FK_CONVERSATION=:idConv";
// Delete current data from database $argQuery = array('idUser'=>array($idUser,PDO::PARAM_INT),
deleteConversation($c); 'idConv'=>array($idConv,PDO::PARAM_INT));
// Add conversation $this->connection->execQuery($insertUserQuery,$argQuery);
$this->connection->execQuery($conversationInsertionQuery,$argConversationInsertion); }
// Add messages to conversation
foreach($c->listMessages as $msg){ /// Brief : adding a new message into a conversation
$argContainInsertion = array('idConv'=>array($c->id,PDO::PARAM_STR), public function addMessageToConversation(string $message, int $idSender, int $idConv){
'idMessage'=>array($msg->id,PDO::PARAM_STR)); $insertMessageQuery = "INSERT INTO T_H_MESSAGE_MSG VALUES(NULL,:message,:idSender)";
$argMessageInsertion = array('id'=>array($msg->id,PDO::PARAM_STR), $insertMsgInConvQuery = "INSERT INTO T_J_CONTAIN_MESSAGE_CMG VALUES(:idConv,:idMessage)";
'message'=>array($msg->message,PDO::PARAM_STR),
'idSender'=>array($msg->idSender,PDO::PARAM_STR)); $argInsertMessage= array('message'=>array($message,PDO::PARAM_STR),
$this->connection->execQuery($containInsertionQuery,$argContainInsertion); 'idSender'=>array($idSender,PDO::PARAM_INT));
$this->connection->execQuery($messageInsertionQuery,$argMessageInsertion); $this->connection->execQuery($insertMessageQuery,$argInsertMessage);
} $this->connection->execQuery("SELECT PK_ID
// Add user to conversation FROM T_H_MESSAGE_MSG
foreach($c->listIdUsers as $idUsr){ WHERE PK_ID >= ALL (SELECT max(m2.PK_ID)
$argDiscuteInsertion = array('idUsr'=>array($idUsr,PDO::PARAM_STR), FROM T_H_MESSAGE_MSG m2)",[]);
'idConv'=>array($c->id,PDO::PARAM_STR)); $res=$this->connection->getRes();
$this->connection->execQuery($discuteInsertionQuery,$argDiscuteInsertion); 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 /// Brief : Deleting a conversation and its messages from database
/// Parameters : * $c (Conversation): conversation we want to delete from database public function deleteConversation(int $id):void{
// ---- $deleteConv = "DELETE FROM T_H_CONVERSATION_COV
// Ne pas oublier le on delete cascade dans la création des tables WHERE PK_ID=:idConv";
// Créer des triggers $argIdConv = array('idConv'=>array($id,PDO::PARAM_INT));
// ----
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);
$this->connection->execQuery($deleteConv,$argIdConv); $this->connection->execQuery($deleteConv,$argIdConv);
} }
} }

@ -88,7 +88,7 @@ class MatchGateway{
/// Brief : Deleting a match from database /// Brief : Deleting a match from database
/// Parameters : * $u (Matchs): match we want to delete from database /// Parameters : * $u (Matchs): match we want to delete from database
public function deleteMatch(int $id){ 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)); $arg=array('id'=>array($id, PDO::PARAM_INT));
$this->connection->execQuery($query,$arg); $this->connection->execQuery($query,$arg);
} }

@ -114,6 +114,16 @@
http_response_code(400); 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{ else{
header("HTTP/1.0 401 UNAUTHORIZED REQUEST"); header("HTTP/1.0 401 UNAUTHORIZED REQUEST");
http_response_code(401); http_response_code(401);
@ -137,7 +147,17 @@
if ($idGame != null || $idCreator != null){ if ($idGame != null || $idCreator != null){
$match =$matchgw->postMatch($idGame,$idCreator); $match =$matchgw->postMatch($idGame,$idCreator);
} else{ } 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); http_response_code(400);
} }
} }
@ -195,7 +215,48 @@
if ($idUser != null){ if ($idUser != null){
$matchgw->deleteUserFromMatch($idUser); $matchgw->deleteUserFromMatch($idUser);
} else{ } 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); http_response_code(400);
} }
} }
@ -214,7 +275,7 @@
http_response_code(400); http_response_code(400);
} }
} }
elseif($method_name == "deleteMatch"){ // test : elseif($method_name == "deleteMatch"){ // test : OK
$id = !empty($url[3]) ? (string) $url[3] : null; $id = !empty($url[3]) ? (string) $url[3] : null;
if($id!=null){ if($id!=null){
$matchgw->deleteMatch($id); $matchgw->deleteMatch($id);
@ -223,6 +284,15 @@
http_response_code(400); 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{ else{
header("HTTP/1.0 401 UNAUTHORIZED REQUEST"); header("HTTP/1.0 401 UNAUTHORIZED REQUEST");
http_response_code(401); http_response_code(401);

@ -143,10 +143,6 @@ CREATE TRIGGER before_delete_conversation
BEFORE DELETE BEFORE DELETE
ON T_H_CONVERSATION_COV ON T_H_CONVERSATION_COV
FOR EACH ROW FOR EACH ROW
r record; DELETE FROM T_H_MESSAGE_MSG WHERE PK_ID = (SELECT FK_MESSAGE
FOR r in (SELECT c.PK_ID FROM T_J_CONTAIN_MESSAGE_CMG
FROM T_H_MESSAGE_MSG m, T_J_CONTAIN_MESSAGE c WHERE FK_CONVERSATION=OLD.PK_ID);
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;

Loading…
Cancel
Save