diff --git a/lib/Controller/EpisodeActionController.php b/lib/Controller/EpisodeActionController.php index ce92782..69ecafe 100644 --- a/lib/Controller/EpisodeActionController.php +++ b/lib/Controller/EpisodeActionController.php @@ -5,6 +5,7 @@ namespace OCA\GPodderSync\Controller; use DateTime; use DateTimeZone; +use Doctrine\DBAL\Exception\UniqueConstraintViolationException; use GuzzleHttp\Psr7\Response; use OCA\GPodderSync\Core\EpisodeAction\EpisodeActionReader; use OCA\GPodderSync\Db\EpisodeAction\EpisodeActionEntity; @@ -67,11 +68,11 @@ class EpisodeActionController extends Controller { try { return $this->episodeActionWriter->save($episodeActionEntity); + } catch (UniqueConstraintViolationException $uniqueConstraintViolationException) { + return $this->updateEpisodeAction($episodeAction, $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); + return $this->updateEpisodeAction($episodeAction, $episodeActionEntity); } } } @@ -114,4 +115,17 @@ class EpisodeActionController extends Controller { ->setTimezone(new DateTimeZone('UTC')) ->format("Y-m-d\TH:i:s"); } + + /** + * @param \OCA\GPodderSync\Core\EpisodeAction\EpisodeAction $episodeAction + * @param EpisodeActionEntity $episodeActionEntity + * + * @return EpisodeActionEntity + */ + private function updateEpisodeAction(\OCA\GPodderSync\Core\EpisodeAction\EpisodeAction $episodeAction, EpisodeActionEntity $episodeActionEntity): EpisodeActionEntity + { + $idEpisodeActionEntityToUpdate = $this->episodeActionRepository->findByEpisode($episodeAction->getEpisode(), $this->userId)->getId(); + $episodeActionEntity->setId($idEpisodeActionEntityToUpdate); + return $this->episodeActionWriter->update($episodeActionEntity); + } } diff --git a/lib/Core/SubscriptionChange/SubscriptionChangeSaver.php b/lib/Core/SubscriptionChange/SubscriptionChangeSaver.php index 588214f..aabe997 100644 --- a/lib/Core/SubscriptionChange/SubscriptionChangeSaver.php +++ b/lib/Core/SubscriptionChange/SubscriptionChangeSaver.php @@ -3,12 +3,14 @@ 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 { +class SubscriptionChangeSaver +{ /** * @var SubscriptionChangesReader @@ -31,13 +33,15 @@ class SubscriptionChangeSaver { SubscriptionChangeRequestParser $subscriptionChangeRequestParser, SubscriptionChangeRepository $subscriptionChangeRepository, SubscriptionChangeWriter $subscriptionChangeWriter - ) { + ) + { $this->subscriptionChangeRepository = $subscriptionChangeRepository; $this->subscriptionChangeWriter = $subscriptionChangeWriter; $this->subscriptionChangeRequestParser = $subscriptionChangeRequestParser; } - public function saveSubscriptionChanges(array $urlsSubscribed, array $urlsUnsubscribed, string $userId): void { + public function saveSubscriptionChanges(array $urlsSubscribed, array $urlsUnsubscribed, string $userId): void + { $subscriptionChanges = $this->subscriptionChangeRequestParser->createSubscriptionChangeList($urlsSubscribed, $urlsUnsubscribed); foreach ($subscriptionChanges as $urlChangedSubscriptionStatus) { $subscriptionChangeEntity = new SubscriptionChangeEntity(); @@ -48,15 +52,28 @@ class SubscriptionChangeSaver { try { $this->subscriptionChangeWriter->create($subscriptionChangeEntity); + } catch (UniqueConstraintViolationException $uniqueConstraintViolationException) { + $this->updateSubscription($subscriptionChangeEntity, $userId); } 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); + $this->updateSubscription($subscriptionChangeEntity, $userId); } } } } + /** + * @param SubscriptionChangeEntity $subscriptionChangeEntity + * @param string $userId + * + * @return void + */ + private function updateSubscription(SubscriptionChangeEntity $subscriptionChangeEntity, string $userId): void + { + $idEpisodeActionEntityToUpdate = $this->subscriptionChangeRepository->findByUrl($subscriptionChangeEntity->getUrl(), $userId)->getId(); + $subscriptionChangeEntity->setId($idEpisodeActionEntityToUpdate); + $this->subscriptionChangeWriter->update($subscriptionChangeEntity); + } + }