Skip to content
Snippets Groups Projects
Commit 4229a9e7 authored by SASSU Thomas's avatar SASSU Thomas
Browse files

Partie 5

parent 7d783fa3
No related branches found
No related tags found
No related merge requests found
......@@ -22,14 +22,19 @@ $app->get('/',function () use ($app) {
$app->response->headers->set('Content-Type', 'text/html');
});
// Affiche un jeu au fromat JSON
$app->get('/api/games/:id',function ($id) {
$c = new ControleurGame();
$c->getGame($id);
});
// Affiche tous les jeu paginé au format JSON
$app->get('/api/games/',function () {
$c = new ControleurGame();
$c->getGames();
})->name('getGame');
// Affiche les commentaires d'un jeu
$app->get('/api/games/:id/comments',function ($id) {
$c = new ControleurGame();
$c->getGameComments($id);
});
$app->run();
\ No newline at end of file
......@@ -6,7 +6,6 @@ use gp\modeles\Comment;
use gp\modeles\Game;
use gp\modeles\User;
use Illuminate\Database\Capsule\Manager as DB;
use Faker;
$db = new DB();
$db->addConnection(parse_ini_file('../conf/conf.ini'));
......@@ -16,47 +15,43 @@ $db->bootEloquent();
// use the factory to create a Faker\Generator instance
$faker = Faker\Factory::create();
if(User::first()== null){
//all users
for($i=0; $i<2; $i++) {
$insert = false;
while (!$insert) {
try {
//fake data
$tmp = explode(' ', $faker->name);
$user['fname'] = $tmp[0];
$user['name'] = $tmp[1];
$user['address'] = $faker->address;
$user['email'] = $faker->email;
$user['tel'] = $faker->e164PhoneNumber;
$user['date'] = $faker->date('Y-m-d', '2005-01-01');
//insert
User::create($user['fname'], $user['name'], $user['email'], $user['address'], $user['tel'], $user['date']);
$insert = true;
} catch (PDOException $Exception) {
echo("Duplicate data : recreating new fake");
}
//all users
for($i=0; $i<2; $i++) {
$insert = false;
while (!$insert) {
try {
//fake data
$tmp = explode(' ', $faker->name);
$user['fname'] = $tmp[0];
$user['name'] = $tmp[1];
$user['address'] = $faker->address;
$user['email'] = $faker->email;
$user['tel'] = $faker->e164PhoneNumber;
$user['date'] = $faker->date('Y-m-d', '2005-01-01');
//insert
User::create($user['fname'], $user['name'], $user['email'], $user['address'], $user['tel'], $user['date']);
$insert = true;
} catch (PDOException $Exception) {
echo("Duplicate data : recreating new fake");
}
}
}
if(Comment::first()== null){
//all comments
$users = User::get();
$game = Game::where('id', '=', 12342)->first();
foreach($users as $user){
$insert = false;
while (!$insert) {
try {
//fake data
$title = $faker->text(50);
$content = $faker->text(200);
$date = $faker->date('Y-m-d', '2015-02-01');
//insert comment
Comment::create($game->id, $user->email, $title, $content, $date);
$insert = true;
} catch (PDOException $Exception) {
echo("Duplicate data : recreating new fake");
}
//all comments
$users = User::get();
$game = Game::where('id', '=', 12342)->first();
foreach($users as $user){
$insert = false;
while (!$insert) {
try {
//fake data
$title = $faker->text(50);
$content = $faker->text(200);
$date = $faker->date('Y-m-d', '2015-02-01');
//insert comment
Comment::create($game->id, $user->email, $title, $content, $date);
$insert = true;
} catch (PDOException $Exception) {
echo("Duplicate data : recreating new fake");
}
}
}
\ No newline at end of file
......@@ -29,4 +29,11 @@ class ControleurGame
$v = new VueGame(json_encode($results));
$v->render('getGames');
}
public function getGameComments($id){
$game = Game::where('id', '=', $id)->first();
$comments = $game->comments()->get();
$v = new VueGame(json_encode($comments));
$v->render('getComments');
}
}
\ No newline at end of file
......@@ -20,7 +20,7 @@ class Comment extends \Illuminate\Database\Eloquent\Model
}
function game(){
return $this->BelongsTo('gp\modeles\Game','id_game');
return $this->BelongsTo('gp\modeles\Game','id');
}
function user(){
return $this->BelongsTo('gp\modeles\User', 'email');
......
......@@ -32,6 +32,6 @@ class Game extends \Illuminate\Database\Eloquent\Model
return $this->belongsToMany('gp\modeles\Games', 'similar_games','game1');
}
public function comments(){
return $this->belongsToMany('gp\modeles\comment', 'game2comment','game_id');
return $this->hasMany('gp\modeles\Comment','id_game');
}
}
\ No newline at end of file
......@@ -18,7 +18,12 @@ class VueGame
// break;
// }
// case 'getGames' : {
// $json = $this->afficher_getGame();
// $json = $this->afficher_getGames();
// $cd = '';
// break;
// }
// case 'getComments' : {
// $json = $this->afficher_getComments();
// $cd = '';
// break;
// }
......@@ -29,4 +34,12 @@ class VueGame
// private function afficher_getGame() {
// return $this->tab;
// }
//
// private function afficher_getGames() {
// return $this->tab;
// }
//
// private function afficher_getComments() {
// return $this->tab;
// }
}
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment