Remove played_at column if isWatched flag is set to false before updating database.

This commit is contained in:
Abdulmhsen B. A. A
2022-05-25 07:02:53 +03:00
parent eef22ab926
commit 5ec38addac

View File

@@ -38,7 +38,23 @@ final class PDOAdapter implements StorageInterface
public function insert(iFace $entity): iFace
{
try {
if (null !== ($entity->id ?? null)) {
throw new StorageException(
sprintf('Unable to insert item that has primary key. \'%s\'.', $entity->id), 21
);
}
$data = $entity->getAll();
unset($data[iFace::COLUMN_ID]);
if (false === $entity->isWatched()) {
foreach ($data[iFace::COLUMN_META_DATA] ?? [] as $via => $metadata) {
if (null === ($metadata[iFace::COLUMN_META_DATA_PLAYED_AT] ?? null)) {
continue;
}
unset($data[iFace::COLUMN_META_DATA][$via][iFace::COLUMN_META_DATA_PLAYED_AT]);
}
}
foreach (iFace::ENTITY_ARRAY_KEYS as $key) {
if (null !== ($data[$key] ?? null) && is_array($data[$key])) {
@@ -47,12 +63,6 @@ final class PDOAdapter implements StorageInterface
}
}
if (null !== $data[iFace::COLUMN_ID]) {
throw new StorageException(sprintf('Trying to insert already saved entity #%s', $data['id']), 21);
}
unset($data[iFace::COLUMN_ID]);
if (null === ($this->stmt['insert'] ?? null)) {
$this->stmt['insert'] = $this->pdo->prepare(
$this->pdoInsert('state', iFace::ENTITY_KEYS)
@@ -151,8 +161,21 @@ final class PDOAdapter implements StorageInterface
public function update(iFace $entity): iFace
{
try {
if (null === ($entity->id ?? null)) {
throw new StorageException('Unable to update item without primary key.', 51);
}
$data = $entity->getAll();
if (false === $entity->isWatched()) {
foreach ($data[iFace::COLUMN_META_DATA] ?? [] as $via => $metadata) {
if (null === ($metadata[iFace::COLUMN_META_DATA_PLAYED_AT] ?? null)) {
continue;
}
unset($data[iFace::COLUMN_META_DATA][$via][iFace::COLUMN_META_DATA_PLAYED_AT]);
}
}
foreach (iFace::ENTITY_ARRAY_KEYS as $key) {
if (null !== ($data[$key] ?? null) && is_array($data[$key])) {
ksort($data[$key]);
@@ -160,10 +183,6 @@ final class PDOAdapter implements StorageInterface
}
}
if (null === ($data[iFace::COLUMN_ID] ?? null)) {
throw new StorageException('Trying to update unsaved item.', 51);
}
if (null === ($this->stmt['update'] ?? null)) {
$this->stmt['update'] = $this->pdo->prepare(
$this->pdoUpdate('state', iFace::ENTITY_KEYS)
@@ -393,7 +412,12 @@ final class PDOAdapter implements StorageInterface
*/
private function findByRGuid(iFace $entity): iFace|null
{
$cond = $where = [];
$where = [];
$cond = [
'type' => iFace::TYPE_EPISODE,
'season' => $entity->season,
'episode' => $entity->episode,
];
foreach ($entity->parent as $key => $val) {
if (null === ($val ?? null)) {
@@ -423,10 +447,6 @@ final class PDOAdapter implements StorageInterface
LIMIT 1
";
$cond['season'] = $entity->season;
$cond['episode'] = $entity->episode;
$cond['type'] = iFace::TYPE_EPISODE;
$stmt = $this->pdo->prepare($sql);
if (false === $stmt->execute($cond)) {
@@ -468,7 +488,7 @@ final class PDOAdapter implements StorageInterface
FROM
state
WHERE
type = :type
" . iFace::COLUMN_TYPE . " = :type
AND
JSON_EXTRACT(" . iFace::COLUMN_META_DATA . ", '{$key}') = :id
LIMIT 1