From 7aa3f7ace0184c367acb786ca4e891e7f158bace Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?John=20Molakvo=C3=A6=20=28skjnldsv=29?= Date: Fri, 10 Apr 2020 15:50:39 +0200 Subject: [PATCH] New proper types MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: John Molakvoæ (skjnldsv) --- appinfo/routes.php | 14 +++++++------- lib/Controller/ApiController.php | 7 ++++++- lib/Db/Question.php | 7 +++++++ ...300.php => Version010200Date20200323141300.php} | 13 +++++++++++-- 4 files changed, 31 insertions(+), 10 deletions(-) rename lib/Migration/{Version010200Date2020323141300.php => Version010200Date20200323141300.php} (97%) diff --git a/appinfo/routes.php b/appinfo/routes.php index 0373f2a..1000169 100644 --- a/appinfo/routes.php +++ b/appinfo/routes.php @@ -27,9 +27,9 @@ return [ // Before /{hash} to avoid conflict ['name' => 'page#createForm', 'url' => '/new', 'verb' => 'GET'], - ['name' => 'page#editForm', 'url' => '/{hash}/edit/', 'verb' => 'GET'], - ['name' => 'page#cloneForm', 'url' => '/{hash}/clone/', 'verb' => 'GET'], - ['name' => 'page#getResult', 'url' => '/{hash}/results/', 'verb' => 'GET'], + ['name' => 'page#editForm', 'url' => '/{hash}/edit', 'verb' => 'GET'], + ['name' => 'page#cloneForm', 'url' => '/{hash}/clone', 'verb' => 'GET'], + ['name' => 'page#getResult', 'url' => '/{hash}/results', 'verb' => 'GET'], ['name' => 'page#goto_form', 'url' => '/{hash}', 'verb' => 'GET'], ['name' => 'page#insert_submission', 'url' => '/insert/submission', 'verb' => 'POST'], @@ -39,11 +39,11 @@ return [ ['name' => 'api#getForm', 'url' => 'api/v1/form/{id}', 'verb' => 'GET'], ['name' => 'api#updateForm', 'url' => 'api/v1/form/update/', 'verb' => 'POST'], ['name' => 'api#deleteForm', 'url' => 'api/v1/form/{id}', 'verb' => 'DELETE'], - ['name' => 'api#updateQuestion', 'url' => 'api/v1/question/update/', 'verb' => 'POST'], - ['name' => 'api#reorderQuestions', 'url' => 'api/v1/question/reorder/', 'verb' => 'POST'], - ['name' => 'api#newQuestion', 'url' => 'api/v1/question/', 'verb' => 'POST'], + ['name' => 'api#updateQuestion', 'url' => 'api/v1/question/update', 'verb' => 'POST'], + ['name' => 'api#reorderQuestions', 'url' => 'api/v1/question/reorder', 'verb' => 'POST'], + ['name' => 'api#newQuestion', 'url' => 'api/v1/question', 'verb' => 'POST'], ['name' => 'api#deleteQuestion', 'url' => 'api/v1/question/{id}', 'verb' => 'DELETE'], - ['name' => 'api#newOption', 'url' => 'api/v1/option/', 'verb' => 'POST'], + ['name' => 'api#newOption', 'url' => 'api/v1/option', 'verb' => 'POST'], ['name' => 'api#deleteOption', 'url' => 'api/v1/option/{id}', 'verb' => 'DELETE'], ['name' => 'api#getSubmissions', 'url' => 'api/v1/submissions/{hash}', 'verb' => 'GET'], diff --git a/lib/Controller/ApiController.php b/lib/Controller/ApiController.php index 9dd61c9..4bf44b1 100644 --- a/lib/Controller/ApiController.php +++ b/lib/Controller/ApiController.php @@ -256,11 +256,16 @@ class ApiController extends Controller { 'text' => $text, ]); + if (array_search($type, Question::TYPES) === false) { + $this->logger->debug('Invalid type'); + return new Http\JSONResponse(['message' => 'Invalid type'], Http::STATUS_BAD_REQUEST); + } + try { $form = $this->formMapper->findById($formId); } catch (IMapperException $e) { $this->logger->debug('Could not find form'); - return new Http\JSONResponse([], Http::STATUS_BAD_REQUEST); + return new Http\JSONResponse(['message' => 'Could not find form'], Http::STATUS_BAD_REQUEST); } if ($form->getOwnerId() !== $this->userId) { diff --git a/lib/Db/Question.php b/lib/Db/Question.php index 426db1f..25f5199 100644 --- a/lib/Db/Question.php +++ b/lib/Db/Question.php @@ -43,6 +43,13 @@ class Question extends Entity { protected $mandatory; protected $text; + const TYPES = [ + 'short', + 'long', + 'multiple', + 'multiple_unique' + ]; + public function __construct() { $this->addType('formId', 'integer'); $this->addType('order', 'integer'); diff --git a/lib/Migration/Version010200Date2020323141300.php b/lib/Migration/Version010200Date20200323141300.php similarity index 97% rename from lib/Migration/Version010200Date2020323141300.php rename to lib/Migration/Version010200Date20200323141300.php index cc738b5..461b68c 100644 --- a/lib/Migration/Version010200Date2020323141300.php +++ b/lib/Migration/Version010200Date20200323141300.php @@ -39,7 +39,7 @@ use \DateTime; * Installation class for the forms app. * Initial db creation */ -class Version010200Date2020323141300 extends SimpleMigrationStep { +class Version010200Date20200323141300 extends SimpleMigrationStep { /** @var IDBConnection */ protected $connection; @@ -47,6 +47,15 @@ class Version010200Date2020323141300 extends SimpleMigrationStep { /** @var IConfig */ protected $config; + /** Map of questionTypes to change */ + private $questionTypeMap = [ + 'radiogroup' => 'multiple_unique', + 'checkbox' => 'multiple', + 'text' => 'short', + 'comment' => 'long', + 'dropdown' => 'multiple_unique' + ]; + /** * @param IDBConnection $connection * @param IConfig $config @@ -257,7 +266,7 @@ class Version010200Date2020323141300 extends SimpleMigrationStep { ->values([ 'form_id' => $qb_restore->createNamedParameter($id_mapping['events'][$question['form_id']]['newId'], IQueryBuilder::PARAM_INT), 'order' => $qb_restore->createNamedParameter($id_mapping['events'][$question['form_id']]['nextQuestionOrder']++, IQueryBuilder::PARAM_INT), - 'type' => $qb_restore->createNamedParameter($question['form_question_type'], IQueryBuilder::PARAM_STR), + 'type' => $qb_restore->createNamedParameter($this->questionTypeMap[$question['form_question_type']], IQueryBuilder::PARAM_STR), 'text' => $qb_restore->createNamedParameter($question['form_question_text'], IQueryBuilder::PARAM_STR) ]); $qb_restore->execute();