update existing episode actions with guid

This commit is contained in:
thrillfall 2021-08-25 19:33:00 +02:00
parent 850dfd5eb4
commit 352802ca69
6 changed files with 79 additions and 13 deletions

View File

@ -42,7 +42,7 @@ class EpisodeActionController extends Controller {
* @return Response * @return Response
*/ */
public function create($data) { public function create($data) {
return $this->episodeActionSaver->saveEpisodeAction($data, $this->userId); return $this->episodeActionSaver->saveEpisodeActions($data, $this->userId);
} }
/** /**

View File

@ -33,7 +33,7 @@ class EpisodeActionSaver
* *
* @return EpisodeActionEntity[] * @return EpisodeActionEntity[]
*/ */
public function saveEpisodeAction(string $data, string $userId): array public function saveEpisodeActions(string $data, string $userId): array
{ {
$episodeActionEntities = []; $episodeActionEntities = [];
@ -77,10 +77,19 @@ class EpisodeActionSaver
string $userId string $userId
): EpisodeActionEntity ): EpisodeActionEntity
{ {
$idEpisodeActionEntityToUpdate = $this->episodeActionRepository->findByEpisodeIdentifier( $identifier = $episodeActionEntity->getGuid() ?? $episodeActionEntity->getEpisode();
$episodeActionEntity->getEpisode(), $episodeActionEntityToUpdate = $this->episodeActionRepository->findByEpisodeIdentifier(
$identifier,
$userId $userId
)->getId(); );
if ($episodeActionEntityToUpdate === null && $episodeActionEntity->getGuid() !== null) {
$episodeActionEntityToUpdate = $this->episodeActionRepository->findByEpisodeIdentifier(
$episodeActionEntity->getEpisode(),
$userId
);
}
$idEpisodeActionEntityToUpdate = $episodeActionEntityToUpdate->getId();
$episodeActionEntity->setId($idEpisodeActionEntityToUpdate); $episodeActionEntity->setId($idEpisodeActionEntityToUpdate);
return $this->episodeActionWriter->update($episodeActionEntity); return $this->episodeActionWriter->update($episodeActionEntity);
@ -97,6 +106,7 @@ class EpisodeActionSaver
$episodeActionEntity = new EpisodeActionEntity(); $episodeActionEntity = new EpisodeActionEntity();
$episodeActionEntity->setPodcast($episodeAction->getPodcast()); $episodeActionEntity->setPodcast($episodeAction->getPodcast());
$episodeActionEntity->setEpisode($episodeAction->getEpisode()); $episodeActionEntity->setEpisode($episodeAction->getEpisode());
$episodeActionEntity->setGuid($episodeAction->getGuid());
$episodeActionEntity->setAction($episodeAction->getAction()); $episodeActionEntity->setAction($episodeAction->getAction());
$episodeActionEntity->setPosition($episodeAction->getPosition()); $episodeActionEntity->setPosition($episodeAction->getPosition());
$episodeActionEntity->setStarted($episodeAction->getStarted()); $episodeActionEntity->setStarted($episodeAction->getStarted());

View File

@ -32,7 +32,7 @@ class EpisodeActionMapper extends \OCP\AppFramework\Db\QBMapper
return $this->findEntities($qb); return $this->findEntities($qb);
} }
public function findByEpisodeIdentifier(string $episodeIdentifier, string $userId) : EpisodeActionEntity public function findByEpisodeIdentifier(string $episodeIdentifier, string $userId) : ?EpisodeActionEntity
{ {
$qb = $this->db->getQueryBuilder(); $qb = $this->db->getQueryBuilder();
@ -55,5 +55,7 @@ class EpisodeActionMapper extends \OCP\AppFramework\Db\QBMapper
} catch (DoesNotExistException $e) { } catch (DoesNotExistException $e) {
} catch (MultipleObjectsReturnedException $e) { } catch (MultipleObjectsReturnedException $e) {
} }
return null;
} }
} }

View File

@ -17,7 +17,7 @@ class EpisodeActionRepository {
return $this->episodeActionMapper->findAll($sinceTimestamp, $userId); return $this->episodeActionMapper->findAll($sinceTimestamp, $userId);
} }
public function findByEpisodeIdentifier(string $identifier, string $userId): EpisodeActionEntity { public function findByEpisodeIdentifier(string $identifier, string $userId): ?EpisodeActionEntity {
return $this->episodeActionMapper->findByEpisodeIdentifier($identifier, $userId); return $this->episodeActionMapper->findByEpisodeIdentifier($identifier, $userId);
} }

View File

