fix #109 search for episode action by guid only (retry with episode url if nothing found) thus avoid multiple results

This commit is contained in:
thrillfall 2023-02-24 16:48:16 +01:00
parent 4999b96456
commit a1893061ce
8 changed files with 141 additions and 116 deletions

View File

@ -30,11 +30,6 @@ class EpisodeActionSaver
$this->episodeActionReader = $episodeActionReader; $this->episodeActionReader = $episodeActionReader;
} }
/**
* @param array $episodeActionsArray
* @param string $userId
* @return EpisodeActionEntity[]
*/
public function saveEpisodeActions(array $episodeActionsArray, string $userId): array public function saveEpisodeActions(array $episodeActionsArray, string $userId): array
{ {
$episodeActions = $this->episodeActionReader->fromArray($episodeActionsArray); $episodeActions = $this->episodeActionReader->fromArray($episodeActionsArray);
@ -68,15 +63,7 @@ class EpisodeActionSaver
string $userId string $userId
): EpisodeActionEntity ): EpisodeActionEntity
{ {
$identifier = $episodeActionEntity->getGuid() ?? $episodeActionEntity->getEpisode(); $episodeActionToUpdate = $this->findEpisodeActionToUpdate($episodeActionEntity, $userId);
$episodeActionToUpdate = $this->episodeActionRepository->findByEpisodeIdentifier(
$identifier,
$userId
);
if ($episodeActionToUpdate === null && $episodeActionEntity->getGuid() !== null) {
$episodeActionToUpdate = $this->getOldEpisodeActionByEpisodeUrl($episodeActionEntity->getEpisode(), $userId);
}
$episodeActionEntity->setId($episodeActionToUpdate->getId()); $episodeActionEntity->setId($episodeActionToUpdate->getId());
@ -85,14 +72,6 @@ class EpisodeActionSaver
return $this->episodeActionWriter->update($episodeActionEntity); return $this->episodeActionWriter->update($episodeActionEntity);
} }
private function getOldEpisodeActionByEpisodeUrl(string $episodeUrl, string $userId): ?EpisodeAction
{
return $this->episodeActionRepository->findByEpisodeIdentifier(
$episodeUrl,
$userId
);
}
private function ensureGuidDoesNotGetNulledWithOldData(EpisodeAction $episodeActionToUpdate, EpisodeActionEntity $episodeActionEntity): void private function ensureGuidDoesNotGetNulledWithOldData(EpisodeAction $episodeActionToUpdate, EpisodeActionEntity $episodeActionEntity): void
{ {
$existingGuid = $episodeActionToUpdate->getGuid(); $existingGuid = $episodeActionToUpdate->getGuid();
@ -116,4 +95,24 @@ class EpisodeActionSaver
return $episodeActionEntity; return $episodeActionEntity;
} }
private function findEpisodeActionToUpdate(EpisodeActionEntity $episodeActionEntity, string $userId): ?EpisodeAction
{
$episodeAction = null;
if ($episodeActionEntity->getGuid() !== null) {
$episodeAction = $this->episodeActionRepository->findByGuid(
$episodeActionEntity->getGuid(),
$userId
);
}
if ($episodeAction === null) {
$episodeAction = $this->episodeActionRepository->findByEpisodeUrl(
$episodeActionEntity->getEpisode(),
$userId
);
}
return $episodeAction;
}
} }

View File

