GameuserController.class.php 2.46 KB
<?php

include_once __ROOT_DIR . '/libs/php-jwt/src/BeforeValidException.php';
include_once __ROOT_DIR . '/libs/php-jwt/src/ExpiredException.php';
include_once __ROOT_DIR . '/libs/php-jwt/src/SignatureInvalidException.php';
include_once __ROOT_DIR . '/libs/php-jwt/src/JWT.php';
use \Firebase\JWT\JWT;

class GameuserController extends Controller {

    public function __construct($name, $request) {
        parent::__construct($name, $request);
    }

    // ==============
    // Actions
    // ==============

    public function processRequest()
    {
        switch ($this->request->getHttpMethod()) {
            case 'GET':
                switch($this->request->getURIParams()[0])
                {
                    case 'user':
                        $id = $this->request->getURIParams()[1];
                        return $this->getGamesByUser($id);
                        break;
                    
                    case 'game':
                        $id = $this->request->getURIParams()[1];
                        return $this->getUsersbyGame($id);
                        break;
                }
                break;
            
            case 'POST':
                $data = json_decode(file_get_contents("php://input"),TRUE);
                return $this->addUserGame($data);
                break;
            
            case 'DELETE':
                $data = json_decode(file_get_contents("php://input"),TRUE);
                return $this->deleteUserGame($data);
                break;
                
        }
        return Response::errorResponse("unsupported parameters or method in game");
    }

    public function getGamesByUser($id)
    {
        $games = User::getGames($id);
        $response = new Response(200,json_encode($games));
        return $response;
    }

    public function getUsersbyGame($id)
    {
        $users = Game::getUsers($id);
        $response = new Response(200,json_encode($users));
        return $response;
    }

    public function addUserGame($data)
    {
        $idLigne = Game::addUserGame(array(":GAME_ID" => $data['GAME_ID'], ":USER_ID" => $data['USER_ID']));
        $response = new Response(200,json_encode("Joueur ajouté"));
        return $response;
    }

    public function deleteUserGame($data)
    {
        $game = Game::deleteUserGame(array(":GAME_ID" => $data['GAME_ID'], ':USER_ID' => $data['USER_ID']));
        $response = new Response(200, json_encode("Ligne supprimé"));
        return $response;
    }

}