From e87093fdf8e646da7a7b545ba6e43b3e13ec71a6 Mon Sep 17 00:00:00 2001 From: "dorian.hodin" Date: Wed, 8 Mar 2023 14:27:48 +0100 Subject: [PATCH] Add almost all route, route for GatewayQuestion.phpnot done yet because GatewayQuestion.php not working and use wrong class --- Source/API/script/Gateway/GatewayForm.php | 30 +-- Source/API/script/Gateway/GatewayKeyword.php | 16 +- .../GatewayListResponseOfCandidate.php | 29 +-- .../Gateway/GatewayPossibleResponse.php | 13 +- Source/API/script/Gateway/GatewayQuestion.php | 26 +- Source/API/script/Gateway/GatewayResponse.php | 31 +-- Source/API/script/index.php | 231 +++++++++++++++++- 7 files changed, 299 insertions(+), 77 deletions(-) diff --git a/Source/API/script/Gateway/GatewayForm.php b/Source/API/script/Gateway/GatewayForm.php index 7a3fb66..7c43ec4 100644 --- a/Source/API/script/Gateway/GatewayForm.php +++ b/Source/API/script/Gateway/GatewayForm.php @@ -26,13 +26,13 @@ class GatewayForm { $query = "INSERT INTO Form(title, description) VALUES(:title, :description)"; $this->connection->executeQuery($query, array( - ':title' => array($title, PDO::PARAM_STR), //parameters[0] = title of the form - ':description' => array($description, PDO::PARAM_STR) //parameters[1] = description of the form + ':title' => array($title, PDO::PARAM_STR), + ':description' => array($description, PDO::PARAM_STR) )); } } - public function getForm(): array //parameters never used cause every function require this parameter + public function getForm(): array { $query = "SELECT * FROM `form`"; $this->connection->executeQuery($query); @@ -62,43 +62,43 @@ class GatewayForm $query = "INSERT INTO Reference(possibleResponse, keyword) VALUES(:possibleResponse, :keyword)"; $this->connection->executeQuery($query, array( ':possibleResponse' => array($this->selectForDeleteAndInsert($idQuestion,$response), PDO::PARAM_INT), - ':keyword' => array($keyword, PDO::PARAM_STR) //parameters[0] = keyword + ':keyword' => array($keyword, PDO::PARAM_STR) )); } - public function deleteKeywordFromQuestion(array $parameters): void + public function deleteKeywordFromQuestion(string $keyword, int $idQuestion, string $response): void { $query = "DELETE FROM Reference WHERE response = :idResponse AND keyword = :idKeword"; $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) )); } - public function updateTitleToForm(array $parameters): void + public function updateTitleToForm(int $id, string $title): void { $query = "UPDATE Form SET title = :title WHERE id = :id"; $this->connection->executeQuery($query, array( - ':title' => array($parameters[0], PDO::PARAM_STR), //parameters[0] = title - ':id' => array($parameters[1], PDO::PARAM_INT) //parameters[1] = idForm + ':title' => array($title, PDO::PARAM_STR), + ':id' => array($id, PDO::PARAM_INT) )); } - public function updateDescriptionToForm(array $parameters): void + public function updateDescriptionToForm(int $id, string $description): void { $query = "UPDATE Form SET title = :title WHERE description = :description"; $this->connection->executeQuery($query, array( - ':description' => array($parameters[0], PDO::PARAM_STR), //parameters[0] = description - ':id' => array($parameters[1], PDO::PARAM_INT) //parameters[1] = idForm + ':description' => array($description, PDO::PARAM_STR), + ':id' => array($id, PDO::PARAM_INT) )); } - public function deleteDescriptionToForm(array $idForm): void + public function deleteDescriptionToForm(int $idForm): void { $query = "UPDATE Form SET title = :title WHERE description = :description"; $this->connection->executeQuery($query, array( ':description' => array('', PDO::PARAM_STR), - ':id' => array($idForm[0], PDO::PARAM_INT) + ':id' => array($idForm, PDO::PARAM_INT) )); } } diff --git a/Source/API/script/Gateway/GatewayKeyword.php b/Source/API/script/Gateway/GatewayKeyword.php index 91c169f..a5cb34e 100644 --- a/Source/API/script/Gateway/GatewayKeyword.php +++ b/Source/API/script/Gateway/GatewayKeyword.php @@ -16,39 +16,39 @@ class GatewayKeyword try{ $this->connection = (new ConnectClass)->connect(); }catch(PDOException $e){ - throw new PDOException(); + throw new PDOException($e->getMessage(), $e->getCode(), $e); } } - public function insertKeyword(array $keyword): void + public function insertKeyword(string $keyword): void { $query = "INSERT INTO Keyword(word) VALUES(:word)"; $this->connection->executeQuery($query, array( - ':word' => array($keyword[0], PDO::PARAM_STR) + ':word' => array($keyword, PDO::PARAM_STR) )); } - public function deleteKeyword(array $keyword): void + public function deleteKeyword(string $keyword): void { $query = "DELETE FROM Keyword WHERE word = :word"; $this->connection->executeQuery($query, array( - ':word' => array($keyword[0], PDO::PARAM_STR) + ':word' => array($keyword, PDO::PARAM_STR) )); } - public function getAllKeyword(array $ignore): array + public function getAllKeyword(): array { $query = "SELECT * FROM Keyword"; $this->connection->executeQuery($query); return $this->connection->getResults(); } - public function getKeywordsContentByReference(array $id): array + public function getKeywordsContentByReference(int $id): array { $query = "SELECT k.* FROM Keyword k, Reference r WHERE k.word = r.keyword AND r.possibleResponse = :id"; $this->connection->executeQuery($query, array( - ':id' => array($id[0], PDO::PARAM_STR) + ':id' => array($id, PDO::PARAM_STR) )); diff --git a/Source/API/script/Gateway/GatewayListResponseOfCandidate.php b/Source/API/script/Gateway/GatewayListResponseOfCandidate.php index f8625f1..6a0a3f0 100644 --- a/Source/API/script/Gateway/GatewayListResponseOfCandidate.php +++ b/Source/API/script/Gateway/GatewayListResponseOfCandidate.php @@ -16,10 +16,11 @@ class GatewayListResponseOfCandidate try{ $this->connection = (new ConnectClass)->connect(); }catch(PDOException $e){ - throw new PDOException(); - } } + throw new PDOException($e->getMessage(), $e->getCode(), $e); + } + } - public function getDetailsListResponsesOfCandidate(array $idListResponse): array + public function getDetailsListResponsesOfCandidate(int $idListResponse): array { $gatewayResponse = new GatewayResponse(); $gatewayKeyword = new GatewayKeyword(); @@ -27,7 +28,7 @@ class GatewayListResponseOfCandidate $query = "SELECT * FROM ListResponsesOfCandidate WHERE id = :id"; $this->connection->executeQuery($query, array( - ':id' => array($idListResponse[0], PDO::PARAM_INT) + ':id' => array($idListResponse, PDO::PARAM_INT) )); $questionList = $this->connection->getResults()[0]; @@ -35,13 +36,13 @@ class GatewayListResponseOfCandidate $responses = $gatewayResponse->getResponsesByIdListCandidate($questionList['id']); foreach ($responses as $row) { - $tabKeywords[] = $gatewayKeyword->getKeywordsContentByReference(array($row['id'])); + $tabKeywords[] = $gatewayKeyword->getKeywordsContentByReference($row['id']); } return array($questionList, $responses, $tabKeywords); } - public function getAllListResponsesOfCandidate(array $ignore): array + public function getAllListResponsesOfCandidate(): array { $query = "SELECT * FROM ListResponsesOfCandidate"; $this->connection->executeQuery($query); @@ -49,21 +50,21 @@ class GatewayListResponseOfCandidate return $this->connection->getResults(); } - public function deleteListResponseOfCandidate(array $id): void + public function deleteListResponseOfCandidate(int $id): void { $gatewayResponse = new GatewayResponse(); - foreach ( $gatewayResponse->getResponsesIdByIdListCandidate($id[0]) as $response){ - $gatewayResponse->deleteResponseById(array($response)); + foreach ( $gatewayResponse->getResponsesIdByIdListCandidate($id) as $response){ + $gatewayResponse->deleteResponseById($response); } $query = "DELETE FROM ListResponsesOfCandidate WHERE id = :id"; $this->connection->executeQuery($query, array( - 'id' => array($id[0], PDO::PARAM_STR) + 'id' => array($id, PDO::PARAM_STR) )); } - public function insertListResponsesOfCandidate(array $parameters): void + public function insertListResponsesOfCandidate(array $answer, string $titleForm,int $idQuestion): void { $gatewayResponse = new GatewayResponse(); $gatewayQuestion = new GatewayQuestion(); @@ -71,14 +72,14 @@ class GatewayListResponseOfCandidate $query = "INSERT INTO ListResponsesOfCandidate(date, titleForm) VALUES(:date, :titleForm)"; $this->connection->executeQuery($query, array( ':date' => array(date('Y-m-d H:i:s'), PDO::PARAM_STR), - ':titleForm' => array($parameters[3], PDO::PARAM_STR) //parameters[3] = titleForm + ':titleForm' => array($titleForm, PDO::PARAM_STR) //parameters[3] = titleForm )); $idListQuestion = $this->connection->lastInsertId(); - for($i = 0; $i < count($parameters[1]); $i++) //parameters[1] = answer + for($i = 0; $i < count($answer); $i++) //parameters[1] = answer { - $idResponse = $gatewayResponse->insertResponse(array($gatewayQuestion->getQuestionContentById(array($parameters[0][$i])), $parameters[1][$i], $parameters[2][$i])); //parameters[1] = answer, parameters[2] = category, parameters[3] = id + $idResponse = $gatewayResponse->insertResponse($gatewayQuestion->getQuestionContentById($idQuestion), "",array()); //$parameters[1][$i], $parameters[2][$i])); //parameters[1] = answer, parameters[2] = category, parameters[3] = id $query = "INSERT INTO Submit (responsesCandidate, response) VALUES(:responsesCandidate, :response)"; $this->connection->executeQuery($query, array( diff --git a/Source/API/script/Gateway/GatewayPossibleResponse.php b/Source/API/script/Gateway/GatewayPossibleResponse.php index e3c901c..9865a5c 100644 --- a/Source/API/script/Gateway/GatewayPossibleResponse.php +++ b/Source/API/script/Gateway/GatewayPossibleResponse.php @@ -16,25 +16,26 @@ class GatewayPossibleResponse try{ $this->connection = (new ConnectClass)->connect(); }catch(PDOException $e){ - throw new PDOException(); - } } + throw new PDOException($e->getMessage(), $e->getCode(), $e); + } + } - public function getPossibleResponseByQuestion(array $idQuestion): array + public function getPossibleResponseByQuestion(int $idQuestion): array { $query = "SELECT pr.* FROM Propose p, PossibleResponse pr WHERE p.question = :questionId AND p.possibleResponse = pr.id"; $this->connection->executeQuery($query, array( - ':questionId' => array($idQuestion[0], PDO::PARAM_INT) + ':questionId' => array($idQuestion, PDO::PARAM_INT) )); return $this->connection->getResults(); } - public function insertPossibleResponse(array $contentPossibleResponse): int + public function insertPossibleResponse(string $contentPossibleResponse): int { $query = "INSERT INTO PossibleResponse(content) VALUES(:content)"; $this->connection->executeQuery($query, array( - ':content' => array($contentPossibleResponse[0], PDO::PARAM_STR) + ':content' => array($contentPossibleResponse, PDO::PARAM_STR) )); return $this->connection->lastInsertId(); diff --git a/Source/API/script/Gateway/GatewayQuestion.php b/Source/API/script/Gateway/GatewayQuestion.php index 67b84b2..c8e5afd 100644 --- a/Source/API/script/Gateway/GatewayQuestion.php +++ b/Source/API/script/Gateway/GatewayQuestion.php @@ -19,21 +19,20 @@ class GatewayQuestion throw new PDOException(); } } - public function insertQuestion(QuestionAPI $question, int $idForm): void + public function insertQuestion(string $contentQuestion, string $classQuestion, int $idForm, array $listPossibleResponse, array $listOfCategories): void { $gatewayPossibleResponse = new GatewayPossibleResponse(); $query = "INSERT INTO Question(content, type, form) VALUES(:content, :type, :form)"; $this->connection->executeQuery($query, array( - ':content' => array($question->getContent(), PDO::PARAM_STR), - ':type' => array(get_class($question), PDO::PARAM_STR), + ':content' => array($contentQuestion, PDO::PARAM_STR), + ':type' => array($classQuestion, PDO::PARAM_STR), ':form' => array($idForm, PDO::PARAM_INT) )); $idQuestion = $this->connection->lastInsertId(); - if(get_class($question) != TextQuestionAPI::class){ - $listPossibleResponse = $question->getPossibleResponses(); + if($classQuestion != "TextQuestionAPI"){ for($i = 0; $i < count($listPossibleResponse); $i++){ @@ -45,14 +44,12 @@ class GatewayQuestion ':possibleResponse' => array($idPossibleResponse, PDO::PARAM_INT) )); - - foreach ($question->getCategories()[$i] as $keyword){ + foreach ($listOfCategories[$i] as $keyword){ $gatewayForm = new GatewayForm(); - $gatewayForm->assignKeywordToQuestion(array($keyword, $listPossibleResponse[$i], $idQuestion)); + $gatewayForm->assignKeywordToQuestion($keyword, $listPossibleResponse[$i], $idQuestion); } } } - } public function deleteQuestion(QuestionAPI $question): void @@ -94,7 +91,7 @@ class GatewayQuestion )); } - public function getAllQuestions(array $idForm): array //print en json un array contenant trois qui pour chaque indice commun de ces 3 array une question, sa liste de reponse possible et sa liste de keyword associer au réponse. les deux autres sont null si c'est une textBox + public function getAllQuestions(array $idForm): array //Print en json un array contenant trois qui pour chaque indice commun de ces 3 array une question, sa liste de reponse possible et sa liste de keyword associer au réponse. les deux autres sont null si c'est une textBox { $query = "SELECT * FROM Question WHERE form = :form"; $this->connection->executeQuery($query, array( @@ -112,13 +109,12 @@ class GatewayQuestion for ($i = 0; $i < count($listQuestions); $i++) { if ($listQuestions[$i]["type"] != "BusinessClass/TextQuestion") { - $possibleResponses = $gatewayPossibleResponse->getPossibleResponseByQuestion(array($listQuestions[$i]["id"])); //$this->connection->getResults(); + $possibleResponses = $gatewayPossibleResponse->getPossibleResponseByQuestion($listQuestions[$i]["id"]); //$this->connection->getResults(); $tmpTabKeyword = []; $tmpTabPossibleResponse = []; foreach ($possibleResponses as $row){ - $tmpTabKeyword[] = $gatewayKeyword->getKeywordsContentByReference(array($row["id"])); - + $tmpTabKeyword[] = $gatewayKeyword->getKeywordsContentByReference($row["id"]); $tmpTabPossibleResponse[] = $row["content"]; } $possibleResponsesContent[] = $tmpTabPossibleResponse; @@ -134,11 +130,11 @@ class GatewayQuestion return array(); } - public function getQuestionContentById(array $id): string + public function getQuestionContentById(int $id): string { $query = "SELECT content FROM Question WHERE id = :id"; $this->connection->executeQuery($query, array( - ':id' => array($id[0], PDO::PARAM_INT) + ':id' => array($id, PDO::PARAM_INT) )); return $this->connection->getResults()[0][0]; diff --git a/Source/API/script/Gateway/GatewayResponse.php b/Source/API/script/Gateway/GatewayResponse.php index 3aad165..6c89d55 100644 --- a/Source/API/script/Gateway/GatewayResponse.php +++ b/Source/API/script/Gateway/GatewayResponse.php @@ -16,60 +16,61 @@ class GatewayResponse try{ $this->connection = (new ConnectClass)->connect(); }catch(PDOException $e){ - throw new PDOException(); - } } + throw new PDOException($e->getMessage(), $e->getCode(), $e); + } + } - public function getResponsesByIdListCandidate(array $listResponsesOfCandidateId): array + public function getResponsesByIdListCandidate(int $listResponsesOfCandidateId): array { - $result = $this->getResponsesIdByIdListCandidate(array($listResponsesOfCandidateId[0])); + $result = $this->getResponsesIdByIdListCandidate($listResponsesOfCandidateId); $tab = []; foreach ($result as $row){ - $tab[] = (new GatewayKeyword())->getKeywordsContentByReference(array($row['id'])); + $tab[] = (new GatewayKeyword())->getKeywordsContentByReference($row['id']); } return array($result, $tab); } - public function getResponsesIdByIdListCandidate(array $listResponsesOfCandidateId): array + public function getResponsesIdByIdListCandidate(int $listResponsesOfCandidateId): array { $query = "SELECT r.id FROM Response r, Submit s WHERE s.responseCandidate = :id AND r.id = s.response"; $this->connection->executeQuery($query, array( - ':id' => array($listResponsesOfCandidateId[0], PDO::PARAM_INT) + ':id' => array($listResponsesOfCandidateId, PDO::PARAM_INT) )); return $this->connection->getResults(); } - public function deleteResponseById(array $responseId): void + public function deleteResponseById(int $responseId): void { $query = "DELETE FROM Categorize WHERE response = :id"; $this->connection->executeQuery($query, array( - ':id' => array($responseId[0], PDO::PARAM_INT) + ':id' => array($responseId, PDO::PARAM_INT) )); $query = "DELETE FROM Submit WHERE response = :id"; $this->connection->executeQuery($query, array( - ':id' => array($responseId[0], PDO::PARAM_INT) + ':id' => array($responseId, PDO::PARAM_INT) )); $query = "DELETE FROM Response WHERE id = :id"; $this->connection->executeQuery($query, array( - ':id' => array($responseId[0], PDO::PARAM_INT) + ':id' => array($responseId, PDO::PARAM_INT) )); } - public function insertResponse(array $parameters): int + public function insertResponse(string $content, string $questionContent, array $category): int { $query = "INSERT INTO Response(content, questionContent) VALUES (:content, :questionContent)"; $this->connection->executeQuery($query, array( - ':content' => array($parameters[0], PDO::PARAM_STR), //parameters[0] = content - ':questionContent' => array($parameters[1], PDO::PARAM_STR) //parameters[1] = questionContent + ':content' => array($content, PDO::PARAM_STR), + ':questionContent' => array($questionContent, PDO::PARAM_STR) )); $idResponse = $this->connection->lastInsertId(); - foreach ($parameters[2] as $keyword){ //parameters[2] = category (array) + foreach ($category as $keyword){ $query = "INSERT INTO Categorize (response, keyword) VALUES(:response, :keyword)"; $this->connection->executeQuery($query, array( ':response' => array($idResponse, PDO::PARAM_STR), diff --git a/Source/API/script/index.php b/Source/API/script/index.php index b511968..515a3cd 100644 --- a/Source/API/script/index.php +++ b/Source/API/script/index.php @@ -4,6 +4,10 @@ use ExceptionHandle\HttpNotFoundError; use ExceptionHandle\PDOError; use ExceptionHandle\TypeErrorParameters; use Gateway\GatewayForm; +use Gateway\GatewayKeyword; +use Gateway\GatewayListResponseOfCandidate; +use Gateway\GatewayPossibleResponse; +use Gateway\GatewayResponse; use Psr\Http\Message\ResponseInterface as Response; use Psr\Http\Message\ServerRequestInterface as Request; use Slim\Factory\AppFactory; @@ -30,7 +34,7 @@ $errorMiddleware = $app->addErrorMiddleware(true, true, true); /** * Add a route for the API */ -$app->get('/', function (Request $request, Response $response) { +$app->get('/', function (Request $request) { throw new HttpNotFoundError($request); }); @@ -43,7 +47,7 @@ $app->get('/getForm', function(Request $request, Response $response){ return $response->withHeader('Content-type', 'application/json')->withStatus(200); }); -$app->put('/insertForm', function(Request $request, Response $response){ +$app->put('/insertForm', function(Request $request){ $parameters = $request->getQueryParams(); if (empty($parameters['title']) || empty($parameters['description'])){ throw new TypeErrorParameters($request); @@ -55,7 +59,7 @@ $app->put('/insertForm', function(Request $request, Response $response){ } }); -$app->put('/deleteForm', function(Request $request, Response $response){ +$app->put('/deleteForm', function(Request $request){ $parameters = $request->getQueryParams(); if (empty($parameters['id'])){ throw new TypeErrorParameters($request); @@ -80,7 +84,7 @@ $app->get('/selectForDeleteAndInsert', function(Request $request, Response $resp return $response->withHeader('Content-type', 'application/json')->withStatus(200); }); -$app->put('/assignKeywordToQuestion', function(Request $request, Response $response){ +$app->put('/assignKeywordToQuestion', function(Request $request){ $parameters = $request->getQueryParams(); if (empty($parameters['keyword']) || empty($parameters['id']) || empty($parameters['response'])){ throw new TypeErrorParameters($request); @@ -92,6 +96,225 @@ $app->put('/assignKeywordToQuestion', function(Request $request, Response $respo } }); +$app->put('/deleteKeywordFromQuestion', function(Request $request){ + $parameters = $request->getQueryParams(); + if (empty($parameters['keyword']) || empty($parameters['id']) || empty($parameters['response'])){ + throw new TypeErrorParameters($request); + } + try{ + (new GatewayForm)->deleteKeywordFromQuestion($parameters['keyword'],$parameters['id'],$parameters['response']); + }catch (PDOException $e){ + throw new PDOError($request,$e->getMessage(),$e); + } +}); + + +$app->put('/updateTitleToForm', function(Request $request){ + $parameters = $request->getQueryParams(); + if (empty($parameters['id']) || empty($parameters['title'])){ + throw new TypeErrorParameters($request); + } + try{ + (new GatewayForm)->updateTitleToForm($parameters['id'],$parameters['title']); + }catch (PDOException $e){ + throw new PDOError($request,$e->getMessage(),$e); + } +}); + +$app->put('/updateDescriptionToForm', function(Request $request){ + $parameters = $request->getQueryParams(); + if (empty($parameters['id']) || empty($parameters['description'])){ + throw new TypeErrorParameters($request); + } + try{ + (new GatewayForm)->updateDescriptionToForm($parameters['id'],$parameters['description']); + }catch (PDOException $e){ + throw new PDOError($request,$e->getMessage(),$e); + } +}); + +$app->put('/deleteDescriptionToForm', function(Request $request){ + $parameters = $request->getQueryParams(); + if (empty($parameters['id'])){ + throw new TypeErrorParameters($request); + } + try{ + (new GatewayForm)->deleteDescriptionToForm($parameters['id']); + }catch (PDOException $e){ + throw new PDOError($request,$e->getMessage(),$e); + } +}); + +$app->put('/insertKeyword', function(Request $request){ + $parameters = $request->getQueryParams(); + if (empty($parameters['keyword'])){ + throw new TypeErrorParameters($request); + } + try{ + (new GatewayKeyword)->insertKeyword($parameters['keyword']); + }catch (PDOException $e){ + throw new PDOError($request,$e->getMessage(),$e); + } +}); + +$app->put('/deleteKeyword', function(Request $request){ + $parameters = $request->getQueryParams(); + if (empty($parameters['keyword'])){ + throw new TypeErrorParameters($request); + } + try{ + (new GatewayKeyword)->deleteKeyword(($parameters['keyword'])); + }catch (PDOException $e){ + throw new PDOError($request,$e->getMessage(),$e); + } +}); + +$app->get('getAllKeyword', function(Request $request, Response $response){ + try{ + $response->getBody()->write(json_encode((new GatewayKeyword)->getAllKeyword(),JSON_UNESCAPED_UNICODE)); + }catch (PDOException $e){ + throw new PDOError($request,$e->getMessage(),$e); + } + return $response->withHeader('Content-type', 'application/json')->withStatus(200); + +}); + +$app->get('/getKeywordsContentByReference', function(Request $request, Response $response){ + $parameters = $request->getQueryParams(); + if (empty($parameters['id'])){ + throw new TypeErrorParameters($request); + } + try{ + $response->getBody()->write(json_encode((new GatewayKeyword)->getKeywordsContentByReference($parameters['id']),JSON_UNESCAPED_UNICODE)); + }catch (PDOException $e){ + throw new PDOError($request,$e->getMessage(),$e); + } + return $response->withHeader('Content-type', 'application/json')->withStatus(200); +}); + +$app->get('/getDetailsListResponseOfCandidate', function(Request $request, Response $response){ + $parameters = $request->getQueryParams(); + if (empty($parameters['id'])){ + throw new TypeErrorParameters($request); + } + try{ + $response->getBody()->write(json_encode((new GatewayListResponseOfCandidate)->getDetailsListResponsesOfCandidate($parameters['id']),JSON_UNESCAPED_UNICODE)); + }catch (PDOException $e){ + throw new PDOError($request,$e->getMessage(),$e); + } + return $response->withHeader('Content-type', 'application/json')->withStatus(200); +}); + +$app->get('/getAllListResponseOfCandidate', function(Request $request, Response $response){ + try{ + $response->getBody()->write(json_encode((new GatewayListResponseOfCandidate)->getAllListResponsesOfCandidate(),JSON_UNESCAPED_UNICODE)); + }catch (PDOException $e){ + throw new PDOError($request,$e->getMessage(),$e); + } + return $response->withHeader('Content-type', 'application/json')->withStatus(200); +}); + +$app->put('/deleteListResponseOfCandidate', function(Request $request){ + $parameters = $request->getQueryParams(); + if (empty($parameters['id'])){ + throw new TypeErrorParameters($request); + } + try{ + (new GatewayListResponseOfCandidate)->deleteListResponseOfCandidate($parameters['id']); + }catch (PDOException $e){ + throw new PDOError($request,$e->getMessage(),$e); + } +}); + +$app->put('/insertListResponseOfCandidate', function(Request $request){ + $parameters = $request->getQueryParams(); + if (empty($parameters['answer']) || empty($parameters['titleForm']) || empty($parameters['idQuestion'])){ + throw new TypeErrorParameters($request); + } + try{ + (new GatewayListResponseOfCandidate)->insertListResponsesOfCandidate($parameters['answer'],$parameters['titleForm'],$parameters['idQuestion']); + }catch (PDOException $e){ + throw new PDOError($request,$e->getMessage(),$e); + } +}); + +$app->get('/getPossibleResponseByQuestion', function(Request $request, Response $response){ + $parameters = $request->getQueryParams(); + if (empty($parameters['id'])){ + throw new TypeErrorParameters($request); + } + try{ + $response->getBody()->write(json_encode((new GatewayPossibleResponse)->getPossibleResponseByQuestion($parameters['id']))); + }catch (PDOException $e){ + throw new PDOError($request,$e->getMessage(),$e); + } + return $response->withHeader('Content-type', 'application/json')->withStatus(200); +}); + +$app->get('/insertPossibleResponse', function(Request $request, Response $response){ + $parameters = $request->getQueryParams(); + if (empty($parameters['content'])){ + throw new TypeErrorParameters($request); + } + try{ + $response->getBody()->write(json_encode((new GatewayPossibleResponse)->insertPossibleResponse($parameters['content']))); + }catch (PDOException $e){ + throw new PDOError($request,$e->getMessage(),$e); + } + return $response->withHeader('Content-type', 'application/json')->withStatus(200); +}); + +$app->get('/getResponsesByIdListCandidate', function(Request $request, Response $response){ + $parameters = $request->getQueryParams(); + if (empty($parameters['id'])){ + throw new TypeErrorParameters($request); + } + try{ + $response->getBody()->write(json_encode((new GatewayResponse)->getResponsesByIdListCandidate($parameters['id']))); + }catch (PDOException $e){ + throw new PDOError($request,$e->getMessage(),$e); + } + return $response->withHeader('Content-type', 'application/json')->withStatus(200); +}); + +$app->get('/getResponsesIdByIdListCandidate', function(Request $request, Response $response){ + $parameters = $request->getQueryParams(); + if (empty($parameters['id'])){ + throw new TypeErrorParameters($request); + } + try{ + $response->getBody()->write(json_encode((new GatewayResponse)->getResponsesIdByIdListCandidate($parameters['id']))); + }catch (PDOException $e){ + throw new PDOError($request,$e->getMessage(),$e); + } + return $response->withHeader('Content-type', 'application/json')->withStatus(200); +}); + +$app->put('/deleteResponseById', function(Request $request){ + $parameters = $request->getQueryParams(); + if (empty($parameters['id'])){ + throw new TypeErrorParameters($request); + } + try{ + (new GatewayResponse)->deleteResponseById($parameters['id']); + }catch (PDOException $e){ + throw new PDOError($request,$e->getMessage(),$e); + } +}); + +$app->get('/insertResponse', function(Request $request, Response $response){ + $parameters = $request->getQueryParams(); + if (empty($parameters['content']) || empty($parameters['questionContent']) || empty($parameters['category'])){ + throw new TypeErrorParameters($request); + } + try{ + $response->getBody()->write(json_encode((new GatewayResponse)->insertResponse($parameters['content'],$parameters['questionContent'],$parameters['category']),JSON_UNESCAPED_UNICODE)); + }catch (PDOException $e){ + throw new PDOError($request,$e->getMessage(),$e); + } + return $response->withHeader('Content-type', 'application/json')->withStatus(200); +}); + // Run app $app->run();