GameController.class.php 3.07 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100
<?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 GameController extends Controller {

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

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

    public function processRequest()
    {
         switch ($this->request->getHttpMethod()) {
            case 'GET':
                $id = $this->request->getURIParams()[0];
                return $this->getGame($id);
                break;

            case 'POST':
                $data = json_decode(file_get_contents("php://input"),TRUE);
                return $this->createGame($data);
                break;
            
            case 'PUT':
                $id = $this->request->getURIParams()[0];
                $data = json_decode(file_get_contents("php://input"),TRUE);
                return $this->updateGame($id, $data);
                break;
            
            case 'DELETE':
                $id = $this->request->getURIParams()[0];
                return $this->deleteGame($id);
                break;
        }
        return Response::errorResponse("unsupported parameters or method in game");
    }

    public function getGame($id)
    {
        $game = Game::getGame($id);
        if($game == Array()){
            $response = Response::errorInParametersResponse("Game not found");
        }else{
            $response = new Response(200,json_encode($game));
        }
        return $response;
    }

    public function createGame($data)
    {
        if(array_key_exists("GAME_DESC", $data)){
            $game = Game::createGame(array( ":GAME_DESC" => $data['GAME_DESC']));
            $response = new Response(200,json_encode($game));
        }else{
            $response = Response::errorInParametersResponse("Parameters missing");
        }
        return $response;
    }

    public function updateGame($id, $data)
    {
        $gameValues = Game::getGame($id);
        $gameValues=($gameValues[0]);
        if($gameValues == []){
            $response = Response::errorResponse("Game not found");
            return $response;
        }else{
            if(array_key_exists('GAME_DESC',$data)){
                $desc = $data['GAME_DESC'];
            }else{
                $desc = $gameValues->GAME_DESC;
            }

            $game = Game::updateGame(array(":GAME_ID" => $id, ":GAME_DESC" => $desc));
            $response = new Response(200,json_encode($game));
            return $response;
        }
    }

    public function deleteGame($id)
    {
        $game = Game::getGame($id);
        if($game == Array()){
            $response = Response::errorInParametersResponse(json_encode("Game not found"));
        }else{
            $res = Game::deleteGame($id);
            $response = Response::okResponse(json_encode($res));
        }
        return $response;
    }

}