nextcloud < v22.0 throws UniqueConstraintViolationException. This can be reverted after v21 reaches end of support

This commit is contained in:
thrillfall 2021-08-21 20:20:24 +02:00
parent ab155ca94a
commit 57f0691b4c
2 changed files with 40 additions and 9 deletions

View File

@ -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);
}
}

View File

@ -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);
}
}