chore(api/*): cleans up unnecessary network calls, fixes tag + genre assignments

This commit is contained in:
Cory Dransfeldt 2025-06-06 12:01:42 -07:00
parent 8890f7dcfc
commit 5a133a7c23
No known key found for this signature in database
5 changed files with 33 additions and 44 deletions

View file

@ -92,16 +92,20 @@ class WatchingImportHandler extends ApiHandler
$table = $mediaType === "movie" ? "movies" : "shows";
try {
$response = $this->makeRequest("POST", $table, ["json" => $payload]);
$response = $this->makeRequest("POST", $table, [
"json" => $payload,
"headers" => ["Prefer" => "return=representation"]
]);
} catch (\Exception $e) {
$response = $this->fetchFromApi($table, "tmdb_id=eq.{$id}")[0] ?? [];
}
if (!empty($response["id"])) {
$mediaId = $response["id"];
$existingTagMap = $this->getTagIds($tags);
$updatedTagMap = $this->insertMissingTags($tags, $existingTagMap);
$this->associateTagsWithMedia($mediaType, $mediaId, array_values($updatedTagMap));
$record = $response[0] ?? [];
if (!empty($record["id"])) {
$mediaId = $record["id"];
$tagIds = $this->getTagIds($tags);
if (!empty($tagIds)) $this->associateTagsWithMedia($mediaType, $mediaId, array_values($tagIds));
}
}
@ -119,23 +123,6 @@ class WatchingImportHandler extends ApiHandler
return $map;
}
private function insertMissingTags(array $tags, array $existingMap): array
{
$newTags = array_diff($tags, array_keys($existingMap));
foreach ($newTags as $tag) {
try {
$created = $this->makeRequest("POST", "tags", ["json" => ["name" => $tag]]);
if (!empty($created["id"])) $existingMap[$tag] = $created["id"];
} catch (\Exception $e) {
$fallback = $this->fetchFromApi("tags", "name=eq." . urlencode($tag));
if (!empty($fallback[0]["id"])) $existingMap[$tag] = $fallback[0]["id"];
}
}
return $existingMap;
}
private function associateTagsWithMedia(string $mediaType, int $mediaId, array $tagIds): void
{
$junction = $mediaType === "movie" ? "movies_tags" : "shows_tags";