diff options
author | dartcafe <github@dartcafe.de> | 2020-08-20 20:27:58 +0300 |
---|---|---|
committer | dartcafe <github@dartcafe.de> | 2020-08-20 20:27:58 +0300 |
commit | c7f111b583bc1a27c2654c94d2c067fab329ce23 (patch) | |
tree | 8196c0049623d17b90a74d1db8c48c5eef2a07d7 /lib | |
parent | da6e04caf896491ddf32ae8fc721a5eaec8bfdd8 (diff) |
fixing cloneOptions
Diffstat (limited to 'lib')
-rw-r--r-- | lib/Controller/OptionController.php | 7 | ||||
-rw-r--r-- | lib/Exceptions/DuplicateEntryException.php | 40 | ||||
-rw-r--r-- | lib/Service/OptionService.php | 9 |
3 files changed, 54 insertions, 2 deletions
diff --git a/lib/Controller/OptionController.php b/lib/Controller/OptionController.php index 3b1e7f1e..466902ee 100644 --- a/lib/Controller/OptionController.php +++ b/lib/Controller/OptionController.php @@ -24,6 +24,7 @@ namespace OCA\Polls\Controller; use Exception; +use OCA\Polls\Exceptions\DuplicateEntryException; use OCP\IRequest; @@ -70,7 +71,11 @@ class OptionController extends Controller { * @return DataResponse */ public function add($pollId, $timestamp = 0, $pollOptionText = '') { - return new DataResponse(['option' => $this->optionService->add($pollId, $timestamp, $pollOptionText)], Http::STATUS_OK); + try { + return new DataResponse(['option' => $this->optionService->add($pollId, $timestamp, $pollOptionText)], Http::STATUS_OK); + } catch (DuplicateEntryException $e) { + return new DataResponse(['error' => $e->getMessage()], $e->getStatus()); + } } /** diff --git a/lib/Exceptions/DuplicateEntryException.php b/lib/Exceptions/DuplicateEntryException.php new file mode 100644 index 00000000..d019414f --- /dev/null +++ b/lib/Exceptions/DuplicateEntryException.php @@ -0,0 +1,40 @@ +<?php +/** + * @copyright Copyright (c) 2020 René Gieling <github@dartcafe.de> + * + * @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\Exceptions; + +use OCP\AppFramework\Http; + +class DuplicateEntryException extends \Exception { + /** + * DuplicateEntryException Constructor + * @param string $e exception message + */ + public function __construct($e = 'Duplicate Entry') { + parent::__construct($e); + } + public function getStatus() { + return Http::STATUS_CONFLICT; + } + +} diff --git a/lib/Service/OptionService.php b/lib/Service/OptionService.php index 7208a154..14d6e8a1 100644 --- a/lib/Service/OptionService.php +++ b/lib/Service/OptionService.php @@ -27,6 +27,8 @@ use Exception; use OCP\AppFramework\Db\DoesNotExistException; use OCA\Polls\Exceptions\NotAuthorizedException; use OCA\Polls\Exceptions\BadRequestException; +use OCA\Polls\Exceptions\DuplicateEntryException; +use Doctrine\DBAL\Exception\UniqueConstraintViolationException; use OCA\Polls\Db\OptionMapper; use OCA\Polls\Db\Option; @@ -124,7 +126,12 @@ class OptionService { $this->option->setPollId($pollId); $this->setOption($timestamp, $pollOptionText, 0); - return $this->optionMapper->insert($this->option); + try { + return $this->optionMapper->insert($this->option); + } catch (UniqueConstraintViolationException $e) { + throw new DuplicateEntryException('This option already exists'); + } + } /** |