From b09210655ed2a1a7fa3ee80cdc2cb95261d96a24 Mon Sep 17 00:00:00 2001 From: thrillfall Date: Sun, 11 Jul 2021 22:52:30 +0200 Subject: [PATCH] unique key violation exception is thrown as \Exception thus we need to check the reason --- lib/Controller/EpisodeActionController.php | 14 ++++++++------ .../SubscriptionChange/SubscriptionChangeSaver.php | 14 ++++++++------ 2 files changed, 16 insertions(+), 12 deletions(-) diff --git a/lib/Controller/EpisodeActionController.php b/lib/Controller/EpisodeActionController.php index 9167cb0..5642061 100644 --- a/lib/Controller/EpisodeActionController.php +++ b/lib/Controller/EpisodeActionController.php @@ -4,7 +4,6 @@ declare(strict_types=1); namespace OCA\GPodderSync\Controller; use DateTime; -use Doctrine\DBAL\Exception\UniqueConstraintViolationException; use GuzzleHttp\Psr7\Response; use OCA\GPodderSync\Core\EpisodeAction\EpisodeActionReader; use OCA\GPodderSync\Db\EpisodeAction\EpisodeActionEntity; @@ -12,6 +11,7 @@ use OCA\GPodderSync\Db\EpisodeAction\EpisodeActionRepository; use OCA\GPodderSync\Db\EpisodeAction\EpisodeActionWriter; use OCP\AppFramework\Controller; use OCP\AppFramework\Http\JSONResponse; +use OCP\DB\Exception; use OCP\IRequest; class EpisodeActionController extends Controller { @@ -66,10 +66,12 @@ class EpisodeActionController extends Controller { try { return $this->episodeActionWriter->save($episodeActionEntity); - } catch (UniqueConstraintViolationException $ex) { - $IdEpisodeActionEntityToUpdate = $this->episodeActionRepository->findByEpisode($episodeAction->getEpisode(), $this->userId)->getId(); - $episodeActionEntity->setId($IdEpisodeActionEntityToUpdate); - return $this->episodeActionWriter->update($episodeActionEntity); + } catch (\Exception $exception) { + if ($exception->getReason() === Exception::REASON_UNIQUE_CONSTRAINT_VIOLATION) { + $idEpisodeActionEntityToUpdate = $this->episodeActionRepository->findByEpisode($episodeAction->getEpisode(), $this->userId)->getId(); + $episodeActionEntity->setId($idEpisodeActionEntityToUpdate); + return $this->episodeActionWriter->update($episodeActionEntity); + } } } @@ -81,7 +83,7 @@ class EpisodeActionController extends Controller { * @param int $since * @return JSONResponse */ - public function list(int $since): JSONResponse { + public function list(int $since): JSONResponse { $sinceDatetime = $this->createDateTimeFromTimestamp($since); return new JSONResponse([ "actions" => $this->episodeActionRepository->findAll($sinceDatetime, $this->userId), diff --git a/lib/Core/SubscriptionChange/SubscriptionChangeSaver.php b/lib/Core/SubscriptionChange/SubscriptionChangeSaver.php index 0ce1338..70a39f4 100644 --- a/lib/Core/SubscriptionChange/SubscriptionChangeSaver.php +++ b/lib/Core/SubscriptionChange/SubscriptionChangeSaver.php @@ -3,10 +3,10 @@ declare(strict_types=1); namespace OCA\GPodderSync\Core\SubscriptionChange; -use Doctrine\DBAL\Exception\UniqueConstraintViolationException; use OCA\GPodderSync\Db\SubscriptionChange\SubscriptionChangeEntity; use OCA\GPodderSync\Db\SubscriptionChange\SubscriptionChangeRepository; use OCA\GPodderSync\Db\SubscriptionChange\SubscriptionChangeWriter; +use OCP\DB\Exception; class SubscriptionChangeSaver { @@ -37,7 +37,7 @@ class SubscriptionChangeSaver { $this->subscriptionChangeRequestParser = $subscriptionChangeRequestParser; } - public function saveSubscriptionChanges(string $urlsSubscribed, string $urlsUnsubscribed, string $userId) : void { + public function saveSubscriptionChanges(string $urlsSubscribed, string $urlsUnsubscribed, string $userId): void { $subscriptionChanges = $this->subscriptionChangeRequestParser->createSubscriptionChangeList($urlsSubscribed, $urlsUnsubscribed); foreach ($subscriptionChanges as $urlChangedSubscriptionStatus) { $subscriptionChangeEntity = new SubscriptionChangeEntity(); @@ -48,10 +48,12 @@ class SubscriptionChangeSaver { try { $this->subscriptionChangeWriter->create($subscriptionChangeEntity); - } catch (UniqueConstraintViolationException $ex) { - $idEpisodeActionEntityToUpdate = $this->subscriptionChangeRepository->findByUrl($subscriptionChangeEntity->getUrl(), $userId)->getId(); - $subscriptionChangeEntity->setId($idEpisodeActionEntityToUpdate); - $this->subscriptionChangeWriter->update($subscriptionChangeEntity); + } catch (\Exception $exception) { + if ($exception->getReason() === Exception::REASON_UNIQUE_CONSTRAINT_VIOLATION) { + $idEpisodeActionEntityToUpdate = $this->subscriptionChangeRepository->findByUrl($subscriptionChangeEntity->getUrl(), $userId)->getId(); + $subscriptionChangeEntity->setId($idEpisodeActionEntityToUpdate); + $this->subscriptionChangeWriter->update($subscriptionChangeEntity); + } } } }