first commit
Some checks failed
Build / run (push) Has been cancelled

This commit is contained in:
maher
2025-10-29 11:42:25 +01:00
commit 703f50a09d
4595 changed files with 385164 additions and 0 deletions

View File

@@ -0,0 +1,53 @@
<?php
namespace App\Actions\Titles;
use App\Models\Person;
use Carbon\Carbon;
use Illuminate\Support\Arr;
use Illuminate\Support\Collection;
trait InsertsTmdbTitleOrPerson
{
public function insertOrRetrieve(Collection $tmdbItems): Collection
{
$tmdbItems = $tmdbItems->map(function ($value) {
unset($value['relation_data']);
unset($value['model_type']);
unset($value['id']);
return $value;
});
$select =
static::class === Person::class
? ['id', 'tmdb_id', 'name']
: ['id', 'tmdb_id', 'name', 'is_series'];
$existing = $this->withoutGlobalScope('adult')
->select($select)
->whereIn('tmdb_id', $tmdbItems->pluck('tmdb_id'))
->get()
->mapWithKeys(fn($item) => [$item['tmdb_id'] => $item]);
$new = $tmdbItems
->filter(fn($item) => !isset($existing[$item['tmdb_id']]))
->values();
if ($new->isNotEmpty()) {
$new->transform(function ($item) {
$item['created_at'] = Arr::get(
$item,
'created_at',
Carbon::now(),
);
return $item;
});
$this->insert($new->toArray());
return $this->whereIn('tmdb_id', $tmdbItems->pluck('tmdb_id'))
->select($select)
->get();
} else {
return $existing;
}
}
}