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;
}
/**
* @param array $episodeActionsArray
* @param string $userId
* @return EpisodeActionEntity[]
*/
public function saveEpisodeActions(array $episodeActionsArray, string $userId): array
{
$episodeActions = $this->episodeActionReader->fromArray($episodeActionsArray);
@ -68,15 +63,7 @@ class EpisodeActionSaver
string $userId
): EpisodeActionEntity
{
$identifier = $episodeActionEntity->getGuid() ?? $episodeActionEntity->getEpisode();
$episodeActionToUpdate = $this->episodeActionRepository->findByEpisodeIdentifier(
$identifier,
$userId
);
if ($episodeActionToUpdate === null && $episodeActionEntity->getGuid() !== null) {
$episodeActionToUpdate = $this->getOldEpisodeActionByEpisodeUrl($episodeActionEntity->getEpisode(), $userId);
}
$episodeActionToUpdate = $this->findEpisodeActionToUpdate($episodeActionEntity, $userId);
$episodeActionEntity->setId($episodeActionToUpdate->getId());
@ -85,14 +72,6 @@ class EpisodeActionSaver
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
{
$existingGuid = $episodeActionToUpdate->getGuid();
@ -116,4 +95,24 @@ class EpisodeActionSaver
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
* @return EpisodeActionEntity|null
*/
public function findByEpisodeIdentifier(string $episodeIdentifier, string $userId) : ?EpisodeActionEntity
public function findByEpisodeUrl(string $episodeIdentifier, string $userId) : ?EpisodeActionEntity
{
$qb = $this->db->getQueryBuilder();
$qb->select('*')
->from($this->getTableName())
->where(
$qb->expr()->orX(
$qb->expr()->eq('episode', $qb->createNamedParameter($episodeIdentifier)),
$qb->expr()->eq('guid', $qb->createNamedParameter($episodeIdentifier)))
$qb->expr()->eq('episode', $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(
$qb->expr()->eq('user_id', $qb->createNamedParameter($userId))

View File

@ -28,8 +28,20 @@ class EpisodeActionRepository {
return $episodeActions;
}
public function findByEpisodeIdentifier(string $identifier, string $userId): ?EpisodeAction {
$episodeActionEntity = $this->episodeActionMapper->findByEpisodeIdentifier($identifier, $userId);
public function findByEpisodeUrl(string $episodeUrl, string $userId): ?EpisodeAction {
$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) {
return null;

View File

@ -3,7 +3,9 @@ declare(strict_types=1);
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\Core\EpisodeAction\EpisodeActionSaver;
use OCA\GPodderSync\Db\EpisodeAction\EpisodeActionEntity;
@ -12,7 +14,6 @@ use OCA\GPodderSync\Db\EpisodeAction\EpisodeActionRepository;
use OCA\GPodderSync\Db\EpisodeAction\EpisodeActionWriter;
use OCP\AppFramework\App;
use OCP\AppFramework\IAppContainer;
use OCP\IConfig;
use OCP\IRequest;
use Test\TestCase;
use tests\Helper\DatabaseTransaction;

View File

@ -22,19 +22,13 @@ class EpisodeActionGuidMigrationTest extends TestCase
private IAppContainer $container;
/**
* @var EpisodeActionWriter
*/
private $episodeActionWriter;
private EpisodeActionWriter $episodeActionWriter;
/**
* @var EpisodeActionRepository
*/
private $episodeActionRepository;
private EpisodeActionRepository $episodeActionRepository;
public function setUp(): void {
parent::setUp();
$app = new App('gpoddersync');
$app = new App('nextcloud-gpodder');
$this->container = $app->getContainer();
$this->episodeActionWriter = $this->container->get(EpisodeActionWriter::class);
$this->episodeActionRepository = $this->container->get(EpisodeActionRepository::class);
@ -85,7 +79,7 @@ class EpisodeActionGuidMigrationTest extends TestCase
self::assertSame(
$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
@ -96,12 +90,12 @@ class EpisodeActionGuidMigrationTest extends TestCase
self::assertSame(
$savedEpisodeActionEntityWithGuid->getId(),
$this->episodeActionRepository->findByEpisodeIdentifier($episodeActionEntityWithGuid->getEpisode(), self::USER_ID_0)->getId()
$this->episodeActionRepository->findByEpisodeUrl($episodeActionEntityWithGuid->getEpisode(), self::USER_ID_0)->getId()
);
self::assertSame(
$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 */
$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());
}

View File

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

View File

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