From b4ad0290e47aa5c43e795e54a352dec65f0132b1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?John=20Molakvo=C3=A6=20=28skjnldsv=29?= Date: Tue, 28 Apr 2020 21:14:40 +0200 Subject: [PATCH] Add Forms Service MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: John Molakvoæ (skjnldsv) --- lib/Controller/ApiController.php | 120 +++++++++++++----------------- lib/Controller/PageController.php | 56 +++----------- lib/Service/FormsService.php | 101 +++++++++++++++++++++++++ 3 files changed, 162 insertions(+), 115 deletions(-) create mode 100644 lib/Service/FormsService.php diff --git a/lib/Controller/ApiController.php b/lib/Controller/ApiController.php index 98d6644..9394c04 100644 --- a/lib/Controller/ApiController.php +++ b/lib/Controller/ApiController.php @@ -29,95 +29,78 @@ namespace OCA\Forms\Controller; -use OCA\Forms\AppInfo\Application; -use OCP\AppFramework\Controller; -use OCP\AppFramework\Http; -use OCP\AppFramework\Db\DoesNotExistException; -use OCP\AppFramework\Db\IMapperException; +use OCA\Forms\Db\Answer; +use OCA\Forms\Db\AnswerMapper; +use OCA\Forms\Db\Form; +use OCA\Forms\Db\FormMapper; +use OCA\Forms\Db\Option; +use OCA\Forms\Db\OptionMapper; +use OCA\Forms\Db\Question; +use OCA\Forms\Db\QuestionMapper; +use OCA\Forms\Db\Submission; +use OCA\Forms\Db\SubmissionMapper; +use OCA\Forms\Service\FormsService; +use OCP\AppFramework\Controller; +use OCP\AppFramework\Db\IMapperException; +use OCP\AppFramework\Http; use OCP\ILogger; use OCP\IRequest; -use OCP\IUser; use OCP\IUserSession; use OCP\Security\ISecureRandom; -use OCA\Forms\Db\Form; -use OCA\Forms\Db\FormMapper; -use OCA\Forms\Db\Question; -use OCA\Forms\Db\QuestionMapper; -use OCA\Forms\Db\Option; -use OCA\Forms\Db\OptionMapper; -use OCA\Forms\Db\Submission; -use OCA\Forms\Db\SubmissionMapper; -use OCA\Forms\Db\Answer; -use OCA\Forms\Db\AnswerMapper; - class ApiController extends Controller { - private $formMapper; + protected $appName; + + /** @var SubmissionMapper */ private $submissionMapper; - private $answerMapper; + + /** @var FormMapper */ + private $formMapper; + + /** @var QuestionMapper */ private $questionMapper; + + /** @var OptionMapper */ private $optionMapper; + /** @var AnswerMapper */ + private $answerMapper; + /** @var ILogger */ private $logger; /** @var IUserSession */ private $userSession; + + /** @var FormsService */ + private $formsService; - public function __construct( - IRequest $request, - $userId, // TODO remove & replace with userSession below. - IUserSession $userSession, - FormMapper $formMapper, - SubmissionMapper $submissionMapper, - AnswerMapper $answerMapper, - QuestionMapper $questionMapper, - OptionMapper $optionMapper, - ILogger $logger - ) { - parent::__construct(Application::APP_ID, $request); + public function __construct(string $appName, + IRequest $request, + $userId, // TODO remove & replace with userSession below. + IUserSession $userSession, + FormMapper $formMapper, + SubmissionMapper $submissionMapper, + AnswerMapper $answerMapper, + QuestionMapper $questionMapper, + OptionMapper $optionMapper, + ILogger $logger, + FormsService $formsService) { + parent::__construct($appName, $request); + $this->appName = $appName; $this->userId = $userId; $this->userSession = $userSession; $this->formMapper = $formMapper; + $this->questionMapper = $questionMapper; + $this->optionMapper = $optionMapper; $this->submissionMapper = $submissionMapper; $this->answerMapper = $answerMapper; $this->questionMapper = $questionMapper; $this->optionMapper = $optionMapper; $this->logger = $logger; - } - - private function getOptions(int $questionId): array { - $optionList = []; - try{ - $optionEntities = $this->optionMapper->findByQuestion($questionId); - foreach ($optionEntities as $optionEntity) { - $optionList[] = $optionEntity->read(); - } - - } catch (DoesNotExistException $e) { - //handle silently - } finally { - return $optionList; - } - } - - private function getQuestions(int $formId): array { - $questionList = []; - try{ - $questionEntities = $this->questionMapper->findByForm($formId); - foreach ($questionEntities as $questionEntity) { - $question = $questionEntity->read(); - $question['options'] = $this->getOptions($question['id']); - $questionList[] = $question; - } - - } catch (DoesNotExistException $e) { - //handle silently - }finally{ - return $questionList; - } + $this->formsService = $formsService; } /** @@ -143,20 +126,19 @@ class ApiController extends Controller { /** * @NoAdminRequired + * + * * Read all information to edit a Form (form, questions, options, except submissions/answers). */ public function getForm(int $id): Http\JSONResponse { try { - $form = $this->formMapper->findById($id); + $results = $this->formsService->getForm($id); } catch (IMapperException $e) { $this->logger->debug('Could not find form'); return new Http\JSONResponse([], Http::STATUS_BAD_REQUEST); } - $result = $form->read(); - $result['questions'] = $this->getQuestions($id); - - return new Http\JSONResponse($result); + return new Http\JSONResponse($results); } /** @@ -623,7 +605,7 @@ class ApiController extends Controller { try { $form = $this->formMapper->findById($formId); - $questions = $this->getQuestions($formId); + $questions = $this->formsService->getQuestions($formId); } catch (IMapperException $e) { $this->logger->debug('Could not find form'); return new Http\JSONResponse(['message' => 'Could not find form'], Http::STATUS_BAD_REQUEST); diff --git a/lib/Controller/PageController.php b/lib/Controller/PageController.php index 1cfa186..2c8ce9c 100644 --- a/lib/Controller/PageController.php +++ b/lib/Controller/PageController.php @@ -29,22 +29,20 @@ namespace OCA\Forms\Controller; -use Exception; use OCA\Forms\Db\Form; use OCA\Forms\Db\FormMapper; -use OCA\Forms\Db\Question; -use OCA\Forms\Db\QuestionMapper; -use OCA\Forms\Db\Option; use OCA\Forms\Db\OptionMapper; +use OCA\Forms\Db\QuestionMapper; +use OCA\Forms\Service\FormsService; use OCP\AppFramework\Controller; use OCP\AppFramework\Db\DoesNotExistException; use OCP\AppFramework\Http\TemplateResponse; use OCP\IGroupManager; +use OCP\IInitialStateService; use OCP\IRequest; use OCP\IURLGenerator; use OCP\IUserSession; -use OCP\IInitialStateService; use OCP\Util; class PageController extends Controller { @@ -65,6 +63,9 @@ class PageController extends Controller { /** @var IInitialStateService */ private $initialStateService; + + /** @var FormsService */ + private $formService; public function __construct(string $appName, IRequest $request, @@ -74,7 +75,8 @@ class PageController extends Controller { QuestionMapper $questionMapper, OptionMapper $optionMapper, IUserSession $userSession, - IInitialStateService $initialStateService) { + IInitialStateService $initialStateService, + FormsService $formsService) { parent::__construct($appName, $request); $this->groupManager = $groupManager; @@ -85,6 +87,7 @@ class PageController extends Controller { $this->optionMapper = $optionMapper; $this->userSession = $userSession; $this->initialStateService = $initialStateService; + $this->formsService = $formsService; } /** @@ -149,45 +152,6 @@ class PageController extends Controller { return new TemplateResponse($this->appName, 'main'); } - private function getOptions(int $questionId): array { - $optionList = []; - try{ - $optionEntities = $this->optionMapper->findByQuestion($questionId); - foreach ($optionEntities as $optionEntity) { - $optionList[] = $optionEntity->read(); - } - - } catch (DoesNotExistException $e) { - //handle silently - } finally { - return $optionList; - } - } - - private function getQuestions(int $formId): array { - $questionList = []; - try{ - $questionEntities = $this->questionMapper->findByForm($formId); - foreach ($questionEntities as $questionEntity) { - $question = $questionEntity->read(); - $question['options'] = $this->getOptions($question['id']); - $questionList[] = $question; - } - - } catch (DoesNotExistException $e) { - //handle silently - }finally{ - return $questionList; - } - } - - private function getForm(int $id): array { - $form = $this->formMapper->findById($id); - $result = $form->read(); - $result['questions'] = $this->getQuestions($id); - return $result; - } - /** * @NoAdminRequired * @NoCSRFRequired @@ -220,7 +184,7 @@ class PageController extends Controller { $renderAs = $this->userSession->isLoggedIn() ? 'user' : 'public'; Util::addScript($this->appName, 'submit'); - $this->initialStateService->provideInitialState($this->appName, 'form', $this->getForm($form->getId())); + $this->initialStateService->provideInitialState($this->appName, 'form', $this->formsService->getForm($form->getId())); return new TemplateResponse($this->appName, 'main', [], $renderAs); } diff --git a/lib/Service/FormsService.php b/lib/Service/FormsService.php new file mode 100644 index 0000000..7d78bc8 --- /dev/null +++ b/lib/Service/FormsService.php @@ -0,0 +1,101 @@ + + * + * @author John Molakvoæ + * + * @license GNU AGPL version 3 or any later version + * + * This code is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License, version 3, + * as published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License, version 3, + * along with this program. If not, see + * + */ + +namespace OCA\Forms\Service; + +use OCA\Forms\Db\FormMapper; +use OCA\Forms\Db\OptionMapper; +use OCA\Forms\Db\QuestionMapper; +use OCP\AppFramework\Db\DoesNotExistException; +use OCP\AppFramework\Db\IMapperException; + +/** + * Trait for getting forms information in a service + */ +class FormsService { + + /** @var FormMapper */ + private $formMapper; + + /** @var QuestionMapper */ + private $questionMapper; + + /** @var OptionMapper */ + private $optionMapper; + + public function __construct(FormMapper $formMapper, + QuestionMapper $questionMapper, + OptionMapper $optionMapper) { + $this->formMapper = $formMapper; + $this->questionMapper = $questionMapper; + $this->optionMapper = $optionMapper; + } + + + public function getOptions(int $questionId): array { + $optionList = []; + try{ + $optionEntities = $this->optionMapper->findByQuestion($questionId); + foreach ($optionEntities as $optionEntity) { + $optionList[] = $optionEntity->read(); + } + + } catch (DoesNotExistException $e) { + //handle silently + } finally { + return $optionList; + } + } + + public function getQuestions(int $formId): array { + $questionList = []; + try{ + $questionEntities = $this->questionMapper->findByForm($formId); + foreach ($questionEntities as $questionEntity) { + $question = $questionEntity->read(); + $question['options'] = $this->getOptions($question['id']); + $questionList[] = $question; + } + + } catch (DoesNotExistException $e) { + //handle silently + }finally{ + return $questionList; + } + } + + /** + * Get a form data + * + * @param integer $id + * @return array + * @throws IMapperException + */ + public function getForm(int $id): array { + $form = $this->formMapper->findById($id); + $result = $form->read(); + $result['questions'] = $this->getQuestions($id); + + return $result; + } + +}