@ -43,16 +43,35 @@ class EpisodeActionMapper extends QBMapper
* @param string $userId * @param string $userId
* @return EpisodeActionEntity|null * @return EpisodeActionEntity|null
*/ */
public function findByEpisodeIdentifier(string $episodeIdentifier, string $userId) : ?EpisodeActionEntity public function findByEpisodeUrl(string $episodeIdentifier, string $userId) : ?EpisodeActionEntity
{ {
$qb = $this->db->getQueryBuilder(); $qb = $this->db->getQueryBuilder();
$qb->select('*') $qb->select('*')
->from($this->getTableName()) ->from($this->getTableName())
->where( ->where(
$qb->expr()->orX( $qb->expr()->eq('episode', $qb->createNamedParameter($episodeIdentifier))
$qb->expr()->eq('episode', $qb->createNamedParameter($episodeIdentifier)), )
$qb->expr()->eq('guid', $qb->createNamedParameter($episodeIdentifier))) ->andWhere(
$qb->expr()->eq('user_id', $qb->createNamedParameter($userId))
);
try {
/** @var EpisodeActionEntity $episodeActionEntity */
return $this->findEntity($qb);
} catch (DoesNotExistException|MultipleObjectsReturnedException|Exception $e) {
return null;
}
}
public function findByGuid(string $guid, string $userId) : ?EpisodeActionEntity
{
$qb = $this->db->getQueryBuilder();
$qb->select('*')
->from($this->getTableName())
->where(
$qb->expr()->eq('guid', $qb->createNamedParameter($guid))
) )
->andWhere( ->andWhere(
$qb->expr()->eq('user_id', $qb->createNamedParameter($userId)) $qb->expr()->eq('user_id', $qb->createNamedParameter($userId))

View File

@ -28,8 +28,20 @@ class EpisodeActionRepository {
return $episodeActions; return $episodeActions;
} }
public function findByEpisodeIdentifier(string $identifier, string $userId): ?EpisodeAction { public function findByEpisodeUrl(string $episodeUrl, string $userId): ?EpisodeAction {
$episodeActionEntity = $this->episodeActionMapper->findByEpisodeIdentifier($identifier, $userId); $episodeActionEntity = $this->episodeActionMapper->findByEpisodeUrl($episodeUrl, $userId);
if ($episodeActionEntity === null) {
return null;
}
return $this->mapEntityToEpisodeAction(
$episodeActionEntity
);
}
public function findByGuid(string $guid, string $userId): ?EpisodeAction {
$episodeActionEntity = $this->episodeActionMapper->findByGuid($guid, $userId);
if ($episodeActionEntity === null) { if ($episodeActionEntity === null) {
return null; return null;

View File

@ -3,7 +3,9 @@ declare(strict_types=1);
namespace tests\Integration\Controller; namespace tests\Integration\Controller;
use OC\Security\SecureRandom; require_once __DIR__ . '/../../Helper/DatabaseTransaction.php';
require_once __DIR__ . '/../../Helper/Writer/TestWriter.php';
use OCA\GPodderSync\Controller\EpisodeActionController; use OCA\GPodderSync\Controller\EpisodeActionController;
use OCA\GPodderSync\Core\EpisodeAction\EpisodeActionSaver; use OCA\GPodderSync\Core\EpisodeAction\EpisodeActionSaver;
use OCA\GPodderSync\Db\EpisodeAction\EpisodeActionEntity; use OCA\GPodderSync\Db\EpisodeAction\EpisodeActionEntity;
@ -12,7 +14,6 @@ use OCA\GPodderSync\Db\EpisodeAction\EpisodeActionRepository;
use OCA\GPodderSync\Db\EpisodeAction\EpisodeActionWriter; use OCA\GPodderSync\Db\EpisodeAction\EpisodeActionWriter;
use OCP\AppFramework\App; use OCP\AppFramework\App;
use OCP\AppFramework\IAppContainer; use OCP\AppFramework\IAppContainer;
use OCP\IConfig;
use OCP\IRequest; use OCP\IRequest;
use Test\TestCase; use Test\TestCase;
use tests\Helper\DatabaseTransaction; use tests\Helper\DatabaseTransaction;

View File

@ -22,19 +22,13 @@ class EpisodeActionGuidMigrationTest extends TestCase
private IAppContainer $container; private IAppContainer $container;
/** private EpisodeActionWriter $episodeActionWriter;
* @var EpisodeActionWriter
*/
private $episodeActionWriter;
/** private EpisodeActionRepository $episodeActionRepository;
* @var EpisodeActionRepository
*/
private $episodeActionRepository;
public function setUp(): void { public function setUp(): void {
parent::setUp(); parent::setUp();
$app = new App('gpoddersync'); $app = new App('nextcloud-gpodder');
$this->container = $app->getContainer(); $this->container = $app->getContainer();
$this->episodeActionWriter = $this->container->get(EpisodeActionWriter::class); $this->episodeActionWriter = $this->container->get(EpisodeActionWriter::class);
$this->episodeActionRepository = $this->container->get(EpisodeActionRepository::class); $this->episodeActionRepository = $this->container->get(EpisodeActionRepository::class);
@ -85,7 +79,7 @@ class EpisodeActionGuidMigrationTest extends TestCase
self::assertSame( self::assertSame(
$savedEpisodeActionEntity->getId(), $savedEpisodeActionEntity->getId(),
$this->episodeActionRepository->findByEpisodeIdentifier($episodeActionEntity->getEpisode(), self::USER_ID_0)->getId() $this->episodeActionRepository->findByEpisodeUrl($episodeActionEntity->getEpisode(), self::USER_ID_0)->getId()
); );
//update same episode action again this time with guid //update same episode action again this time with guid
@ -96,12 +90,12 @@ class EpisodeActionGuidMigrationTest extends TestCase
self::assertSame( self::assertSame(
$savedEpisodeActionEntityWithGuid->getId(), $savedEpisodeActionEntityWithGuid->getId(),
$this->episodeActionRepository->findByEpisodeIdentifier($episodeActionEntityWithGuid->getEpisode(), self::USER_ID_0)->getId() $this->episodeActionRepository->findByEpisodeUrl($episodeActionEntityWithGuid->getEpisode(), self::USER_ID_0)->getId()
); );
self::assertSame( self::assertSame(
$savedEpisodeActionEntityWithGuid->getId(), $savedEpisodeActionEntityWithGuid->getId(),
$this->episodeActionRepository->findByEpisodeIdentifier($episodeActionEntityWithGuid->getGuid(), self::USER_ID_0)->getId() $this->episodeActionRepository->findByGuid($episodeActionEntityWithGuid->getGuid(), self::USER_ID_0)->getId()
); );
} }

View File

@ -49,7 +49,7 @@ class EpisodeActionRepositoryTest extends \Test\TestCase
/** @var $episodeActionRepository EpisodeActionRepository */ /** @var $episodeActionRepository EpisodeActionRepository */
$episodeActionRepository = $this->container->get(EpisodeActionRepository::class); $episodeActionRepository = $this->container->get(EpisodeActionRepository::class);
$retrievedEpisodeActionEntity = $episodeActionRepository->findByEpisodeIdentifier($guid, self::USER_ID_0); $retrievedEpisodeActionEntity = $episodeActionRepository->findByGuid($guid, self::USER_ID_0);
self::assertSame('2021-08-22T23:58:56', $retrievedEpisodeActionEntity->getTimestamp()); self::assertSame('2021-08-22T23:58:56', $retrievedEpisodeActionEntity->getTimestamp());
} }

View File

@ -10,7 +10,7 @@ use Test\TestCase;
/** /**
* @group DB * @group DB
*/ */
class EpisodeActionSaverGuidBackwardCompatbilityTest extends TestCase class EpisodeActionSaverGuidBackwardCompatibilityTest extends TestCase
{ {
private const USER_ID_0 = "testuser0"; private const USER_ID_0 = "testuser0";

View File

@ -79,7 +79,7 @@ class TimestampMigrationTest extends TestCase
$trueCrimeEpisodeActionEntity->setGuid(uniqid("self::TEST_GUID_1234")); $trueCrimeEpisodeActionEntity->setGuid(uniqid("self::TEST_GUID_1234"));
$this->episodeActionWriter->save($trueCrimeEpisodeActionEntity); $this->episodeActionWriter->save($trueCrimeEpisodeActionEntity);
$episodeActionBeforeConversion = $this->episodeActionMapper->findByEpisodeIdentifier( $episodeActionBeforeConversion = $this->episodeActionMapper->findByGuid(
$scienceEpisodeActionEntity->getGuid(), $scienceEpisodeActionEntity->getGuid(),
self::ADMIN self::ADMIN
); );
@ -92,7 +92,7 @@ class TimestampMigrationTest extends TestCase
$timestampMigration = new TimestampMigration($this->dbConnection, $this->migrationConfig); $timestampMigration = new TimestampMigration($this->dbConnection, $this->migrationConfig);
$timestampMigration->run($this->createMock(SimpleOutput::class)); $timestampMigration->run($this->createMock(SimpleOutput::class));
$scienceEpisodeActionAfterConversion = $this->episodeActionMapper->findByEpisodeIdentifier( $scienceEpisodeActionAfterConversion = $this->episodeActionMapper->findByGuid(
$scienceEpisodeActionEntity->getGuid(), $scienceEpisodeActionEntity->getGuid(),
self::ADMIN self::ADMIN
); );
@ -101,7 +101,7 @@ class TimestampMigrationTest extends TestCase
$scienceEpisodeActionAfterConversion->getTimestampEpoch() $scienceEpisodeActionAfterConversion->getTimestampEpoch()
); );
$trueCrimeEpisodeActionAfterConversion = $this->episodeActionMapper->findByEpisodeIdentifier( $trueCrimeEpisodeActionAfterConversion = $this->episodeActionMapper->findByGuid(
$trueCrimeEpisodeActionEntity->getGuid(), $trueCrimeEpisodeActionEntity->getGuid(),
self::ADMIN self::ADMIN
); );