Add Forms Service

Signed-off-by: John Molakvoæ (skjnldsv) <skjnldsv@protonmail.com>
This commit is contained in:
John Molakvoæ (skjnldsv) 2020-04-28 21:14:40 +02:00
parent 648cbbd987
commit b4ad0290e4
No known key found for this signature in database
GPG key ID: 60C25B8C072916CF
3 changed files with 162 additions and 115 deletions

View file

@ -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);

View file

@ -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);
}

View file

@ -0,0 +1,101 @@
<?php
/**
* @copyright Copyright (c) 2020 John Molakvoæ <skjnldsv@protonmail.com>
*
* @author John Molakvoæ <skjnldsv@protonmail.com>
*
* @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 <http://www.gnu.org/licenses/>
*
*/
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;
}
}