Files
cv4/app/Jobs/ComicUpsert.php
2025-01-18 19:05:23 -05:00

72 lines
2.0 KiB
PHP

<?php
namespace App\Jobs;
use App\Models\Author;
use App\Models\Comic;
use App\Models\Image;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Queue\Queueable;
use Illuminate\Support\Facades\Log;
use romanzipp\QueueMonitor\Traits\IsMonitored;
class ComicUpsert implements ShouldQueue
{
use IsMonitored, Queueable;
/**
* Create a new job instance.
*/
public function __construct(
public array $comic,
) {}
/**
* Execute the job.
*/
public function handle(): void
{
$this->queueProgress(0);
Log::info("JOB ComicUpsert START");
$comicsUpsertArray = [];
$authorsUpsertArray = [];
foreach ($this->comic['list'] as $comic) {
$comicsUpsertArray[] = [
'pathword' => $comic['path_word'],
'uuid' => '',
'name' => $comic['name'],
'alias' => '{}',
'description' => '',
'cover' => $comic['cover'],
'upstream_updated_at' => $comic['datetime_updated'] ?? null,
];
foreach ($comic['author'] as $author) {
$authorsUpsertArray[] = [
'name' => $author['name']
];
}
}
// Do an upsert for comics
Comic::upsert($comicsUpsertArray, uniqueBy: 'pathword', update: ['upstream_updated_at']);
Author::upsert($authorsUpsertArray, uniqueBy: 'name');
// Had to do a second pass to insert the relationships
foreach ($this->comic['list'] as $comic) {
// Get the comic id
$comicObj = Comic::where('pathword', $comic['path_word'])->first();
foreach ($comic['author'] as $author) {
$authorObj = Author::where('name', $author['name'])->first();
$comicObj->authors()->sync($authorObj);
}
}
Log::info('JOB ComicUpsert END');
$this->queueProgress(100);
}
}