@ -5,6 +5,7 @@ namespace tests\Integration;
use Doctrine\DBAL\Exception\UniqueConstraintViolationException; use Doctrine\DBAL\Exception\UniqueConstraintViolationException;
use OCA\GPodderSync\Core\EpisodeAction\EpisodeActionReader; use OCA\GPodderSync\Core\EpisodeAction\EpisodeActionReader;
use OCA\GPodderSync\Core\EpisodeAction\EpisodeActionSaver;
use OCA\GPodderSync\Db\EpisodeAction\EpisodeActionEntity; use OCA\GPodderSync\Db\EpisodeAction\EpisodeActionEntity;
use OCA\GPodderSync\Db\EpisodeAction\EpisodeActionRepository; use OCA\GPodderSync\Db\EpisodeAction\EpisodeActionRepository;
use OCA\GPodderSync\Db\EpisodeAction\EpisodeActionWriter; use OCA\GPodderSync\Db\EpisodeAction\EpisodeActionWriter;
@ -28,11 +29,17 @@ class EpisodeActionGuidMigrationTest extends TestCase
*/ */
private $episodeActionWriter; private $episodeActionWriter;
/**
* @var EpisodeActionRepository
*/
private $episodeActionRepository;
public function setUp(): void { public function setUp(): void {
parent::setUp(); parent::setUp();
$app = new App('gpoddersync'); $app = new App('gpoddersync');
$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);
} }
/** /**
@ -63,6 +70,10 @@ class EpisodeActionGuidMigrationTest extends TestCase
} }
/**
*
* @group findme
*/
public function testFindEpisodeActionByEpisodeUrlAndThenGuid() public function testFindEpisodeActionByEpisodeUrlAndThenGuid()
{ {
$episodeActionEntity = new EpisodeActionEntity(); $episodeActionEntity = new EpisodeActionEntity();
@ -76,12 +87,9 @@ class EpisodeActionGuidMigrationTest extends TestCase
$episodeActionEntity->setUserId(self::USER_ID_0); $episodeActionEntity->setUserId(self::USER_ID_0);
$savedEpisodeActionEntity = $this->episodeActionWriter->save($episodeActionEntity); $savedEpisodeActionEntity = $this->episodeActionWriter->save($episodeActionEntity);
/** @var EpisodeActionRepository $episodeActionRepository */
$episodeActionRepository = $this->container->get(EpisodeActionRepository::class);
self::assertSame( self::assertSame(
$savedEpisodeActionEntity->getId(), $savedEpisodeActionEntity->getId(),
$episodeActionRepository->findByEpisodeIdentifier($episodeActionEntity->getEpisode(), self::USER_ID_0)->getId() $this->episodeActionRepository->findByEpisodeIdentifier($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
@ -92,12 +100,12 @@ class EpisodeActionGuidMigrationTest extends TestCase
self::assertSame( self::assertSame(
$savedEpisodeActionEntityWithGuid->getId(), $savedEpisodeActionEntityWithGuid->getId(),
$episodeActionRepository->findByEpisodeIdentifier($episodeActionEntityWithGuid->getEpisode(), self::USER_ID_0)->getId() $this->episodeActionRepository->findByEpisodeIdentifier($episodeActionEntityWithGuid->getEpisode(), self::USER_ID_0)->getId()
); );
self::assertSame( self::assertSame(
$savedEpisodeActionEntityWithGuid->getId(), $savedEpisodeActionEntityWithGuid->getId(),
$episodeActionRepository->findByEpisodeIdentifier($episodeActionEntityWithGuid->getGuid(), self::USER_ID_0)->getId() $this->episodeActionRepository->findByEpisodeIdentifier($episodeActionEntityWithGuid->getGuid(), self::USER_ID_0)->getId()
); );
} }

View File

@ -0,0 +1,46 @@
<?php
declare(strict_types=1);
namespace tests\Integration;
use OCA\GPodderSync\Core\EpisodeAction\EpisodeActionSaver;
use OCP\AppFramework\App;
use Test\TestCase;
/**
* @group DB
*/
class EpisodeActionSaverGuidMigrationTest extends TestCase
{
private const USER_ID_0 = "user0@127.0.0.1";
private \OCP\AppFramework\IAppContainer $container;
public function setUp(): void {
parent::setUp();
$app = new App('gpoddersync');
$this->container = $app->getContainer();
}
public function testCreateEpisodeActionWithoutGuidThenCreateAgainWithGuid() : void
{
/** @var EpisodeActionSaver $episodeActionSaver */
$episodeActionSaver = $this->container->get(EpisodeActionSaver::class);
$episodeUrl = uniqid("https://dts.podtrac.com/redirect.mp3/chrt.fm/track");
$guid = uniqid("gid://art19-episode-locator/V0/Ktd");
$savedEpisodeActionEntityWithoutGuid = $episodeActionSaver->saveEpisodeActions(
"[EpisodeAction{podcast='https://rss.art19.com/dr-death-s3-miracle-man', episode='{$episodeUrl}', action=PLAY, timestamp=Mon Aug 23 01:58:56 GMT+02:00 2021, started=47, position=54, total=2252}]",
self::USER_ID_0
)[0];
$savedEpisodeActionEntityWithGuid = $episodeActionSaver->saveEpisodeActions(
"[EpisodeAction{podcast='https://rss.art19.com/dr-death-s3-miracle-man', episode='{$episodeUrl}', guid='{$guid}', action=PLAY, timestamp=Mon Aug 23 01:58:56 GMT+02:00 2021, started=47, position=54, total=2252}]",
self::USER_ID_0
)[0];
self::assertSame($savedEpisodeActionEntityWithoutGuid->getId(), $savedEpisodeActionEntityWithGuid->getId());
}
}