From c7f111b583bc1a27c2654c94d2c067fab329ce23 Mon Sep 17 00:00:00 2001 From: dartcafe Date: Thu, 20 Aug 2020 19:27:58 +0200 Subject: fixing cloneOptions --- lib/Controller/OptionController.php | 7 +++++- lib/Exceptions/DuplicateEntryException.php | 40 ++++++++++++++++++++++++++++++ lib/Service/OptionService.php | 9 ++++++- 3 files changed, 54 insertions(+), 2 deletions(-) create mode 100644 lib/Exceptions/DuplicateEntryException.php (limited to 'lib') 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 @@ + + * + * @author René Gieling + * + * @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 . + * + */ + +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'); + } + } /** -- cgit v1.2.3