From 5c0ad12fd991d8fca156a6a12448c0819ad14f7d Mon Sep 17 00:00:00 2001 From: Lucie Bedouret Date: Tue, 22 Nov 2022 11:57:18 +0100 Subject: [PATCH] ADD : skin and games methods --- api-rest/gateways/conversationGataway.php | 12 +++--- api-rest/gateways/gameGateway.php | 46 +++++++++++++++++++++++ api-rest/gateways/matchGateway.php | 10 ++--- api-rest/gateways/skinGateway.php | 45 ++++++++++++++++++++++ api-rest/gateways/userGateway.php | 26 ++++++------- api-rest/index.php | 19 +++++++--- api-rest/model/game.php | 17 +++++++++ api-rest/model/skin.php | 16 ++++++++ api-rest/model/user.php | 6 +-- 9 files changed, 164 insertions(+), 33 deletions(-) create mode 100644 api-rest/gateways/gameGateway.php create mode 100644 api-rest/gateways/skinGateway.php create mode 100644 api-rest/model/game.php create mode 100644 api-rest/model/skin.php diff --git a/api-rest/gateways/conversationGataway.php b/api-rest/gateways/conversationGataway.php index c49e74c..ee2d4a9 100644 --- a/api-rest/gateways/conversationGataway.php +++ b/api-rest/gateways/conversationGataway.php @@ -33,8 +33,8 @@ class ConversationGateway{ $tabIdMessages=NULL; $tabMessages=NULL; - $query1 = "SELECT idConversation FROM InConversation WHERE idUser=:idUser"; - $query2 = "SELECT idUser FROM InConversation WHERE idConversation=:idConv"; + $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"; @@ -88,7 +88,7 @@ class ConversationGateway{ /// 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)"; + $query2 = "INSERT INTO Use VALUES(:idUser,:idConv)"; $arg1 = array('idConv'=>array($c->id,PDO::PARAM_STR), 'name'=>array($c->name, PDO::PARAM_STR)); @@ -108,11 +108,11 @@ class ConversationGateway{ $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"; + $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 InConversation VALUES(:idUsr,:idConv)"; + $query6 = "INSERT INTO Use VALUES(:idUsr,:idConv)"; $arg1 = array('idConv'=>array($c->id,PDO::PARAM_STR)); @@ -152,7 +152,7 @@ class ConversationGateway{ 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"; + $query3 = "DELETE FROM Use WHERE idConversation = :idConv"; $query4 = "DELETE FROM Conversation WHERE id = :idConv"; foreach($c->listMessages as $msg){ diff --git a/api-rest/gateways/gameGateway.php b/api-rest/gateways/gameGateway.php new file mode 100644 index 0000000..53c0600 --- /dev/null +++ b/api-rest/gateways/gameGateway.php @@ -0,0 +1,46 @@ +connection=$_connection; + } + + /* Functions implemented to manage games' data from database + * getGames : returning all the games found in database + * getGameById : returning a game found in database with its id + */ + +/// Brief : Returning all the games found in database + public function getGames():?array{ + $tabGames=null; + $query="SELECT * FROM Game"; + $this->connection->execQuery($query,[]); + $res = $this->connection->getRes(); + foreach($res as $row){ + $tabGames[]= new Game($row['id'],$row['name'],$row['image']); + } + return $tabGames; + } + +/// Brief : Returning a game found in database with its id +/// Parameters : * $id (string): identifier of the game we are looking for + public function getGameById(string $id):?Game{ + $game=null; + $query="SELECT * FROM Game WHERE id=:id"; + $arg=array('id'=>array($id,PDO::PARAM_STR)); + $this->connection->execQuery($query,$arg); + $res=$this->connection->getRes(); + foreach($res as $row){ + $game= new Game($row['id'],$row['name'],$row['image']); + } + return $game; + } +} + +?> \ No newline at end of file diff --git a/api-rest/gateways/matchGateway.php b/api-rest/gateways/matchGateway.php index 638b3b0..b5f8eea 100644 --- a/api-rest/gateways/matchGateway.php +++ b/api-rest/gateways/matchGateway.php @@ -25,7 +25,7 @@ class MatchGateway{ public function getMatch(string $matchId):?Matchs{ $match=NULL; $query1="SELECT id, inGame, idGame FROM Matchs WHERE id = :id"; - $query2="SELECT idUser FROM InMatch WHERE idMatch=:id"; + $query2="SELECT idUser FROM Play WHERE idMatch=:id"; $arg=array('id' => array($matchId, PDO::PARAM_STR)); $this->connection->execQuery($query2, $arg); $res=$this->connection->getRes(); @@ -45,7 +45,7 @@ class MatchGateway{ /// Parameters : * $u (Matchs): match we want to insert in database public function postMatch(Matchs $m){ $query1="INSERT INTO Matchs VALUES(:idMatch,0,:idGame)"; - $query2="INSERT INTO InMatch VALUES(:idMatch,:idUser)"; + $query2="INSERT INTO Play VALUES(:idMatch,:idUser)"; $arg1=array('idMatch'=>array($m->id, PDO::PARAM_STR), 'idGame'=>array($m->idGame, PDO::PARAM_STR)); $this->connection->execQuery($query1,$arg1); @@ -62,8 +62,8 @@ class MatchGateway{ public function putMatch(Matchs $m){ $query1="UPDATE Matchs SET inGame= :inGame WHERE id=:id"; //Peut-etre la possibilité de faire mieux??? - $query2="DELETE FROM InMatch WHERE idMatch=:idMatch"; - $query3="INSERT INTO InMatch VALUES(:idMatch,:idUser)"; + $query2="DELETE FROM Play WHERE idMatch=:idMatch"; + $query3="INSERT INTO Play VALUES(:idMatch,:idUser)"; $arg1=array('inGame'=>array($m->inGame, PDO::PARAM_BOOL), 'id'=>array($m->id,PDO::PARAM_STR)); $arg2=array('idMatch'=>array($m->id,PDO::PARAM_STR)); @@ -80,7 +80,7 @@ class MatchGateway{ /// Brief : Deleting a match from database /// Parameters : * $u (Matchs): match we want to delete from database public function deleteMatch(Matchs $m){ - $query1="DELETE FROM InMatch WHERE idMatch=:id"; + $query1="DELETE FROM Play WHERE idMatch=:id"; $query2="DELETE FROM Matchs WHERE id=:id"; $arg=array('id'=>array($m->id, PDO::PARAM_STR)); $this->connection->execQuery($query1,$arg); diff --git a/api-rest/gateways/skinGateway.php b/api-rest/gateways/skinGateway.php new file mode 100644 index 0000000..cde2b6f --- /dev/null +++ b/api-rest/gateways/skinGateway.php @@ -0,0 +1,45 @@ +connection=$_connection; + } + + /* Functions implemented to manage skins' data from database + * getGames : returning all the skins found in database + * getGameById : returning a skin found in database with its id + */ + +/// Brief : Returning all the skins found in database +public function getSkins():?array{ + $tabSkins=null; + $query="SELECT * FROM Skin"; + $this->connection->execQuery($query,[]); + $res = $this->connection->getRes(); + foreach($res as $row){ + $tabSkins[]= new Game($row['id'],$row['name'],$row['image']); + } + return $tabSkins; +} + +/// Brief : Returning a skin found in database with its id +/// Parameters : * $id (string): identifier of the skin we are looking for +public function getSkinById(string $id):?Game{ + $skin=null; + $query="SELECT * FROM Skin WHERE id=:id"; + $arg=array('id'=>array($id,PDO::PARAM_STR)); + $this->connection->execQuery($query,$arg); + $res=$this->connection->getRes(); + foreach($res as $row){ + $skin= new Game($row['id'],$row['name'],$row['image']); + } + return $skin; +} +} + +?> \ No newline at end of file diff --git a/api-rest/gateways/userGateway.php b/api-rest/gateways/userGateway.php index 9f4ea3a..4c87f64 100644 --- a/api-rest/gateways/userGateway.php +++ b/api-rest/gateways/userGateway.php @@ -13,8 +13,8 @@ class UserGateway{ /* Functions implemented to manage user's data from database * getUsers : returning an array of users containing all the user stored in database - * getUserById : returning an user found in database with his id - * getUserByUsername : returning an user found in database with his username + * getUserById : returning an user found in database with its id + * getUserByUsername : returning an user found in database with its username * getUserForConnection : returning an user if there is a correspondance between the username and the password, used for connection * getLastId : returning the last Id of the users @@ -29,7 +29,7 @@ class UserGateway{ public function getUserById(string $id):?User{ $usr=NULL; $query= "SELECT * FROM User U WHERE id = :id "; - $query2="SELECT idSkin FROM HasSkin WHERE idUser=:id"; + $query2="SELECT idSkin FROM Own WHERE idUser=:id"; $arg= array('id'=> array($id,PDO::PARAM_STR)); $this->connection->execQuery($query2,$arg); $res=$this->connection->getRes(); @@ -39,7 +39,7 @@ class UserGateway{ $this->connection->execQuery($query,$arg); $res=$this->connection->getRes(); foreach($res as $row){ - $usr = new User ($row['id'],$row['username'],$row['password'],$row['nationality'],$row['sex'],$row['dateOfBirth'],$row['currentBobCoins'],$row['totalBobCoins'],$row['nbGamesPlayed'],$row['currentIdSkin'],$tabSkin); + $usr = new User ($row['id'],$row['username'],$row['password'],$row['nationality'],$row['sex'],$row['dateOfBirth'],$row['currentBobCoins'],$row['totalBobCoins'],$row['nbGamesPlayed'],$row['currentSkin'],$tabSkin); } return $usr; } @@ -50,12 +50,12 @@ class UserGateway{ $usr=NULL; $query= "SELECT * FROM User U WHERE username = :username "; - $query2="SELECT idSkin FROM HasSkin WHERE idUser=:id"; + $query2="SELECT idSkin FROM Own WHERE idUser=:id"; $arg = array('username'=>array($username,PDO::PARAM_STR)); $this->connection->execQuery($query,$arg); $res=$this->connection->getRes(); foreach($res as $row){ - $usr = new User ($row['id'],$row['username'],$row['password'],$row['nationality'],$row['sex'],$row['dateOfBirth'],$row['currentBobCoins'],$row['totalBobCoins'],$row['nbGamesPlayed'],$row['currentIdSkin'],null); + $usr = new User ($row['id'],$row['username'],$row['password'],$row['nationality'],$row['sex'],$row['dateOfBirth'],$row['currentBobCoins'],$row['totalBobCoins'],$row['nbGamesPlayed'],$row['currentSkin'],null); } $arg2=array('id'=>array($usr->id, PDO::PARAM_STR)); $this->connection->execQuery($query2,$arg2); @@ -75,12 +75,12 @@ class UserGateway{ public function getUserForConnection(string $username, string $password):?User{ $usr=NULL; $query= "SELECT * FROM User U WHERE username = :username AND password = :password"; - $query2="SELECT idSkin FROM HasSkin WHERE idUser=:id"; + $query2="SELECT idSkin FROM Own WHERE idUser=:id"; $arg = array('username'=>array($username,PDO::PARAM_STR),'password'=>array($password,PDO::PARAM_STR)); $this->connection->execQuery($query,$arg); $res=$this->connection->getRes(); foreach($res as $row){ - $usr = new User ($row['id'],$row['username'],$row['password'],$row['nationality'],$row['sex'],$row['dateOfBirth'],$row['currentBobCoins'],$row['totalBobCoins'],$row['nbGamesPlayed'],$row['currentIdSkin'],null); + $usr = new User ($row['id'],$row['username'],$row['password'],$row['nationality'],$row['sex'],$row['dateOfBirth'],$row['currentBobCoins'],$row['totalBobCoins'],$row['nbGamesPlayed'],$row['currentSkin'],null); } $arg2=array('id'=>array($usr->id, PDO::PARAM_STR)); $this->connection->execQuery($query2,$arg2); @@ -111,7 +111,7 @@ class UserGateway{ return; } $query = "INSERT INTO User VALUES (:id, :username, :password, :nationality, :sex, :dateOfBirth, 0, 0, 0, 'S0001')"; - $query2 = "INSERT INTO HasSkin VALUES(:id,'S0001')"; + $query2 = "INSERT INTO Own VALUES(:id,'S0001')"; $arg=array('id' => array($u->id, PDO::PARAM_STR), 'username' => array($u->username, PDO::PARAM_STR), 'password' => array($u->password, PDO::PARAM_STR), @@ -126,9 +126,9 @@ class UserGateway{ /// Brief : Modifying an EXISTING user in database /// Parameters : * $u (User): user we want to update in database public function putUser(User $u){ - $query="UPDATE User SET username = :username, password=:password, sex=:sex, nationality=:nationality, currentBobCoins=:currentBobCoins, totalBobCoins=:totalBobCoins, nbGamesPlayed=:nbGamesPlayed, currentIdSkin=:currentIdSkin WHERE id=:id"; - $query2="DELETE FROM HasSkin WHERE idUser=:id"; - $query3="INSERT INTO HasSkin VALUES(:idUsr,:idSkin)"; + $query="UPDATE User SET username = :username, password=:password, sex=:sex, nationality=:nationality, currentBobCoins=:currentBobCoins, totalBobCoins=:totalBobCoins, nbGamesPlayed=:nbGamesPlayed, currentSkin=:currentSkin WHERE id=:id"; + $query2="DELETE FROM Own WHERE idUser=:id"; + $query3="INSERT INTO Own VALUES(:idUsr,:idSkin)"; $arg=array(':id' => array($u->id, PDO::PARAM_STR), ':username' => array($u->username, PDO::PARAM_STR), ':password' => array($u->password, PDO::PARAM_STR), @@ -137,7 +137,7 @@ class UserGateway{ ':currentBobCoins' => array($u->currentBobCoins, PDO::PARAM_INT), ':totalBobCoins' => array($u->totalBobCoins, PDO::PARAM_INT), ':nbGamesPlayed' => array($u->nbGamesPlayed, PDO::PARAM_INT), - ':currentIdSkin'=> array($u->currentIdSkin, PDO::PARAM_STR)); + ':currentSkin'=> array($u->currentSkin, PDO::PARAM_STR)); $arg2=array('id'=>array($u->id,PDO::PARAM_STR)); $this->connection->execQuery($query, $arg); $this->connection->execQuery($query2,$arg2); diff --git a/api-rest/index.php b/api-rest/index.php index 7e1fe64..8863f10 100644 --- a/api-rest/index.php +++ b/api-rest/index.php @@ -8,6 +8,8 @@ include ('gateways/userGateway.php'); include ('gateways/matchGateway.php'); include ('gateways/conversationGataway.php'); + include ('gateways/gameGateway.php'); + include ('gateways/skinGateway.php'); // Connection to database // A changer quand la base de données sera hébergée, comment masquer les var? @@ -22,20 +24,25 @@ $usergw = new UserGateway($database); $matchgw = new MatchGateway($database); $conversationgw = new ConversationGateway($database); + $gamegw = new GameGateway($database); + $skingw = new SkinGateway($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); + // Testing + $res=$gamegw->getGames(); + echo json_encode($res); + $res=$gamegw->getGameById("G0002"); + echo json_encode($res); + $res=$skingw->getSkins(); + echo json_encode($res); + $res=$skingw->getSkinById("S0001"); + echo json_encode($res); // Managing request, routing and sending responses /* $requestMethod = $_SERVER['REQUEST_METHOD']; $requestName = $_REQUEST['fname']; - if(empty($requestName)){ header("HTTP/1.0 400 Request Name Empty"); http_response_code(400); diff --git a/api-rest/model/game.php b/api-rest/model/game.php new file mode 100644 index 0000000..64fedd3 --- /dev/null +++ b/api-rest/model/game.php @@ -0,0 +1,17 @@ +id=$_id; + $this->name=$_name; + $this->image=$_image; + } + +} + +?> \ No newline at end of file diff --git a/api-rest/model/skin.php b/api-rest/model/skin.php new file mode 100644 index 0000000..f4ce795 --- /dev/null +++ b/api-rest/model/skin.php @@ -0,0 +1,16 @@ +id=$_id; + $this->name=$_name; + $this->image=$_image; + } +} + +?> \ No newline at end of file diff --git a/api-rest/model/user.php b/api-rest/model/user.php index 50b3fc3..2ef6275 100644 --- a/api-rest/model/user.php +++ b/api-rest/model/user.php @@ -12,10 +12,10 @@ class User { public int $currentBobCoins; public int $totalBobCoins; public int $nbGamesPlayed; - public string $currentIdSkin; + public string $currentSkin; public $listIdSkin; - public function __construct(string $_id,string $_username,string $_password, string $_nationality,string $_sex, string $_dateOfBirth, int $_currentBobCoins, int $_totalBobCoins, int $_nbGamesPlayed, string $_currentIdSkin, $_listIdSkin){ + public function __construct(string $_id,string $_username,string $_password, string $_nationality,string $_sex, string $_dateOfBirth, int $_currentBobCoins, int $_totalBobCoins, int $_nbGamesPlayed, string $_currentSkin, $_listIdSkin){ $this->id=$_id; $this->username=$_username; $this->password=$_password; @@ -25,7 +25,7 @@ class User { $this->currentBobCoins=$_currentBobCoins; $this->totalBobCoins=$_totalBobCoins; $this->nbGamesPlayed=$_nbGamesPlayed; - $this->currentIdSkin=$_currentIdSkin; + $this->currentSkin=$_currentSkin; $this->listIdSkin=$_listIdSkin; }