From 7b2e7b2c98b3bf25522ee9c70e599a0d64f18ef8 Mon Sep 17 00:00:00 2001
From: SASSU Thomas <thomas.sassu5@etu.univ-lorraine.fr>
Date: Tue, 24 Mar 2020 15:52:54 +0100
Subject: [PATCH] =?UTF-8?q?Partie=208=20et=20autres=20fonctionalit=C3=A9s?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

---
 GamePedia/index.php                           | 22 ++++++++-
 .../src/controleurs/ControleurComment.php     | 49 +++++++++++++++++++
 GamePedia/src/controleurs/ControleurGame.php  |  7 ++-
 .../src/controleurs/ControleurPlatform.php    |  2 +-
 GamePedia/src/modeles/Game.php                |  2 +-
 GamePedia/src/vues/VueComment.php             | 16 ++++++
 6 files changed, 94 insertions(+), 4 deletions(-)
 create mode 100644 GamePedia/src/controleurs/ControleurComment.php
 create mode 100644 GamePedia/src/vues/VueComment.php

diff --git a/GamePedia/index.php b/GamePedia/index.php
index 3bb1506..354001f 100644
--- a/GamePedia/index.php
+++ b/GamePedia/index.php
@@ -7,6 +7,7 @@ use \gp\controleurs\ControleurPrincipal as ControleurPrincipal;
 use \gp\controleurs\ControleurGame as ControleurGame;
 use \gp\controleurs\ControleurPlatform as ControleurPlatform;
 use \gp\controleurs\ControleurCharacter as ControleurCharacter;
+use \gp\controleurs\ControleurComment as ControleurComment;
 
 $db = new DB();
 $db->addConnection(parse_ini_file('./conf/conf.ini'));
@@ -55,7 +56,12 @@ $app->get('/api/games/:id/platforms',function ($id) use ($app){
     $c = new ControleurGame();
     $c->getGamePlatforms($id);
 });
-
+//Post
+$app->post('/api/games/:id/comments',function ($id) use ($app){
+    $app->response->headers->set('Content-Type', 'application/json');
+    $c = new ControleurComment();
+    $c->postComment($id);
+})->name('postComment');
 /* ------------------------------------------------------------
  * PLATFORMS
  * ------------------------------------------------------------
@@ -84,5 +90,19 @@ $app->get('/api/characters/',function () use ($app){
     $c = new ControleurCharacter();
     $c->getCharacters();
 })->name('getCharacters');
+/* ------------------------------------------------------------
+ * COMMENT
+ * ------------------------------------------------------------
+ */
+$app->get('/api/comments/:id',function ($id) use ($app){
+    $app->response->headers->set('Content-Type', 'application/json');
+    $c = new ControleurComment();
+    $c->getComment($id);
+})->name('getComment');
+$app->get('/api/comments/',function () use ($app){
+    $app->response->headers->set('Content-Type', 'application/json');
+    $c = new ControleurComment();
+    $c->getComments();
+})->name('getComments');
 
 $app->run();
