ADD : methodes pour les conversations et les messages
continuous-integration/drone/push Build is failing Details

stub-api
Lucie Bedouret 2 years ago
parent 981cc3e957
commit fb0d6ec69a

@ -0,0 +1,169 @@
<?php
require_once('model/conversation.php');
require_once('model/message.php');
class ConversationGateway{
private $connection;
// Constructor
public function __construct($_connection){
$this->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);
}
}
?>

@ -1,10 +1,9 @@
<?php <?php
include('model/match.php'); require_once('model/match.php');
class MatchGateway{ class MatchGateway{
// Object Attributes
private $connection; private $connection;
// Constructor // Constructor
@ -14,7 +13,7 @@ class MatchGateway{
/* Functions implemented to manage matches' data from database /* Functions implemented to manage matches' data from database
* getMatch : returning a match found in database with his id * getMatch : returning a match found in database with its id
* postMatch : adding a NEW user in database * postMatch : adding a NEW user in database
* putMatch : modifying an EXISTING user in database * putMatch : modifying an EXISTING user in database
* deleteMatch : deleting an user from database * deleteMatch : deleting an user from database

@ -17,28 +17,13 @@ class UserGateway{
* getUserByUsername : returning an user found in database with his username * getUserByUsername : returning an user found in database with his username
* getUserForConnection : returning an user if there is a correspondance between * getUserForConnection : returning an user if there is a correspondance between
the username and the password, used for connection the username and the password, used for connection
* getLastId : returning the last Id of the users
* postUser : adding a NEW user in database * postUser : adding a NEW user in database
* putUser : modifying an EXISTING user in database * putUser : modifying an EXISTING user in database
* deleteUser : deleting an user from database * deleteUser : deleting an user from database
*/ */
/*
/// Brief : Returning an array of users containing all the user stored in database
public function getUsers():array{
$tabUser=NULL;
$query1= "SELECT * FROM User";
$query2="SELECT idSkin FROM HasSkin WHERE idUser=:idUser";
$this->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 /// Brief : Returning an user found in database with his id
/// Parameters : * $id (string): identifier of the user we are looking for /// Parameters : * $id (string): identifier of the user we are looking for
public function getUserById(string $id):?User{ public function getUserById(string $id):?User{
@ -107,6 +92,17 @@ class UserGateway{
return $usr; 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 /// Brief : Adding a NEW user in database
/// Parameters : * $u (User): user we want to insert in database /// Parameters : * $u (User): user we want to insert in database
public function postUser(User $u): void{ public function postUser(User $u): void{

@ -7,6 +7,7 @@
include ('dbConnection.php'); include ('dbConnection.php');
include ('gateways/userGateway.php'); include ('gateways/userGateway.php');
include ('gateways/matchGateway.php'); include ('gateways/matchGateway.php');
include ('gateways/conversationGataway.php');
// Connection to database // Connection to database
// A changer quand la base de données sera hébergée, comment masquer les var? // A changer quand la base de données sera hébergée, comment masquer les var?
@ -20,6 +21,14 @@
// Initializing Gateways // Initializing Gateways
$usergw = new UserGateway($database); $usergw = new UserGateway($database);
$matchgw = new MatchGateway($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 // Managing request, routing and sending responses
/* /*
@ -47,9 +56,7 @@
$res=$usergw->getUserByUsername($username); $res=$usergw->getUserByUsername($username);
} }
else{ else{
// read all users
$res= $usergw->getUsers();
echo json_encode($res);
} }
break; break;
case 'getMatch': case 'getMatch':

@ -0,0 +1,19 @@
<?php
class Conversation{
// Object attributes
public string $id;
public string $name;
public $listMessages;
public $listIdUsers;
public function __construct($_id,$_name,$_listMessages,$_listIdUsers){
$this->id=$_id;
$this->name=$_name;
$this->listMessages=$_listMessages;
$this->listIdUsers=$_listIdUsers;
}
}
?>

@ -14,7 +14,7 @@ class Matchs{
$this->id=$_id; $this->id=$_id;
$this->inGame=$_inGame; $this->inGame=$_inGame;
$this->idGame=$_idGame; $this->idGame=$_idGame;
// Only one user at the moment of the creation // Only one user at creation
$this->listIdUsers=$_listIdUsers; $this->listIdUsers=$_listIdUsers;
} }
} }

@ -0,0 +1,17 @@
<?php
class Message {
// Object attributes
public String $id;
public String $message;
public String $idSender;
public function __construct(String $_id, String $_message, String $_idSender){
$this->id=$_id;
$this->message=$_message;
$this->idSender=$_idSender;
}
}
?>
Loading…
Cancel
Save