[#7] upcoming

This commit is contained in:
2024-12-31 00:51:40 +02:00
parent a483141cae
commit 02009132a4
3 changed files with 152 additions and 0 deletions
@@ -0,0 +1,112 @@
<?php
namespace App\Console\Commands\ItemProcessing;
use App\Models\ItemToFetch;
use App\Models\ItemToFetchJobs;
use App\Models\Movie;
use App\Models\UpcomingMovie;
use App\Service\MediaProcessing\MovieProcessingService;
use App\Service\TMDBApi\MoviesApi;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\DB;
class FetchUpcomingMoviesCommand extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'processing:fetch-upcoming-movies';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Fetch Upcoming movies.';
private MoviesApi $moviesApi;
private MovieProcessingService $movieProcessingService;
public function __construct(
MoviesApi $moviesApi,
MovieProcessingService $movieProcessingService
)
{
$this->moviesApi = $moviesApi;
$this->movieProcessingService = $movieProcessingService;
parent::__construct();
}
/**
* Execute the console command.
*/
public function handle()
{
$upcomingMovies = $this->moviesApi->getUpcoming();
if (empty($upcomingMovies) || empty($upcomingMovies['results'])) {
return;
}
try {
DB::beginTransaction();
$newUpcomingIds = [];
$oldUpcomingIds = UpcomingMovie::all()->pluck('movie_id');
foreach ($upcomingMovies['results'] as $upcomingMovie) {
$movie = $this->process($upcomingMovie);
if ($movie) {
$newUpcomingIds[] = [
'movie_id' => $movie->id
];
}
}
if (!empty($newUpcomingIds)) {
UpcomingMovie::whereIn('id', $oldUpcomingIds)->delete();
UpcomingMovie::insert($newUpcomingIds);
}
DB::commit();
} catch (\Throwable $e) {
DB::rollBack();
}
}
private function process(array $upcomingMovie)
{
$movie = Movie::where('tmdb_id', $upcomingMovie['id'])->first();
if ($movie) {
return $movie;
}
$itemToFetch = ItemToFetch::where('tmdb_id', $upcomingMovie['id'])->where('type', $type)->first();
if ($itemToFetch) {
$this->movieProcessingService->process($itemToFetch, ItemToFetchJobs::JOB_DETAILS);
return Movie::where('tmdb_id', $upcomingMovie['id'])->first();
}
ItemToFetch::create([
'tmdb_id' => $upcomingMovie['id'],
'type' => ItemToFetch::TYPE_MOVIE,
]);
$jobs = ItemToFetchJobs::JOBS;
$jobs[] = "Details";
ItemToFetchJobs::create([
'tmdb_id' => $upcomingMovie['id'],
'type' => ItemToFetch::TYPE_MOVIE,
'remaining_jobs' => json_encode($jobs),
]);
return null;
}
}
+12
View File
@@ -0,0 +1,12 @@
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class UpcomingMovie extends Model
{
protected $fillable = [
'movie_id'
];
}
@@ -0,0 +1,28 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
/**
* Run the migrations.
*/
public function up(): void
{
Schema::create('upcoming_movies', function (Blueprint $table) {
$table->id();
$table->unsignedBigInteger('movie_id');
$table->timestamps();
});
}
/**
* Reverse the migrations.
*/
public function down(): void
{
Schema::dropIfExists('upcoming_movies');
}
};