parent
2589cad4e2
commit
7a90d0aba2
@ -0,0 +1,37 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace API\Gateway;
|
||||||
|
|
||||||
|
use PDO;
|
||||||
|
|
||||||
|
class Connection extends PDO
|
||||||
|
{
|
||||||
|
private $stmt;
|
||||||
|
|
||||||
|
public function __construct(string $dsn, string $username, string $password)
|
||||||
|
{
|
||||||
|
parent::__construct($dsn, $username, $password);
|
||||||
|
$this->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/** * @param string $query
|
||||||
|
* @param array $parameters *
|
||||||
|
* @return bool Returns `true` on success, `false` otherwise
|
||||||
|
*/
|
||||||
|
public function executeQuery(string $query, array $parameters = []): bool
|
||||||
|
{
|
||||||
|
$this->stmt = parent::prepare($query);
|
||||||
|
foreach ($parameters as $name => $value) {
|
||||||
|
$this->stmt->bindValue($name, $value[0], $value[1]);
|
||||||
|
}
|
||||||
|
|
||||||
|
return $this->stmt->execute();
|
||||||
|
}
|
||||||
|
|
||||||
|
public function getResults(): array
|
||||||
|
{
|
||||||
|
return $this->stmt->fetchall();
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,61 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace API\Gateway;
|
||||||
|
|
||||||
|
use API\PDO;
|
||||||
|
use FORM_BusinessClass\BoxQuestion;
|
||||||
|
use FORM_BusinessClass\Question;
|
||||||
|
|
||||||
|
class GatewayQuestion
|
||||||
|
{
|
||||||
|
private Connection $connection;
|
||||||
|
|
||||||
|
public function __construct(Connection $connection)
|
||||||
|
{
|
||||||
|
$this->connection = $connection;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function insertQuestion(Question $question, string $idForm)
|
||||||
|
{
|
||||||
|
$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),
|
||||||
|
':form' => array($idForm, PDO::PARAM_STR)
|
||||||
|
));
|
||||||
|
|
||||||
|
$this->connection->executeQuery();
|
||||||
|
|
||||||
|
$idQuestion = $this->connection->lastInsertId();
|
||||||
|
|
||||||
|
if(get_class($question) == BoxQuestion::class){
|
||||||
|
$listPossibleResponse = $question->getPossibleResponses();
|
||||||
|
|
||||||
|
for($i = 0; $i < count($listPossibleResponse); $i++){
|
||||||
|
|
||||||
|
$query = "INSERT INTO PossibleResponse(content) VALUES(:content)";
|
||||||
|
$this->connection->executeQuery($query, array(
|
||||||
|
':content' => array($listPossibleResponse[$i], PDO::PARAM_STR)
|
||||||
|
));
|
||||||
|
|
||||||
|
$this->connection->executeQuery();
|
||||||
|
|
||||||
|
$idPossibleReponse = $this->connection->lastInsertId();
|
||||||
|
|
||||||
|
$query = "INSERT INTO Propose(question, possibleResponse) VALUES(:question, :possibleResponse)";
|
||||||
|
$this->connection->executeQuery($query, array(
|
||||||
|
':question' => array($idQuestion, PDO::PARAM_INT),
|
||||||
|
':possibleResponse' => array($idPossibleReponse, PDO::PARAM_INT)
|
||||||
|
));
|
||||||
|
|
||||||
|
$this->connection->executeQuery();
|
||||||
|
|
||||||
|
|
||||||
|
foreach ($question->getCategories()[$i] as $keyword){
|
||||||
|
$gatewayForm = new GatewayForm($this->connection);
|
||||||
|
$gatewayForm->assignKeywordToQuestion($keyword, $idQuestion);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in new issue