[#2] networks

This commit is contained in:
2025-01-08 02:15:22 +02:00
parent ddbfc95102
commit 300c2574ea
5 changed files with 116 additions and 2 deletions
+16
View File
@@ -0,0 +1,16 @@
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class Network extends Model
{
protected $fillable = [
'tmdb_id',
'logo_path',
'name',
'slug',
'origin_country',
];
}
+5
View File
@@ -75,4 +75,9 @@ class Show extends Model
{
return $this->belongsToMany(Genre::class, 'show_genres', 'show_id', 'genre_id');
}
public function networks()
{
return $this->belongsToMany(Network::class, 'show_networks', 'show_id', 'network_id');
}
}
@@ -11,6 +11,7 @@ use App\Models\Image;
use App\Models\ItemToFetch;
use App\Models\ItemToFetchJobs;
use App\Models\Keyword;
use App\Models\Network;
use App\Models\Person;
use App\Models\Season;
use App\Models\Show;
@@ -80,9 +81,11 @@ class ShowProcessingService implements ProcessingInterface
$this->processGenres($item, $tmdbShow['genres']);
}
// @todo process genres
if (!empty($tmdbShow['networks'])) {
$this->processNetworks($item, $tmdbShow['networks']);
}
// @todo process created_by
// @todo process networks
// @todo process production countries
// @todo process production companies
@@ -762,4 +765,33 @@ class ShowProcessingService implements ProcessingInterface
}
}
}
private function processNetworks($item, array $networks)
{
foreach ($networks as $tmdbNetwork) {
$network = Network::where('name', $tmdbNetwork['name'])
->where('tmdb_id', $tmdbNetwork['id'])
->first();
if (!$network) {
$network = Network::create([
'name' => $tmdbNetwork['name'],
'slug' => Str::slug($tmdbNetwork['name']),
'tmdb_id' => $tmdbNetwork['id'],
'logo_path' => $tmdbNetwork['logo_path'],
'origin_country' => $tmdbNetwork['origin_country'],
]);
}
$show = Show::where('tmdb_id', $item->tmdb_id)->first();
if ($show->whereHas('networks', function ($query) use ($network) {
$query->where('tmdb_id', '=', $network->tmdb_id);
})->exists()) {
return;
} else {
$show->networks()->attach($network);
}
}
}
}