Welcome to mirror list, hosted at ThFree Co, Russian Federation.

github.com/nextcloud/polls.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authordartcafe <github@dartcafe.de>2020-06-14 23:42:17 +0300
committerdartcafe <github@dartcafe.de>2020-06-14 23:42:17 +0300
commit76cba400fc9d8139d1b62fb6e1ce3746368a5574 (patch)
tree9f286bd6343c900cb929bcb444d5cf761675dc37 /lib/Controller
parentc6d2e376387755e10d6c4bf4107a14e65a3a5886 (diff)
Added options to API & changed routes & updated comments
Diffstat (limited to 'lib/Controller')
-rw-r--r--lib/Controller/CommentApiController.php26
-rw-r--r--lib/Controller/CommentController.php4
-rw-r--r--lib/Controller/OptionApiController.php161
-rw-r--r--lib/Controller/OptionController.php169
-rw-r--r--lib/Controller/PollController.php9
5 files changed, 200 insertions, 169 deletions
diff --git a/lib/Controller/CommentApiController.php b/lib/Controller/CommentApiController.php
index 97d2ecf7..fbee1078 100644
--- a/lib/Controller/CommentApiController.php
+++ b/lib/Controller/CommentApiController.php
@@ -27,6 +27,7 @@ use Exception;
use OCP\AppFramework\Db\DoesNotExistException;
use OCP\IRequest;
+use \OCP\IURLGenerator;
use OCP\AppFramework\ApiController;
use OCP\AppFramework\Http;
use OCP\AppFramework\Http\DataResponse;
@@ -40,6 +41,7 @@ use OCA\Polls\Service\CommentService;
class CommentApiController extends ApiController {
private $optionService;
+ private $urlGenerator;
/**
* CommentApiController constructor.
* @param string $appName
@@ -50,6 +52,7 @@ class CommentApiController extends ApiController {
public function __construct(
string $appName,
IRequest $request,
+ IURLGenerator $urlGenerator,
CommentService $commentService
) {
parent::__construct($appName,
@@ -58,6 +61,7 @@ class CommentApiController extends ApiController {
'Authorization, Content-Type, Accept',
1728000);
$this->commentService = $commentService;
+ $this->urlGenerator = $urlGenerator;
}
/**
@@ -70,13 +74,13 @@ class CommentApiController extends ApiController {
* @param integer $pollId
* @return DataResponse
*/
- public function get($pollId, $token = '') {
+ public function list($pollId, $token = '') {
try {
- return new DataResponse($this->commentService->get($pollId, $token), Http::STATUS_OK);
+ return new DataResponse($this->commentService->list($pollId, $token), Http::STATUS_OK);
} catch (NotAuthorizedException $e) {
- return new DataResponse($e, Http::STATUS_FORBIDDEN);
+ return new DataResponse('Unauthorized', Http::STATUS_FORBIDDEN);
} catch (DoesNotExistException $e) {
- return new DataResponse($pollId, Http::STATUS_NOT_FOUND);
+ return new DataResponse('Poll with id ' . $pollId . ' not found', Http::STATUS_NOT_FOUND);
}
}
@@ -93,7 +97,9 @@ class CommentApiController extends ApiController {
try {
return new DataResponse($this->commentService->get(0, $token), Http::STATUS_OK);
} catch (NotAuthorizedException $e) {
- return new DataResponse($e, Http::STATUS_FORBIDDEN);
+ return new DataResponse('Unauthorized', Http::STATUS_FORBIDDEN);
+ } catch (DoesNotExistException $e) {
+ return new DataResponse('Poll with token ' . $token . ' not found', Http::STATUS_NOT_FOUND);
}
}
@@ -110,9 +116,11 @@ class CommentApiController extends ApiController {
*/
public function add($message, $pollId, $token) {
try {
- return new DataResponse($this->commentService->add($message, $pollId, $token), Http::STATUS_OK);
+ return new DataResponse($this->commentService->add($message, $pollId, $token), Http::STATUS_CREATED);
} catch (NotAuthorizedException $e) {
- return new DataResponse($e, Http::STATUS_FORBIDDEN);
+ return new DataResponse('Unauthorized', Http::STATUS_FORBIDDEN);
+ } catch (DoesNotExistException $e) {
+ return new DataResponse('Poll with id ' . $pollId . ' not found', Http::STATUS_NOT_FOUND);
}
}
@@ -131,9 +139,9 @@ class CommentApiController extends ApiController {
$this->commentService->delete($commentId, $token);
return new DataResponse($commentId, Http::STATUS_OK);
} catch (NotAuthorizedException $e) {
- return new DataResponse($commentId, Http::STATUS_FORBIDDEN);
+ return new DataResponse('Unauthorized', Http::STATUS_FORBIDDEN);
} catch (DoesNotExistException $e) {
- return new DataResponse($commentId, Http::STATUS_NOT_FOUND);
+ return new DataResponse('Comment does not exist', Http::STATUS_NOT_FOUND);
}
}
diff --git a/lib/Controller/CommentController.php b/lib/Controller/CommentController.php
index 84cfd01a..70403abd 100644
--- a/lib/Controller/CommentController.php
+++ b/lib/Controller/CommentController.php
@@ -63,8 +63,8 @@ class CommentController extends Controller {
* @param integer $pollId
* @return DataResponse
*/
- public function get($pollId) {
- return new DataResponse($this->commentService->get($pollId), Http::STATUS_OK);
+ public function list($pollId) {
+ return new DataResponse($this->commentService->list($pollId), Http::STATUS_OK);
}
/**
diff --git a/lib/Controller/OptionApiController.php b/lib/Controller/OptionApiController.php
new file mode 100644
index 00000000..d6890c31
--- /dev/null
+++ b/lib/Controller/OptionApiController.php
@@ -0,0 +1,161 @@
+<?php
+/**
+ * @copyright Copyright (c) 2017 Vinzenz Rosenkranz <vinzenz.rosenkranz@gmail.com>
+ *
+ * @author René Gieling <github@dartcafe.de>
+ *
+ * @license GNU AGPL version 3 or any later version
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License as
+ * published by the Free Software Foundation, either version 3 of the
+ * License, or (at your option) any later version.
+ *
+ * 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
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ *
+ */
+
+namespace OCA\Polls\Controller;
+
+use Exception;
+use OCP\AppFramework\Db\DoesNotExistException;
+use Doctrine\DBAL\Exception\UniqueConstraintViolationException;
+
+use OCP\IRequest;
+use OCP\AppFramework\ApiController;
+use OCP\AppFramework\Http;
+use OCP\AppFramework\Http\DataResponse;
+
+use OCA\Polls\Exceptions\NotAuthorizedException;
+
+use OCA\Polls\Service\OptionService;
+
+class OptionApiController extends ApiController {
+
+ private $optionService;
+
+ /**
+ * OptionApiController constructor.
+ * @param string $appName
+ * @param IRequest $request
+ * @param OptionService $optionService
+ */
+
+ public function __construct(
+ string $appName,
+ IRequest $request,
+ OptionService $optionService
+ ) {
+ parent::__construct($appName,
+ $request,
+ 'POST, PUT, GET, DELETE',
+ 'Authorization, Content-Type, Accept',
+ 1728000);
+ $this->optionService = $optionService;
+ }
+
+ /**
+ * Get all options of given poll
+ * @NoAdminRequired
+ * @CORS
+ * @NoCSRFRequired
+ * @param integer $pollId
+ * @return array Array of Option objects
+ */
+ public function list($pollId) {
+ try {
+ return new DataResponse($this->optionService->list($pollId), Http::STATUS_OK);
+ } catch (NotAuthorizedException $e) {
+ return new DataResponse('Unauthorized', Http::STATUS_FORBIDDEN);
+ } catch (DoesNotExistException $e) {
+ return new DataResponse('Poll with id ' . $pollId . ' not found', Http::STATUS_NOT_FOUND);
+ }
+ }
+
+
+ /**
+ * getByToken
+ * Read all options of a poll based on a share token and return list as array
+ * @NoAdminRequired
+ * @PublicPage
+ * @NoCSRFRequired
+ * @param string $token
+ * @return DataResponse
+ */
+ public function getByToken($token) {
+ try {
+ return new DataResponse($this->optionService->get(0, $token), Http::STATUS_OK);
+ } catch (NotAuthorizedException $e) {
+ return new DataResponse('Unauthorized', Http::STATUS_FORBIDDEN);
+ } catch (DoesNotExistException $e) {
+ return new DataResponse('Poll with token ' . $token . ' not found', Http::STATUS_NOT_FOUND);
+ }
+ }
+
+ /**
+ * Add a new Option to poll
+ * @NoAdminRequired
+ * @CORS
+ * @NoCSRFRequired
+ * @param Option $option
+ * @return DataResponse
+ */
+ public function add($pollId, $pollOptionText = '', $timestamp = 0) {
+ $option = [
+ 'pollId' => $pollId,
+ 'pollOptionText' => $pollOptionText,
+ 'timestamp' => $timestamp
+ ];
+
+ try {
+ return new DataResponse($this->optionService->add($option), Http::STATUS_CREATED);
+ } catch (NotAuthorizedException $e) {
+ return new DataResponse('Unauthorized', Http::STATUS_FORBIDDEN);
+ } catch (DoesNotExistException $e) {
+ return new DataResponse('Poll with id ' . $pollId . ' not found', Http::STATUS_NOT_FOUND);
+ } catch (UniqueConstraintViolationException $e) {
+ return new DataResponse('Option exists', Http::STATUS_CONFLICT);
+ }
+ }
+
+ /**
+ * Remove a single option
+ * @NoAdminRequired
+ * @CORS
+ * @NoCSRFRequired
+ * @param Option $option
+ * @return DataResponse
+ */
+ public function delete($optionId) {
+ try {
+ $this->optionService->delete($optionId);
+ return new DataResponse($optionId, Http::STATUS_OK);
+ } catch (NotAuthorizedException $e) {
+ return new DataResponse('Unauthorized', Http::STATUS_FORBIDDEN);
+ } catch (DoesNotExistException $e) {
+ return new DataResponse('Option does not exist', Http::STATUS_NOT_FOUND);
+ }
+ }
+
+ /**
+ * Update poll option
+ * @NoAdminRequired
+ * @CORS
+ * @NoCSRFRequired
+ * @param Option $option
+ * @return DataResponse
+ */
+ public function update($option) {
+ try {
+ return new DataResponse($this->optionService->update($option), Http::STATUS_OK);
+ } catch (NotAuthorizedException $e) {
+ return new DataResponse('Unauthorized', Http::STATUS_FORBIDDEN);
+ }
+ }
+}
diff --git a/lib/Controller/OptionController.php b/lib/Controller/OptionController.php
index 0c20b768..7afa4a08 100644
--- a/lib/Controller/OptionController.php
+++ b/lib/Controller/OptionController.php
@@ -24,98 +24,34 @@
namespace OCA\Polls\Controller;
use Exception;
-use OCP\AppFramework\Db\DoesNotExistException;
use OCP\IRequest;
-use OCP\ILogger;
use OCP\AppFramework\Controller;
use OCP\AppFramework\Http;
use OCP\AppFramework\Http\DataResponse;
-use OCP\IGroupManager;
-use OCP\Security\ISecureRandom;
+use OCA\Polls\Exceptions\NotAuthorizedException;
-use OCA\Polls\Db\Poll;
-use OCA\Polls\Db\PollMapper;
-use OCA\Polls\Db\Option;
-use OCA\Polls\Db\OptionMapper;
-use OCA\Polls\Service\LogService;
-use OCA\Polls\Model\Acl;
+use OCA\Polls\Service\OptionService;
class OptionController extends Controller {
- private $userId;
- private $optionMapper;
- private $options;
- private $option;
- private $groupManager;
- private $pollMapper;
- private $logger;
- private $logService;
- private $acl;
+ private $optionService;
/**
* OptionController constructor.
* @param string $appName
- * @param $UserId
* @param IRequest $request
- * @param ILogger $logger
- * @param OptionMapper $optionMapper
- * @param IGroupManager $groupManager
- * @param PollMapper $pollMapper
- * @param LogService $logService
- * @param Acl $acl
+ * @param OptionService $optionService
*/
public function __construct(
string $appName,
- $UserId,
IRequest $request,
- OptionMapper $optionMapper,
- Option $option,
- IGroupManager $groupManager,
- PollMapper $pollMapper,
- ILogger $logger,
- LogService $logService,
- Acl $acl
+ OptionService $optionService
) {
parent::__construct($appName, $request);
- $this->userId = $UserId;
- $this->optionMapper = $optionMapper;
- $this->option = $option;
- $this->groupManager = $groupManager;
- $this->pollMapper = $pollMapper;
- $this->logger = $logger;
- $this->logService = $logService;
- $this->acl = $acl;
- }
-
- /**
- * Set properties from option array
- * @NoAdminRequired
- * @param integer $pollId
- * @return array Array of Option objects
- */
- private function set($option) {
-
- $this->option->setPollId($option['pollId']);
- $this->option->setPollOptionText(trim(htmlspecialchars($option['pollOptionText'])));
- $this->option->setTimestamp($option['timestamp']);
-
- if ($option['timestamp']) {
- $this->option->setOrder($option['timestamp']);
- } else {
- $this->option->setOrder($option['order']);
- }
-
- if ($option['confirmed']) {
- // do not update confirmation date, if option is already confirmed
- if (!$this->option->getConfirmed()) {
- $this->option->setConfirmed(time());
- }
- } else {
- $this->option->setConfirmed(0);
- }
+ $this->optionService = $optionService;
}
/**
@@ -125,20 +61,8 @@ class OptionController extends Controller {
* @param integer $pollId
* @return array Array of Option objects
*/
- public function get($pollId) {
-
- try {
-
- if (!$this->acl->getFoundByToken()) {
- $this->acl->setPollId($pollId);
- }
-
- $this->options = $this->optionMapper->findByPoll($pollId);
-
- return new DataResponse($this->options, Http::STATUS_OK);
- } catch (DoesNotExistException $e) {
- return new DataResponse($e, Http::STATUS_NOT_FOUND);
- }
+ public function list($pollId) {
+ return new DataResponse($this->optionService->list($pollId), Http::STATUS_OK);
}
@@ -152,16 +76,7 @@ class OptionController extends Controller {
* @return DataResponse
*/
public function getByToken($token) {
-
- try {
- $this->acl->setToken($token);
- // return $this->get($this->acl->getPollId());
- $this->options = $this->optionMapper->findByPoll($this->acl->getPollId());
- return new DataResponse($this->options, Http::STATUS_OK);
-
- } catch (DoesNotExistException $e) {
- return new DataResponse($e, Http::STATUS_NOT_FOUND);
- }
+ return new DataResponse($this->optionService->list(0, $token), Http::STATUS_OK);
}
/**
@@ -172,20 +87,7 @@ class OptionController extends Controller {
* @return DataResponse
*/
public function add($option) {
-
- if (!$this->acl->setPollId($option['pollId'])->getAllowEdit()) {
- return new DataResponse(null, Http::STATUS_UNAUTHORIZED);
- }
-
- try {
- $this->option = new Option();
- $this->set($option);
- $this->optionMapper->insert($this->option);
- $this->logService->setLog($option['pollId'], 'addOption');
- return new DataResponse($this->option, Http::STATUS_OK);
- } catch (Exception $e) {
- return new DataResponse($e, Http::STATUS_NOT_FOUND);
- }
+ return new DataResponse($this->optionService->add($option), Http::STATUS_OK);
}
/**
@@ -196,20 +98,7 @@ class OptionController extends Controller {
* @return DataResponse
*/
public function update($option) {
-
- if (!$this->acl->setPollId($option['pollId'])->getAllowEdit()) {
- return new DataResponse(null, Http::STATUS_UNAUTHORIZED);
- }
-
- try {
- $this->option = $this->optionMapper->find($option['id']);
- $this->set($option);
- $this->optionMapper->update($this->option);
- $this->logService->setLog($option['pollId'], 'updateOption');
- return new DataResponse($this->option, Http::STATUS_OK);
- } catch (Exception $e) {
- return new DataResponse($e, Http::STATUS_NOT_FOUND);
- }
+ return new DataResponse($this->optionService->update($option), Http::STATUS_OK);
}
/**
@@ -220,24 +109,7 @@ class OptionController extends Controller {
* @return DataResponse
*/
public function remove($option) {
- try {
-
- if (!$this->acl->setPollId($option['pollId'])->getAllowEdit()) {
- return new DataResponse(null, Http::STATUS_UNAUTHORIZED);
- }
-
- $this->optionMapper->remove($option['id']);
- $this->logService->setLog($option['pollId'], 'deleteOption');
-
- return new DataResponse(array(
- 'action' => 'deleted',
- 'optionId' => $option['id']
- ), Http::STATUS_OK);
-
- } catch (Exception $e) {
- return new DataResponse($e, Http::STATUS_NOT_FOUND);
- }
-
+ return new DataResponse($this->optionService->delete($option['id']), Http::STATUS_OK);
}
/**
@@ -248,21 +120,6 @@ class OptionController extends Controller {
* @return DataResponse
*/
public function reorder($pollId, $options) {
- $i = 0;
-
- if (!$this->acl->setPollId($pollId)->getAllowEdit()) {
- return new DataResponse(null, Http::STATUS_UNAUTHORIZED);
- }
-
- foreach ($options as $option) {
- $this->option = $this->optionMapper->find($option['id']);
- if ($pollId === intval($this->option->getPollId())) {
- $this->option->setOrder(++$i);
- $this->optionMapper->update($this->option);
- }
- }
-
- return $this->get($pollId);
-
+ return new DataResponse($this->optionService->reorder($pollId, $options), Http::STATUS_OK);
}
}
diff --git a/lib/Controller/PollController.php b/lib/Controller/PollController.php
index 4542713a..9a7aa0d9 100644
--- a/lib/Controller/PollController.php
+++ b/lib/Controller/PollController.php
@@ -48,6 +48,7 @@
use OCA\Polls\Db\VoteMapper;
use OCA\Polls\Service\AnonymizeService;
use OCA\Polls\Service\CommentService;
+ use OCA\Polls\Service\OptionService;
use OCA\Polls\Service\LogService;
use OCA\Polls\Service\MailService;
use OCA\Polls\Model\Acl;
@@ -67,6 +68,7 @@
private $anonymizer;
private $logService;
private $commentService;
+ private $optionService;
private $mailService;
private $acl;
@@ -85,6 +87,7 @@
* @param MailService $mailService
* @param AnonymizeService $anonymizer
* @param CommentService $commentService
+ * @param OptionService $optionService
* @param Acl $acl
*/
@@ -104,6 +107,7 @@
LogService $logService,
MailService $mailService,
CommentService $commentService,
+ OptionService $optionService,
AnonymizeService $anonymizer,
Acl $acl
) {
@@ -121,6 +125,7 @@
$this->logService = $logService;
$this->mailService = $mailService;
$this->commentService = $commentService;
+ $this->optionService = $optionService;
$this->anonymizer = $anonymizer;
$this->acl = $acl;
}
@@ -203,8 +208,8 @@
return new DataResponse([
'acl' => $this->acl,
- 'comments' => $this->commentService->get($pollId),
- 'options' => $options,
+ 'comments' => $this->commentService->list($pollId),
+ 'options' => $this->optionService->list($pollId),
'poll' => $this->poll,
'shares' => $shares,
'votes' => $votes