diff --git a/GamePedia/index.php b/GamePedia/index.php index 3bb1506680823df2ea3c12912df488fd728c466c..354001fbf21f18f1c85b7e12d80480d489351d3e 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 0000000000000000000000000000000000000000..85b8819b727022be606ef5feb4095fb58cb2b525 --- /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 ee7e4742122a66e49cc8e023d3f5733fb9e9f1d0..2a296bd58d40203261dbfc9e766d432deb21c806 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 9fadf916c84a31c82498175872468df5b64e25f0..745042706ac091d10eacdbd30f71f69937357ea6 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 120feec11365b49662098cc65c71e767d1cabc91..5b8401649e1189932a41f385db976042b69160ee 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 0000000000000000000000000000000000000000..7c89eb8203c3a7461624fe81e56e4b225d39a2d2 --- /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