diff --git a/GamePedia/index.php b/GamePedia/index.php index 9595c01a4bc663d0d075be6a7200caa20ae6496d..3bb1506680823df2ea3c12912df488fd728c466c 100644 --- a/GamePedia/index.php +++ b/GamePedia/index.php @@ -5,7 +5,8 @@ use \Slim\Slim as Slim; use Illuminate\Database\Capsule\Manager as DB; use \gp\controleurs\ControleurPrincipal as ControleurPrincipal; use \gp\controleurs\ControleurGame as ControleurGame; -use \gp\controleurs\ControleurScript as ControleurScript; +use \gp\controleurs\ControleurPlatform as ControleurPlatform; +use \gp\controleurs\ControleurCharacter as ControleurCharacter; $db = new DB(); $db->addConnection(parse_ini_file('./conf/conf.ini')); @@ -14,7 +15,6 @@ $db->setAsGlobal(); $db->bootEloquent(); $app = new \Slim\Slim(); -$app->response->headers->set('Content-Type', 'application/json'); $app->get('/',function () use ($app) { $c = new ControleurPrincipal(); @@ -22,27 +22,67 @@ $app->get('/',function () use ($app) { $app->response->headers->set('Content-Type', 'text/html'); }); +/* ------------------------------------------------------------ + * GAMES + * ------------------------------------------------------------ + */ + // Affiche un jeu au fromat JSON -$app->get('/api/games/:id',function ($id) { +$app->get('/api/games/:id',function ($id) use ($app){ + $app->response->headers->set('Content-Type', 'application/json'); $c = new ControleurGame(); $c->getGame($id); })->name('getGame'); // Affiche tous les jeu paginé au format JSON -$app->get('/api/games/',function () { +$app->get('/api/games/',function () use ($app){ + $app->response->headers->set('Content-Type', 'application/json'); $c = new ControleurGame(); $c->getGames(); })->name('getGames'); // Affiche les commentaires d'un jeu -$app->get('/api/games/:id/comments',function ($id) { +$app->get('/api/games/:id/comments',function ($id) use ($app){ + $app->response->headers->set('Content-Type', 'application/json'); $c = new ControleurGame(); $c->getGameComments($id); }); -$app->get('/api/games/:id/characters',function ($id) { +$app->get('/api/games/:id/characters',function ($id) use ($app){ + $app->response->headers->set('Content-Type', 'application/json'); $c = new ControleurGame(); $c->getGameCharacters($id); }); -$app->get('/api/games/:id/platforms',function ($id) { +$app->get('/api/games/:id/platforms',function ($id) use ($app){ + $app->response->headers->set('Content-Type', 'application/json'); $c = new ControleurGame(); $c->getGamePlatforms($id); }); + +/* ------------------------------------------------------------ + * PLATFORMS + * ------------------------------------------------------------ + */ +$app->get('/api/platforms/:id',function ($id) use ($app){ + $app->response->headers->set('Content-Type', 'application/json'); + $c = new ControleurPlatform(); + $c->getPlatform($id); +})->name('getPlatform'); +$app->get('/api/platforms/',function () use ($app){ + $app->response->headers->set('Content-Type', 'application/json'); + $c = new ControleurPlatform(); + $c->getPlatforms(); +})->name('getPlatforms'); +/* ------------------------------------------------------------ + * CHARACTERS + * ------------------------------------------------------------ + */ +$app->get('/api/characters/:id',function ($id) use ($app){ + $app->response->headers->set('Content-Type', 'application/json'); + $c = new ControleurCharacter(); + $c->getCharacter($id); +})->name('getCharacter'); +$app->get('/api/characters/',function () use ($app){ + $app->response->headers->set('Content-Type', 'application/json'); + $c = new ControleurCharacter(); + $c->getCharacters(); +})->name('getCharacters'); + $app->run(); \ No newline at end of file diff --git a/GamePedia/src/controleurs/ControleurCharacter.php b/GamePedia/src/controleurs/ControleurCharacter.php new file mode 100644 index 0000000000000000000000000000000000000000..6c73769ecd563d3d1019d0d4a7659ed294628e63 --- /dev/null +++ b/GamePedia/src/controleurs/ControleurCharacter.php @@ -0,0 +1,38 @@ +<?php + +namespace gp\controleurs; + +use gp\modeles\Character; +use gp\vues\VueCharacter; +use Slim\Slim; + +class ControleurCharacter +{ + public function getCharacter($id){ + $character = Character::where('id', '=', $id)->first(); + $v = new VueCharacter(json_encode($character)); + $v->render(); + } + public function getcharacters() { + $results = null; + if(isset($_GET['page'])) { + $page = filter_var($_GET['page'], FILTER_SANITIZE_NUMBER_INT); + ini_set('memory_limit', '512M'); + $response = Character::paginate(200, ['*'], 'page', $page); + $json = (json_decode(json_encode($response), true)); + $route = explode('bdappli_laurent_vonderscher_sassu_percin/GamePedia', Slim::getInstance()->urlFor('getCharacters')); + foreach ($json['data'] as $character) { + $url = $route[1] . "{$character['id']}"; + $results['characters'][] = ["character" => $character, "links" => ["self" => ["href" => $url]]]; + } + $prev = $json['prev_page_url']; + $next = $json['next_page_url']; + if ($next == '') $next = $json['first_page_url']; + if ($prev == '') $prev = $json['last_page_url']; + $links = ["prev" => ["href" => "/api/platforms{$prev}"], "next" => ["href" => "/api/platforms{$next}"]]; + $results['links'] = $links; + } + $v = new VueCharacter(json_encode($results)); + $v->render(); + } +} \ No newline at end of file diff --git a/GamePedia/src/controleurs/ControleurGame.php b/GamePedia/src/controleurs/ControleurGame.php index e45a38038b67d85050923d2974b82cbd86a4b6ff..decabf8763a95d7397ae16f0de964a9ed0bf058e 100644 --- a/GamePedia/src/controleurs/ControleurGame.php +++ b/GamePedia/src/controleurs/ControleurGame.php @@ -14,24 +14,32 @@ class ControleurGame $results['game'] = $game; $results['links'] = [ "comments" =>["href" => $route."/comments"] , - "characters" =>["href" => $route."/characters"] + "characters" =>["href" => $route."/characters"], + "platforms" =>["href" => $route."/platforms"] ]; $v = new VueGame(json_encode($results)); $v->render('getGame'); } public function getGames(){ - $page = filter_var($_GET['page'], FILTER_SANITIZE_NUMBER_INT); - ini_set('memory_limit', '512M'); - $response = Game::paginate(200, ['*'], 'page', $page); - $json = (json_decode(json_encode($response),true)); - foreach ($json['data'] as $game) { - $route = explode('bdappli_laurent_vonderscher_sassu_percin/GamePedia',Slim::getInstance()->urlFor('getGames')); - $url = $route[1]."{$game['id']}"; - $results['games'][] = ["game" => $game, "links" => ["self" => ["href" => $url]]]; + $results = null; + if(isset($_GET['page'])) { + $page = filter_var($_GET['page'], FILTER_SANITIZE_NUMBER_INT); + ini_set('memory_limit', '512M'); + $response = Game::paginate(200, ['*'], 'page', $page); + $json = (json_decode(json_encode($response), true)); + $route = explode('bdappli_laurent_vonderscher_sassu_percin/GamePedia', Slim::getInstance()->urlFor('getGames')); + foreach ($json['data'] as $game) { + $url = $route[1] . "{$game['id']}"; + $results['games'][] = ["game" => $game, "links" => ["self" => ["href" => $url]]]; + } + $prev = $json['prev_page_url']; + $next = $json['next_page_url']; + if ($next == '') $next = $json['first_page_url']; + if ($prev == '') $prev = $json['last_page_url']; + $links = ["prev" => ["href" => "/api/games{$prev}"], "next" => ["href" => "/api/games{$next}"]]; + $results['links'] = $links; } - $links = ["prev" =>["href" => "/api/games{$json['prev_page_url']}"] , "next" =>["href" => "/api/games{$json['next_page_url']}"]]; - $results['links'] = $links; $v = new VueGame(json_encode($results)); $v->render('getGames'); } @@ -46,7 +54,12 @@ class ControleurGame public function getGamePlatforms($id){ $game = Game::where('id', '=', $id)->first(); $platforms = $game->platforms()->get(); - $v = new VueGame(json_encode($platforms)); + $route = explode('bdappli_laurent_vonderscher_sassu_percin/GamePedia',Slim::getInstance()->urlFor('getPlatforms')); + foreach ($platforms as $platform) { + $url = $route[1]."{$platform['id']}"; + $results['platforms'][] = ["platform" => $platform, "links" => ["self" => ["href" => $url]]]; + } + $v = new VueGame(json_encode($results)); $v->render('getPlatforms'); } } \ No newline at end of file diff --git a/GamePedia/src/controleurs/ControleurPlatform.php b/GamePedia/src/controleurs/ControleurPlatform.php new file mode 100644 index 0000000000000000000000000000000000000000..9fadf916c84a31c82498175872468df5b64e25f0 --- /dev/null +++ b/GamePedia/src/controleurs/ControleurPlatform.php @@ -0,0 +1,38 @@ +<?php + +namespace gp\controleurs; + +use gp\modeles\Platform; +use gp\vues\VuePlatform; +use Slim\Slim; + +class ControleurPlatform +{ + public function getPlatform($id){ + $platform = Platform::where('id', '=', $id)->first(); + $v = new VuePlatform(json_encode($platform)); + $v->render(); + } + public function getPlatforms() { + $results = null; + if(isset($_GET['page'])) { + $page = filter_var($_GET['page'], FILTER_SANITIZE_NUMBER_INT); + ini_set('memory_limit', '512M'); + $response = Platform::paginate(20, ['*'], 'page', $page); + $json = (json_decode(json_encode($response), true)); + $route = explode('bdappli_laurent_vonderscher_sassu_percin/GamePedia', Slim::getInstance()->urlFor('getPlatforms')); + foreach ($json['data'] as $platform) { + $url = $route[1] . "{$platform['id']}"; + $results['platforms'][] = ["platform" => $platform, "links" => ["self" => ["href" => $url]]]; + } + $prev = $json['prev_page_url']; + $next = $json['next_page_url']; + if ($next == '') $next = $json['first_page_url']; + if ($prev == '') $prev = $json['last_page_url']; + $links = ["prev" => ["href" => "/api/platforms{$prev}"], "next" => ["href" => "/api/platforms{$next}"]]; + $results['links'] = $links; + } + $v = new VuePlatform(json_encode($results)); + $v->render(); + } +} \ No newline at end of file diff --git a/GamePedia/src/vues/VueCharacter.php b/GamePedia/src/vues/VueCharacter.php new file mode 100644 index 0000000000000000000000000000000000000000..cab4d16b18afd53c3a7be70c2498425779f06b28 --- /dev/null +++ b/GamePedia/src/vues/VueCharacter.php @@ -0,0 +1,16 @@ +<?php + +namespace gp\vues; + +class VueCharacter +{ + public $tab; + + public function __construct($tableau) { + $this->tab = $tableau; + } + + public function render() { + echo $this->tab; + } +} \ No newline at end of file diff --git a/GamePedia/src/vues/VuePlatform.php b/GamePedia/src/vues/VuePlatform.php new file mode 100644 index 0000000000000000000000000000000000000000..8ed316fe790dbacedd019112f88cf4859d09e2aa --- /dev/null +++ b/GamePedia/src/vues/VuePlatform.php @@ -0,0 +1,16 @@ +<?php + +namespace gp\vues; + +class VuePlatform +{ + public $tab; + + public function __construct($tableau) { + $this->tab = $tableau; + } + + public function render() { + echo $this->tab; + } +} \ No newline at end of file