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 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)
{

@ -6,11 +6,11 @@ use Psr\Http\Message\ServerRequestInterface;
use Slim\Exception\HttpSpecializedException;
use Throwable;
class TypeErrorMethod extends HttpSpecializedException {
class TypeErrorParameters extends HttpSpecializedException {
protected $code = 400;
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)
{

@ -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)";
$this->connection->executeQuery($query, array(
':title' => array($parameters[0], PDO::PARAM_STR), //parameters[0] = title of the form
':description' => array($parameters[1], PDO::PARAM_STR) //parameters[1] = description of the form
':title' => array($title, PDO::PARAM_STR), //parameters[0] = title 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`";
$this->connection->executeQuery($query);
@ -57,12 +57,12 @@ class GatewayForm
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)";
$this->connection->executeQuery($query, array(
':possibleResponse' => array($this->selectForDeleteAndInsert($parameters[2],$parameters[1]), PDO::PARAM_INT),
':keyword' => array($parameters[0], PDO::PARAM_STR) //parameters[0] = keyword
':possibleResponse' => array($this->selectForDeleteAndInsert($idQuestion,$response), PDO::PARAM_INT),
':keyword' => array($keyword, PDO::PARAM_STR) //parameters[0] = keyword
));
}

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

Loading…
Cancel
Save