Commit 7b2e7b2c authored by SASSU Thomas's avatar SASSU Thomas
Browse files

Partie 8 et autres fonctionalités

parent 742d2ac6
......@@ -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
<?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
......@@ -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');
}
......
......@@ -2,7 +2,7 @@
namespace gp\controleurs;
use gp\modeles\Platform;
use gp\modeles\Comment;
use gp\vues\VuePlatform;
use Slim\Slim;
......
......@@ -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');
......
<?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
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