From 5ea3ce00d4a5016808cab4dad8f0070d3fec359c Mon Sep 17 00:00:00 2001
From: SASSU Thomas <thomas.sassu5@etu.univ-lorraine.fr>
Date: Tue, 24 Mar 2020 13:13:14 +0100
Subject: [PATCH] ajout characters platforms et erreur slim $_GET[page]
 inexistant et ajoiut des differents controleurs et vues

---
 GamePedia/index.php                           | 54 ++++++++++++++++---
 .../src/controleurs/ControleurCharacter.php   | 38 +++++++++++++
 GamePedia/src/controleurs/ControleurGame.php  | 37 ++++++++-----
 .../src/controleurs/ControleurPlatform.php    | 38 +++++++++++++
 GamePedia/src/vues/VueCharacter.php           | 16 ++++++
 GamePedia/src/vues/VuePlatform.php            | 16 ++++++
 6 files changed, 180 insertions(+), 19 deletions(-)
 create mode 100644 GamePedia/src/controleurs/ControleurCharacter.php
 create mode 100644 GamePedia/src/controleurs/ControleurPlatform.php
 create mode 100644 GamePedia/src/vues/VueCharacter.php
 create mode 100644 GamePedia/src/vues/VuePlatform.php

diff --git a/GamePedia/index.php b/GamePedia/index.php
index 9595c01..3bb1506 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 0000000..6c73769
--- /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 e45a380..decabf8 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 0000000..9fadf91
--- /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 0000000..cab4d16
--- /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 0000000..8ed316f
--- /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
-- 
GitLab