\ No newline at end of file
diff --git a/GamePedia/src/controleurs/ControleurComment.php b/GamePedia/src/controleurs/ControleurComment.php
new file mode 100644
index 0000000..85b8819
--- /dev/null
+++ b/GamePedia/src/controleurs/ControleurComment.php
@@ -0,0 +1,49 @@
+<?php
+
+namespace gp\controleurs;
+
+use gp\modeles\Comment;
+use gp\vues\VueComment;
+use Slim\Slim;
+
+class ControleurComment
+{
+    public function getComment($id){
+        $comment = Comment::where('id', '=', $id)->first();
+        $v = new VueComment(json_encode($comment));
+        $v->render();
+    }
+    public function getComments() {
+        $results = null;
+        if(isset($_GET['page'])) {
+            $page = filter_var($_GET['page'], FILTER_SANITIZE_NUMBER_INT);
+            ini_set('memory_limit', '512M');
+            $response = Comment::paginate(50, ['*'], 'page', $page);
+            $json = (json_decode(json_encode($response), true));
+            $route = explode('bdappli_laurent_vonderscher_sassu_percin/GamePedia', Slim::getInstance()->urlFor('getComments'));
+            foreach ($json['data'] as $comment) {
+                $url = $route[1] . "{$comment['id']}";
+                $results['comments'][] = ["comment" => $comment, "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/comments{$prev}"], "next" => ["href" => "/api/comments{$next}"]];
+            $results['links'] = $links;
+        }
+        $v = new VueComment(json_encode($results));
+        $v->render();
+    }
+    public function postComment($id){
+        // Takes raw data from the request
+        $json = file_get_contents('php://input');
+        $data = json_decode($json, true);
+        if (isset($data) && $data != null && isset($data['email']) && isset($data['title']) && isset($data['content'])) {
+                //sans filter var pour les test
+                $results = Comment::create($id, $data['email'], $data['title'], $data['content'], date("Y-m-d H:i:s"));
+                $route = explode('bdappli_laurent_vonderscher_sassu_percin/GamePedia', Slim::getInstance()->urlFor('getComments'));
+                Slim::getInstance()->response->redirect($route[1].$results->id, 201);
+        } else Slim::getInstance()->response->setStatus(500);
+    }
+}
\ No newline at end of file
diff --git a/GamePedia/src/controleurs/ControleurGame.php b/GamePedia/src/controleurs/ControleurGame.php
index ee7e474..2a296bd 100644
--- a/GamePedia/src/controleurs/ControleurGame.php
+++ b/GamePedia/src/controleurs/ControleurGame.php
@@ -47,7 +47,12 @@ class ControleurGame
     public function getGameComments($id){
         $game = Game::where('id', '=', $id)->first();
         $comments = $game->comments()->get();
-        $v = new VueGame(json_encode($comments));
+        $route = explode('bdappli_laurent_vonderscher_sassu_percin/GamePedia',Slim::getInstance()->urlFor('getComments'));
+        foreach ($comments as $comment) {
+            $url = $route[1]."{$comment['id']}";
+            $results['comments'][] = ["comment" => $comment, "links" => ["self" => ["href" => $url]]];
+        }
+        $v = new VueGame(json_encode($results));
         $v->render('getComments');
     }
 
diff --git a/GamePedia/src/controleurs/ControleurPlatform.php b/GamePedia/src/controleurs/ControleurPlatform.php
index 9fadf91..7450427 100644
--- a/GamePedia/src/controleurs/ControleurPlatform.php
+++ b/GamePedia/src/controleurs/ControleurPlatform.php
@@ -2,7 +2,7 @@
 
 namespace gp\controleurs;
 
-use gp\modeles\Platform;
+use gp\modeles\Comment;
 use gp\vues\VuePlatform;
 use Slim\Slim;
 
diff --git a/GamePedia/src/modeles/Game.php b/GamePedia/src/modeles/Game.php
index 120feec..5b84016 100644
--- a/GamePedia/src/modeles/Game.php
+++ b/GamePedia/src/modeles/Game.php
@@ -11,7 +11,7 @@ class Game extends \Illuminate\Database\Eloquent\Model
         return $this->belongsToMany('gp\modeles\Character', 'game2character','game_id');
     }
     public function platforms(){
-        return $this->belongsToMany('gp\modeles\Platform', 'game2platform','game_id');
+        return $this->belongsToMany('gp\modeles\Comment', 'game2platform','game_id');
     }
     public function themes(){
         return $this->belongsToMany('gp\modeles\Theme', 'game2theme','game_id');
diff --git a/GamePedia/src/vues/VueComment.php b/GamePedia/src/vues/VueComment.php
new file mode 100644
index 0000000..7c89eb8
--- /dev/null
+++ b/GamePedia/src/vues/VueComment.php
@@ -0,0 +1,16 @@
+<?php
+
+namespace gp\vues;
+
+class VueComment
+{
+    public $tab;
+
+    public function __construct($tableau) {
+        $this->tab = $tableau;
+    }
+
+    public function render() {
+        echo $this->tab;
+    }
+}
\ No newline at end of file
-- 
GitLab