Starting to add all route, testing
continuous-integration/drone/push Build is passing Details

master
dorian.hodin 2 years ago
parent c22b67c16c
commit f4f538458f

@ -10,7 +10,7 @@ class HttpNotFoundError extends HttpSpecializedException {
protected $code = 404; protected $code = 404;
protected string $title = "Method Not Found"; protected string $title = "Method Not Found";
protected $message = "Method not found on the API, verify the method's name."; protected $message = "You have to add a method name in the URL. Example !: 'http://url/method'";
public function __construct(ServerRequestInterface $request,?Throwable $previous = null) public function __construct(ServerRequestInterface $request,?Throwable $previous = null)
{ {

@ -6,11 +6,11 @@ use Psr\Http\Message\ServerRequestInterface;
use Slim\Exception\HttpSpecializedException; use Slim\Exception\HttpSpecializedException;
use Throwable; use Throwable;
class TypeErrorMethod extends HttpSpecializedException { class TypeErrorParameters extends HttpSpecializedException {
protected $code = 400; protected $code = 400;
protected string $title = "Method query params is not specified"; protected string $title = "Method query params is not specified";
protected $message = "Bad Parameters, The API need a 'method' query params in URL. Exemple :'http://url?method=getSomething'"; protected $message = "Bad Parameters, The API need parameters for this method. Exemple :'http://url/method?param1=1'";
public function __construct(ServerRequestInterface $request,?Throwable $previous = null) public function __construct(ServerRequestInterface $request,?Throwable $previous = null)
{ {

@ -20,19 +20,19 @@ class GatewayForm
} }
} }
public function insertForm(array $parameters): void public function insertForm(string $title, string $description): void
{ {
if(empty($this->getForm(array()))) if(empty($this->getForm()))
{ {
$query = "INSERT INTO Form(title, description) VALUES(:title, :description)"; $query = "INSERT INTO Form(title, description) VALUES(:title, :description)";
$this->connection->executeQuery($query, array( $this->connection->executeQuery($query, array(
':title' => array($parameters[0], PDO::PARAM_STR), //parameters[0] = title of the form ':title' => array($title, PDO::PARAM_STR), //parameters[0] = title of the form
':description' => array($parameters[1], PDO::PARAM_STR) //parameters[1] = description of the form ':description' => array($description, PDO::PARAM_STR) //parameters[1] = description of the form
)); ));
} }
} }
public function getForm(array $ignore): array //parameters never used cause every function require this parameter public function getForm(): array //parameters never used cause every function require this parameter
{ {
$query = "SELECT * FROM `form`"; $query = "SELECT * FROM `form`";
$this->connection->executeQuery($query); $this->connection->executeQuery($query);
@ -57,12 +57,12 @@ class GatewayForm
return $this->connection->getResults()[0][0]; return $this->connection->getResults()[0][0];
} }
public function assignKeywordToQuestion(array $parameters): void public function assignKeywordToQuestion(string $keyword, int $idQuestion, string $response): void
{ {
$query = "INSERT INTO Reference(possibleResponse, keyword) VALUES(:possibleResponse, :keyword)"; $query = "INSERT INTO Reference(possibleResponse, keyword) VALUES(:possibleResponse, :keyword)";
$this->connection->executeQuery($query, array( $this->connection->executeQuery($query, array(
':possibleResponse' => array($this->selectForDeleteAndInsert($parameters[2],$parameters[1]), PDO::PARAM_INT), ':possibleResponse' => array($this->selectForDeleteAndInsert($idQuestion,$response), PDO::PARAM_INT),
':keyword' => array($parameters[0], PDO::PARAM_STR) //parameters[0] = keyword ':keyword' => array($keyword, PDO::PARAM_STR) //parameters[0] = keyword
)); ));
} }

@ -2,7 +2,8 @@
use ExceptionHandle\HttpNotFoundError; use ExceptionHandle\HttpNotFoundError;
use ExceptionHandle\PDOError; use ExceptionHandle\PDOError;
use ExceptionHandle\TypeErrorMethod; use ExceptionHandle\TypeErrorParameters;
use Gateway\GatewayForm;
use Psr\Http\Message\ResponseInterface as Response; use Psr\Http\Message\ResponseInterface as Response;
use Psr\Http\Message\ServerRequestInterface as Request; use Psr\Http\Message\ServerRequestInterface as Request;
use Slim\Factory\AppFactory; use Slim\Factory\AppFactory;
@ -30,32 +31,67 @@ $errorMiddleware = $app->addErrorMiddleware(true, true, true);
* Add a route for the API * Add a route for the API
*/ */
$app->get('/', function (Request $request, Response $response) { $app->get('/', function (Request $request, Response $response) {
throw new HttpNotFoundError($request);
});
$app->get('/getForm', function(Request $request, Response $response){
try{
$response->getBody()->write(json_encode((new GatewayForm)->getForm(), JSON_UNESCAPED_UNICODE));
}catch (PDOException $e){
throw new PDOError($request,$e->getMessage(),$e);
}
return $response->withHeader('Content-type', 'application/json')->withStatus(200);
});
$app->put('/insertForm', function(Request $request, Response $response){
$parameters = $request->getQueryParams();
if (empty($parameters['title']) || empty($parameters['description'])){
throw new TypeErrorParameters($request);
}
try{
(new GatewayForm)->insertForm($parameters['title'],$parameters['description']);
}catch (PDOException $e){
throw new PDOError($request,$e->getMessage(),$e);
}
});
$app->put('/deleteForm', function(Request $request, Response $response){
$parameters = $request->getQueryParams(); $parameters = $request->getQueryParams();
if (empty($parameters['method'])){ if (empty($parameters['id'])){
throw new TypeErrorMethod($request); throw new TypeErrorParameters($request);
}else{ }
$method = $parameters['method']; try{
} (new GatewayForm)->deleteForm($parameters['id']);
unset($parameters['method']); }catch (PDOException $e){
$listGateway = array("\\Gateway\\GatewayForm", "\\Gateway\\GatewayKeyword", "\\Gateway\\GatewayQuestion"); throw new PDOError($request,$e->getMessage(),$e);
$ok = false; }
foreach ($listGateway as $gateway){ });
try {
if (method_exists($gateway, $method)) { $app->get('/selectForDeleteAndInsert', function(Request $request, Response $response){
$ok = true; $parameters = $request->getQueryParams();
$rep = ((new $gateway)->$method($parameters)); if (empty($parameters['id']) || empty($parameters['response'])){
$response->getBody()->write(json_encode($rep, JSON_UNESCAPED_UNICODE)); throw new TypeErrorParameters($request);
} }
}catch (PDOException $e){ try{
throw new PDOError($request,$e->getMessage(),$e); $response->getBody()->write(json_encode((new GatewayForm)->selectForDeleteAndInsert($parameters['id'],$parameters['response']), JSON_UNESCAPED_UNICODE));
} }catch (PDOException $e){
} throw new PDOError($request,$e->getMessage(),$e);
if (!$ok){
throw new HttpNotFoundError($request);
} }
return $response->withHeader('Content-type', 'application/json')->withStatus(200); return $response->withHeader('Content-type', 'application/json')->withStatus(200);
}); });
$app->put('/assignKeywordToQuestion', function(Request $request, Response $response){
$parameters = $request->getQueryParams();
if (empty($parameters['keyword']) || empty($parameters['id']) || empty($parameters['response'])){
throw new TypeErrorParameters($request);
}
try{
(new GatewayForm)->assignKeywordToQuestion($parameters['keyword'],$parameters['id'],$parameters['response']);
}catch (PDOException $e){
throw new PDOError($request,$e->getMessage(),$e);
}
});
// Run app // Run app
$app->run(); $app->run();

Loading…
Cancel
Save