Commit 7a3492af authored by Robin Borgogno's avatar Robin Borgogno

ajout Game et UserGame au MVC

parent 2fbd94d1
<?php
include_once __ROOT_DIR . '/libs/php-jwt/src/BeforeValidException.php';
include_once __ROOT_DIR . '/libs/php-jwt/src/ExpiredException.php';
include_once __ROOT_DIR . '/libs/php-jwt/src/SignatureInvalidException.php';
include_once __ROOT_DIR . '/libs/php-jwt/src/JWT.php';
use \Firebase\JWT\JWT;
class GameController extends Controller {
public function __construct($name, $request) {
parent::__construct($name, $request);
}
// ==============
// Actions
// ==============
public function processRequest()
{
switch ($this->request->getHttpMethod()) {
case 'GET':
$id = $this->request->getURIParams()[0];
return $this->getGame($id);
break;
case 'POST':
$data = json_decode(file_get_contents("php://input"),TRUE);
return $this->createGame($data);
break;
case 'PUT':
$id = $this->request->getURIParams()[0];
$data = json_decode(file_get_contents("php://input"),TRUE);
return $this->updateGame($id, $data);
break;
case 'DELETE':
$id = $this->request->getURIParams()[0];
return $this->deleteGame($id);
break;
}
return Response::errorResponse("unsupported parameters or method in game");
}
public function getGame($id)
{
$game = Game::getGame($id);
if($game == Array()){
$response = Response::errorInParametersResponse("Game not found");
}else{
$response = new Response(200,json_encode($game));
}
return $response;
}
public function createGame($data)
{
if(array_key_exists("GAME_DESC", $data)){
$game = Game::createGame(array( ":GAME_DESC" => $data['GAME_DESC']));
$response = new Response(200,json_encode($game));
}else{
$response = Response::errorInParametersResponse("Parameters missing");
}
return $response;
}
public function updateGame($id, $data)
{
$gameValues = Game::getGame($id);
$gameValues=($gameValues[0]);
if($gameValues == []){
$response = Response::errorResponse("Game not found");
return $response;
}else{
if(array_key_exists('GAME_DESC',$data)){
$desc = $data['GAME_DESC'];
}else{
$desc = $gameValues->GAME_DESC;
}
$game = Game::updateGame(array(":GAME_ID" => $id, ":GAME_DESC" => $desc));
$response = new Response(200,json_encode($game));
return $response;
}
}
public function deleteGame($id)
{
$game = Game::getGame($id);
if($game == Array()){
$response = Response::errorInParametersResponse(json_encode("Game not found"));
}else{
$res = Game::deleteGame($id);
$response = Response::okResponse(json_encode($res));
}
return $response;
}
}
<?php
include_once __ROOT_DIR . '/libs/php-jwt/src/BeforeValidException.php';
include_once __ROOT_DIR . '/libs/php-jwt/src/ExpiredException.php';
include_once __ROOT_DIR . '/libs/php-jwt/src/SignatureInvalidException.php';
include_once __ROOT_DIR . '/libs/php-jwt/src/JWT.php';
use \Firebase\JWT\JWT;
class GamesController extends Controller {
public function __construct($name, $request) {
parent::__construct($name, $request);
}
// ==============
// Actions
// ==============
public function processRequest()
{
switch ($this->request->getHttpMethod()) {
case 'GET':
return $this->getList();
break;
}
return Response::errorResponse("unsupported parameters or method in game");
}
public function getList()
{
$games = Game::getList();
$response = new Response(200,json_encode($games));
return $response;
}
}
<?php
include_once __ROOT_DIR . '/libs/php-jwt/src/BeforeValidException.php';
include_once __ROOT_DIR . '/libs/php-jwt/src/ExpiredException.php';
include_once __ROOT_DIR . '/libs/php-jwt/src/SignatureInvalidException.php';
include_once __ROOT_DIR . '/libs/php-jwt/src/JWT.php';
use \Firebase\JWT\JWT;
class GameuserController extends Controller {
public function __construct($name, $request) {
parent::__construct($name, $request);
}
// ==============
// Actions
// ==============
public function processRequest()
{
switch ($this->request->getHttpMethod()) {
case 'GET':
switch($this->request->getURIParams()[0])
{
case 'user':
$id = $this->request->getURIParams()[1];
return $this->getGamesByUser($id);
break;
case 'game':
$id = $this->request->getURIParams()[1];
return $this->getUsersbyGame($id);
break;
}
break;
case 'POST':
$data = json_decode(file_get_contents("php://input"),TRUE);
return $this->addUserGame($data);
break;
case 'DELETE':
$data = json_decode(file_get_contents("php://input"),TRUE);
return $this->deleteUserGame($data);
break;
}
return Response::errorResponse("unsupported parameters or method in game");
}
public function getGamesByUser($id)
{
$games = User::getGames($id);
$response = new Response(200,json_encode($games));
return $response;
}
public function getUsersbyGame($id)
{
$users = Game::getUsers($id);
$response = new Response(200,json_encode($users));
return $response;
}
public function addUserGame($data)
{
$idLigne = Game::addUserGame(array(":GAME_ID" => $data['GAME_ID'], ":USER_ID" => $data['USER_ID']));
$response = new Response(200,json_encode("Joueur ajouté"));
return $response;
}
public function deleteUserGame($data)
{
$game = Game::deleteUserGame(array(":GAME_ID" => $data['GAME_ID'], ':USER_ID' => $data['USER_ID']));
$response = new Response(200, json_encode("Ligne supprimé"));
return $response;
}
}
...@@ -11,7 +11,7 @@ class LoginController extends Controller { ...@@ -11,7 +11,7 @@ class LoginController extends Controller {
parent::__construct($name, $request); parent::__construct($name, $request);
} }
public function processRequest() { public function processRequest() {
if($this->request->getHttpMethod() !== 'POST') if($this->request->getHttpMethod() !== 'POST')
return Response::errorResponse('{ "message" : "Unsupported endpoint" }' ); return Response::errorResponse('{ "message" : "Unsupported endpoint" }' );
......
<?php
class Game extends Model {
// ===========
// = Statics =
// ===========
protected static $table_name = 'GAME';
public static function getList()
{
$stm = parent::exec('GAME_LIST');
return $stm->fetchAll();
}
public static function getGame($id)
{
$stm = parent::exec('GAME_BY_ID', array(':game_id' => $id));
return $stm->fetchAll();
}
public static function createGame($values)
{
$stm = parent::exec('GAME_CREATE',$values);
$stmbis = parent::exec('GAME_ID_MAX');
return $stmbis->fetchAll();
}
public static function updateGame($values)
{
$stm = parent::exec('GAME_UPDATE',$values);
return "La game a été uploader";
}
public static function deleteGame($id)
{
$stm = parent::exec('GAME_DELETE',array(":GAME_ID" => $id));
return "Game deleted";
}
public static function getUsers($id)
{
$stm = parent::exec('GAME_GET_USERS', array(":GAME_ID" => $id));
return $stm->fetchAll();
}
public static function addUserGame($values)
{
$stm = parent::exec('GAME_ADD_USER', $values);
return $stm;
}
public static function deleteUserGame($values)
{
$stm = parent::exec('GAME_DELETE_USER', $values);
return $stm;
}
}
\ No newline at end of file
...@@ -45,9 +45,15 @@ class User extends Model { ...@@ -45,9 +45,15 @@ class User extends Model {
// load the user with the login $login // load the user with the login $login
public static function tryLogin($login) public static function tryLogin($login)
{ {
$users = static::getListWithLogin($login); $users = static::getListWithLogin($login);
return $users[0]; return $users[0];
} }
public static function getGames($id)
{
$stm = parent::exec('USER_GET_GAMES', array(':USER_ID' => $id));
return $stm->fetchAll();
}
} }
\ No newline at end of file
<?php
Game::addSqlQuery('GAME_LIST',
"SELECT * FROM GAME ORDER BY GAME_ID");
Game::addSqlQuery('GAME_BY_ID',
"SELECT * FROM GAME WHERE GAME_ID=:game_id");
Game::addSqlQuery('GAME_CREATE',
"INSERT INTO GAME (GAME_ID, GAME_DESC) VALUES (NULL, :GAME_DESC)");
Game::addSqlQuery('GAME_ID_MAX',
"SELECT MAX(GAME_ID) FROM GAME");
Game::addSqlQuery('GAME_UPDATE',
"UPDATE GAME SET GAME_DESC=:GAME_DESC WHERE GAME_ID=:GAME_ID");
Game::addSqlQuery('GAME_DELETE',
"DELETE FROM GAME WHERE GAME_ID=:GAME_ID");
Game::addSqlQuery('GAME_GET_USERS',
"SELECT USER_ID FROM GAME_HISTO WHERE GAME_ID=:GAME_ID");
Game::addSqlQuery('GAME_ADD_USER',
"INSERT INTO GAME_HISTO (GAME_HISTO_ID, GAME_ID, USER_ID) VALUES (NULL, :GAME_ID, :USER_ID)");
Game::addSqlQuery('GAME_DELETE_USER',
"DELETE FROM GAME_HISTO WHERE (GAME_ID=:GAME_ID AND USER_ID=:USER_ID)");
\ No newline at end of file
...@@ -20,3 +20,6 @@ User::addSqlQuery('USER_DELETE', ...@@ -20,3 +20,6 @@ User::addSqlQuery('USER_DELETE',
User::addSqlQuery('USER_CREATE', User::addSqlQuery('USER_CREATE',
"INSERT INTO USER (USER_ID, USER_LOGIN, USER_EMAIL, USER_ROLE, USER_PASSWORD, USER_FIRSTNAME, USER_LASTNAME) VALUES (NULL, :login, :email, :role, :password, :firstname, :lastname)"); "INSERT INTO USER (USER_ID, USER_LOGIN, USER_EMAIL, USER_ROLE, USER_PASSWORD, USER_FIRSTNAME, USER_LASTNAME) VALUES (NULL, :login, :email, :role, :password, :firstname, :lastname)");
User::addSqlQuery('USER_GET_GAMES',
"SELECT GAME_ID FROM GAME_HISTO WHERE USER_ID=:USER_ID");
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment