diff options
author | dartcafe <github@dartcafe.de> | 2020-06-20 12:38:34 +0300 |
---|---|---|
committer | dartcafe <github@dartcafe.de> | 2020-06-20 12:38:34 +0300 |
commit | d3b8c7c7028b6c112c230df6c010263dfb1a1130 (patch) | |
tree | 29e63adc08d02a4fc1b6eb32c01c72046444fc5b /lib/Controller | |
parent | b1c1a707d1b5523868d992b96a4ccddd1086ef2b (diff) |
Completed API with poll
Diffstat (limited to 'lib/Controller')
-rw-r--r-- | lib/Controller/PollApiController.php | 217 | ||||
-rw-r--r-- | lib/Controller/PollController.php | 331 | ||||
-rw-r--r-- | lib/Controller/SubscriptionController.php | 12 |
3 files changed, 321 insertions, 239 deletions
diff --git a/lib/Controller/PollApiController.php b/lib/Controller/PollApiController.php new file mode 100644 index 00000000..777df2ba --- /dev/null +++ b/lib/Controller/PollApiController.php @@ -0,0 +1,217 @@ +<?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 OCA\Polls\Exceptions\EmptyTitleException; + use OCA\Polls\Exceptions\InvalidAccessException; + use OCA\Polls\Exceptions\InvalidShowResultsException; + use OCA\Polls\Exceptions\InvalidPollTypeException; + use OCA\Polls\Exceptions\NotAuthorizedException; + + use OCP\IRequest; + use OCP\ILogger; + use OCP\AppFramework\ApiController; + use OCP\AppFramework\Http; + use OCP\AppFramework\Http\DataResponse; + + use OCA\Polls\Service\PollService; + + class PollApiController extends ApiController { + + private $logger; + private $pollService; + + /** + * PollController constructor. + * @param string $appName + * @param $userId + * @param IRequest $request + * @param ILogger $logger + * @param PollService $pollService + */ + + public function __construct( + string $appName, + IRequest $request, + ILogger $logger, + PollService $pollService + ) { + parent::__construct($appName, $request); + $this->logger = $logger; + $this->pollService = $pollService; + } + + + /** + * list + * @NoAdminRequired + * @NoCSRFRequired + * @CORS + * @return DataResponse + */ + + public function list() { + try { + return new DataResponse($this->pollService->list(), Http::STATUS_OK); + } catch (DoesNotExistException $e) { + return new DataResponse([], Http::STATUS_NOT_FOUND); + } catch (NotAuthorizedException $e) { + return new DataResponse($e->getMessage(), $e->getStatus()); + } + } + + + /** + * get + * @NoAdminRequired + * @NoCSRFRequired + * @param integer $pollId + * @return array + */ + public function get($pollId) { + try { + return new DataResponse($this->pollService->get($pollId), Http::STATUS_OK); + } catch (DoesNotExistException $e) { + return new DataResponse('Not found', Http::STATUS_NOT_FOUND); + } catch (NotAuthorizedException $e) { + return new DataResponse($e->getMessage(), $e->getStatus()); + } + } + + /** + * delete + * @NoAdminRequired + * @NoCSRFRequired + * @param Array $poll + * @return DataResponse + */ + + public function delete($pollId) { + try { + return new DataResponse($this->pollService->delete($pollId), Http::STATUS_OK); + } catch (DoesNotExistException $e) { + return new DataResponse('Poll not found', Http::STATUS_NOT_FOUND); + } catch (NotAuthorizedException $e) { + return new DataResponse($e->getMessage(), $e->getStatus()); + } + } + + /** + * deletePermanently + * @NoAdminRequired + * @NoCSRFRequired + * @param Array $poll + * @return DataResponse + */ + + public function deletePermanently($pollId) { + try { + return new DataResponse($this->pollService->deletePermanently($pollId), Http::STATUS_OK); + } catch (DoesNotExistException $e) { + return new DataResponse('Poll not found', Http::STATUS_NOT_FOUND); + } catch (NotAuthorizedException $e) { + return new DataResponse($e->getMessage(), $e->getStatus()); + } + + } + + /** + * write + * @NoAdminRequired + * @NoCSRFRequired + * @param Array $poll + * @return DataResponse + */ + + public function add($type, $title) { + try { + return new DataResponse($this->pollService->add($type, $title), Http::STATUS_OK); + } catch (NotAuthorizedException $e) { + return new DataResponse($e->getMessage(), $e->getStatus()); + } catch (InvalidPollTypeException $e) { + return new DataResponse($e->getMessage(), $e->getStatus()); + } catch (EmptyTitleException $e) { + return new DataResponse($e->getMessage(), $e->getStatus()); + } + } + + /** + * write + * @NoAdminRequired + * @NoCSRFRequired + * @param Array $poll + * @return DataResponse + */ + + public function update($pollId, $poll) { + try { + return new DataResponse($this->pollService->update($pollId, $poll), Http::STATUS_OK); + } catch (DoesNotExistException $e) { + return new DataResponse('Poll not found', Http::STATUS_NOT_FOUND); + } catch (NotAuthorizedException $e) { + return new DataResponse($e->getMessage(), $e->getStatus()); + } catch (InvalidAccessException $e) { + return new DataResponse($e->getMessage(), $e->getStatus()); + } catch (InvalidShowResultsException $e) { + return new DataResponse($e->getMessage(), $e->getStatus()); + } catch (EmptyTitleException $e) { + return new DataResponse($e->getMessage(), $e->getStatus()); + } + } + + /** + * clone + * @NoAdminRequired + * @NoCSRFRequired + * @param integer $pollId + * @return DataResponse + */ + public function clone($pollId) { + try { + return new DataResponse($this->pollService->clone($pollId), Http::STATUS_OK); + } catch (DoesNotExistException $e) { + return new DataResponse('Poll not found', Http::STATUS_NOT_FOUND); + } catch (NotAuthorizedException $e) { + return new DataResponse($e->getMessage(), $e->getStatus()); + } + } + + /** + * enum + * @NoAdminRequired + * @NoCSRFRequired + * @param Array $poll + * @return DataResponse + */ + + public function enum() { + return [ + 'poll' => $this->pollService->getValidEnum() + ]; + } + + +} diff --git a/lib/Controller/PollController.php b/lib/Controller/PollController.php index 6bf98137..89446ee0 100644 --- a/lib/Controller/PollController.php +++ b/lib/Controller/PollController.php @@ -23,86 +23,44 @@ 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\IUser; - use OCP\Security\ISecureRandom; - - use OCA\Polls\Db\Poll; - use OCA\Polls\Db\PollMapper; - use OCA\Polls\Db\Option; - use OCA\Polls\Db\OptionMapper; - use OCA\Polls\Service\CommentService; - use OCA\Polls\Service\OptionService; - use OCA\Polls\Service\ShareService; - use OCA\Polls\Service\VoteService; - use OCA\Polls\Service\LogService; - use OCA\Polls\Model\Acl; +use Exception; +use OCP\AppFramework\Db\DoesNotExistException; +use OCA\Polls\Exceptions\EmptyTitleException; +use OCA\Polls\Exceptions\InvalidAccessException; +use OCA\Polls\Exceptions\InvalidShowResultsException; +use OCA\Polls\Exceptions\InvalidPollTypeException; +use OCA\Polls\Exceptions\NotAuthorizedException; + +use OCP\IRequest; +use OCP\ILogger; +use OCP\AppFramework\Controller; +use OCP\AppFramework\Http; +use OCP\AppFramework\Http\DataResponse; + +use OCA\Polls\Service\PollService; class PollController extends Controller { - private $userId; - private $pollMapper; - private $optionMapper; - private $logger; - private $poll; - private $logService; - private $commentService; - private $optionService; - private $shareService; - private $voteService; - private $acl; + private $logger; + private $pollService; /** * PollController constructor. * @param string $appName - * @param $userId * @param IRequest $request * @param ILogger $logger - * @param OptionMapper $optionMapper - * @param PollMapper $pollMapper - * @param LogService $logService - * @param CommentService $commentService - * @param OptionService $optionService - * @param ShareService $shareService - * @param VoteService $voteService - * @param Acl $acl + * @param PollService $pollService */ public function __construct( - string $appName, - $userId, + string $appName, IRequest $request, ILogger $logger, - OptionMapper $optionMapper, - PollMapper $pollMapper, - Poll $poll, - LogService $logService, - CommentService $commentService, - OptionService $optionService, - ShareService $shareService, - VoteService $voteService, - Acl $acl + PollService $pollService ) { parent::__construct($appName, $request); - $this->userId = $userId; - $this->pollMapper = $pollMapper; - $this->optionMapper = $optionMapper; + $this->pollService = $pollService; $this->logger = $logger; - $this->poll = $poll; - $this->logService = $logService; - $this->commentService = $commentService; - $this->optionService = $optionService; - $this->shareService = $shareService; - $this->voteService = $voteService; - $this->acl = $acl; } @@ -114,29 +72,13 @@ */ public function list() { - if (\OC::$server->getUserSession()->isLoggedIn()) { - $pollList = []; - - try { - - $polls = $this->pollMapper->findAll(); - // TODO: Not the elegant way. Improvement neccessary - foreach ($polls as $poll) { - $combinedPoll = (object) array_merge( - (array) json_decode(json_encode($poll)), (array) json_decode(json_encode($this->acl->setPollId($poll->getId())))); - if ($combinedPoll->allowView) { - $pollList[] = $combinedPoll; - } - } - - return new DataResponse($pollList, Http::STATUS_OK); - } catch (DoesNotExistException $e) { - return new DataResponse($e, Http::STATUS_NOT_FOUND); - } - } else { - return new DataResponse([], Http::STATUS_OK); + try { + return new DataResponse($this->pollService->list(), Http::STATUS_OK); + } catch (DoesNotExistException $e) { + return new DataResponse([], Http::STATUS_NOT_FOUND); + } catch (NotAuthorizedException $e) { + return new DataResponse($e->getMessage(), $e->getStatus()); } - } @@ -147,117 +89,95 @@ * @param integer $pollId * @return array */ - public function get($pollId) { - - try { - if (!$this->acl->getFoundByToken()) { - $this->acl->setPollId($pollId); - } - - $this->poll = $this->pollMapper->find($pollId); - - if (!$this->acl->getAllowView()) { - return new DataResponse(null, Http::STATUS_UNAUTHORIZED); - } - - return new DataResponse([ - 'acl' => $this->acl, - 'poll' => $this->poll, - 'comments' => $this->commentService->list($pollId), - 'options' => $this->optionService->list($pollId), - 'shares' => $this->shareService->list($pollId), - 'votes' => $this->voteService->list($pollId) - ], Http::STATUS_OK); - + public function get($pollId, $token) { + try { + return new DataResponse($this->pollService->get($pollId, $token), Http::STATUS_OK); } catch (DoesNotExistException $e) { - $this->logger->info('Poll ' . $pollId . ' not found!', ['app' => 'polls']); - return new DataResponse(null, Http::STATUS_NOT_FOUND); - } + return new DataResponse('Not found', Http::STATUS_NOT_FOUND); + } catch (NotAuthorizedException $e) { + return new DataResponse($e->getMessage(), $e->getStatus()); + } } /** - * getByToken - * Read all options of a poll based on a share token and return list as array + * delete * @NoAdminRequired - * @PublicPage * @NoCSRFRequired - * @param string $token + * @param Array $poll * @return DataResponse */ - public function getByToken($token) { + + public function delete($pollId) { try { - return $this->get($this->acl->setToken($token)->getPollId()); + return new DataResponse($this->pollService->delete($pollId), Http::STATUS_OK); } catch (DoesNotExistException $e) { - return new DataResponse($e, Http::STATUS_NOT_FOUND); + return new DataResponse('Poll not found', Http::STATUS_NOT_FOUND); + } catch (NotAuthorizedException $e) { + return new DataResponse($e->getMessage(), $e->getStatus()); } - } /** - * delete + * deletePermanently * @NoAdminRequired * @NoCSRFRequired * @param Array $poll * @return DataResponse */ - public function delete($pollId) { - + public function deletePermanently($pollId) { try { - // Find existing poll - $this->poll = $this->pollMapper->find($pollId); - $this->acl->setPollId($this->poll->getId()); - - if (!$this->acl->getAllowEdit()) { - $this->logger->alert('Unauthorized delete attempt from user ' . $this->userId); - return new DataResponse(['message' => 'Unauthorized write attempt.'], Http::STATUS_UNAUTHORIZED); - } + return new DataResponse($this->pollService->deletePermanently($pollId), Http::STATUS_OK); + } catch (DoesNotExistException $e) { + return new DataResponse('Poll not found', Http::STATUS_NOT_FOUND); + } catch (NotAuthorizedException $e) { + return new DataResponse($e->getMessage(), $e->getStatus()); + } + } - if ($this->poll->getDeleted()) { - $this->poll->setDeleted(0); - } else { - $this->poll->setDeleted(time()); - } - $this->pollMapper->update($this->poll); - $this->logService->setLog($this->poll->getId(), 'deletePoll'); - return new DataResponse(['deleted' => $pollId], Http::STATUS_OK); + /** + * write + * @NoAdminRequired + * @NoCSRFRequired + * @param Array $poll + * @return DataResponse + */ - } catch (Exception $e) { - return new DataResponse($e, Http::STATUS_NOT_FOUND); + public function add($type, $title) { + try { + return new DataResponse($this->pollService->add($type, $title), Http::STATUS_OK); + } catch (NotAuthorizedException $e) { + return new DataResponse($e->getMessage(), $e->getStatus()); + } catch (InvalidPollTypeException $e) { + return new DataResponse($e->getMessage(), $e->getStatus()); + } catch (EmptyTitleException $e) { + return new DataResponse($e->getMessage(), $e->getStatus()); } } /** - * deletePermanently + * write * @NoAdminRequired * @NoCSRFRequired * @param Array $poll * @return DataResponse */ - public function deletePermanently($pollId) { - + public function update($pollId, $poll) { + $this->logger->alert(json_encode($poll)); try { - // Find existing poll - $this->poll = $this->pollMapper->find($pollId); - $this->acl->setPollId($this->poll->getId()); - - if (!$this->acl->getAllowEdit()) { - $this->logger->alert('Unauthorized delete attempt from user ' . $this->userId); - return new DataResponse(['message' => 'Unauthorized write attempt.'], Http::STATUS_UNAUTHORIZED); - } - - if (!$this->poll->getDeleted()) { - $this->logger->alert('user ' . $this->userId . ' trying to permanently delete active poll'); - return new DataResponse(['message' => 'Permanent deletion of active poll.'], Http::STATUS_CONFLICT); - } - - $this->pollMapper->delete($this->poll); - return new DataResponse([], Http::STATUS_OK); - - } catch (Exception $e) { - return new DataResponse($e, Http::STATUS_NOT_FOUND); + return new DataResponse($this->pollService->update($pollId, $poll), Http::STATUS_OK); + } catch (DoesNotExistException $e) { + return new DataResponse('Poll not found', Http::STATUS_NOT_FOUND); + } catch (NotAuthorizedException $e) { + return new DataResponse($e->getMessage(), $e->getStatus()); + } catch (InvalidAccessException $e) { + return new DataResponse($e->getMessage(), $e->getStatus()); + } catch (InvalidShowResultsException $e) { + return new DataResponse($e->getMessage(), $e->getStatus()); + } catch (EmptyTitleException $e) { + return new DataResponse($e->getMessage(), $e->getStatus()); } } @@ -265,50 +185,24 @@ * write * @NoAdminRequired * @NoCSRFRequired + * @depicated * @param Array $poll * @return DataResponse */ public function write($poll) { - try { - // Find existing poll - $this->poll = $this->pollMapper->find($poll['id']); - $this->acl->setPollId($this->poll->getId()); - if (!$this->acl->getAllowEdit()) { - $this->logger->alert('Unauthorized write attempt from user ' . $this->userId); - return new DataResponse(['message' => 'Unauthorized write attempt.'], Http::STATUS_UNAUTHORIZED); - } - - } catch (Exception $e) { - $this->poll = new Poll(); - - $this->poll->setType($poll['type']); - $this->poll->setOwner($this->userId); - $this->poll->setCreated(time()); - } finally { - $this->poll->setTitle($poll['title']); - $this->poll->setDescription($poll['description']); - $this->poll->setAccess($poll['access']); - $this->poll->setExpire($poll['expire']); - $this->poll->setAnonymous(intval($poll['anonymous'])); - $this->poll->setFullAnonymous(0); - $this->poll->setAllowMaybe(intval($poll['allowMaybe'])); - $this->poll->setVoteLimit(intval($poll['voteLimit'])); - $this->poll->setSettings(''); - $this->poll->setOptions(''); - $this->poll->setShowResults($poll['showResults']); - $this->poll->setDeleted($poll['deleted']); - $this->poll->setAdminAccess($poll['adminAccess']); - - if ($this->poll->getId() > 0) { - $this->pollMapper->update($this->poll); - $this->logService->setLog($this->poll->getId(), 'updatePoll'); - } else { - $this->pollMapper->insert($this->poll); - $this->logService->setLog($this->poll->getId(), 'addPoll'); - } - return $this->get($this->poll->getId()); + return new DataResponse($this->pollService->write($poll), Http::STATUS_OK); + } catch (DoesNotExistException $e) { + return new DataResponse('Poll not found', Http::STATUS_NOT_FOUND); + } catch (NotAuthorizedException $e) { + return new DataResponse($e->getMessage(), $e->getStatus()); + } catch (InvalidAccessException $e) { + return new DataResponse($e->getMessage(), $e->getStatus()); + } catch (InvalidShowResultsException $e) { + return new DataResponse($e->getMessage(), $e->getStatus()); + } catch (EmptyTitleException $e) { + return new DataResponse($e->getMessage(), $e->getStatus()); } } @@ -320,40 +214,13 @@ * @return DataResponse */ public function clone($pollId) { - $this->poll = $this->pollMapper->find($pollId); - - $clonePoll = new Poll(); - $clonePoll->setOwner($this->userId); - $clonePoll->setCreated(time()); - $clonePoll->setTitle('Clone of ' . $this->poll->getTitle()); - $clonePoll->setDeleted(0); - - $clonePoll->setType($this->poll->getType()); - $clonePoll->setDescription($this->poll->getDescription()); - $clonePoll->setAccess($this->poll->getAccess()); - $clonePoll->setExpire($this->poll->getExpire()); - $clonePoll->setAnonymous(intval($this->poll->getAnonymous())); - $clonePoll->setFullAnonymous(0); - $clonePoll->setAllowMaybe(intval($this->poll->getAllowMaybe())); - $clonePoll->setVoteLimit(intval($this->poll->getVoteLimit())); - $clonePoll->setSettings(''); - $clonePoll->setOptions(''); - $clonePoll->setShowResults($this->poll->getShowResults()); - $clonePoll->setAdminAccess($this->poll->getAdminAccess()); - - $this->pollMapper->insert($clonePoll); - $this->logService->setLog($clonePoll->getId(), 'addPoll'); - - foreach ($this->optionMapper->findByPoll($pollId) as $option) { - $newOption = new Option(); - $newOption->setPollId($clonePoll->getId()); - $newOption->setPollOptionText($option->getPollOptionText()); - $newOption->setTimestamp($option->getTimestamp()); - - $this->optionMapper->insert($newOption); + try { + return new DataResponse($this->pollService->clone($pollId), Http::STATUS_OK); + } catch (DoesNotExistException $e) { + return new DataResponse('Poll not found', Http::STATUS_NOT_FOUND); + } catch (NotAuthorizedException $e) { + return new DataResponse($e->getMessage(), $e->getStatus()); } - return new DataResponse(['pollId' => $clonePoll->getId()], Http::STATUS_OK); - } } diff --git a/lib/Controller/SubscriptionController.php b/lib/Controller/SubscriptionController.php index d201fa6e..f3cc655a 100644 --- a/lib/Controller/SubscriptionController.php +++ b/lib/Controller/SubscriptionController.php @@ -28,25 +28,23 @@ 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 OCA\Polls\Db\Subscription; -use OCA\Polls\Db\SubscriptionMapper; +use OCA\Polls\Service\SubscriptionService; class SubscriptionController extends Controller { private $userId; - private $mapper; + private $subscriptionService; private $logger; /** * SubscriptionController constructor. * @param string $appName * @param $UserId - * @param SubscriptionMapper $mapper + * @param SubscriptionService $subscriptionService * @param IRequest $request * @param ILogger $logger */ @@ -54,14 +52,14 @@ class SubscriptionController extends Controller { public function __construct( string $appName, $userId, - SubscriptionMapper $mapper, + SubscriptionService $subscriptionService, IRequest $request, ILogger $logger ) { parent::__construct($appName, $request); $this->userId = $userId; - $this->mapper = $mapper; + $this->subscriptionService = $subscriptionService; $this->logger = $logger; } |