58 lines
1.3 KiB
PHP
58 lines
1.3 KiB
PHP
<?php
|
|
|
|
namespace App\Livewire;
|
|
|
|
use App\Livewire\Forms\MovieListForm;
|
|
use App\Models\Interfaces\MovieDbInterface;
|
|
use App\Models\MovieList as MovieListModel;
|
|
use Livewire\Component;
|
|
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
|
|
|
|
class MovieList extends Component
|
|
{
|
|
public int $id;
|
|
public string $query;
|
|
public MovieListModel $list;
|
|
public $movies = [];
|
|
public MovieListForm $form;
|
|
|
|
public function mount($id): void
|
|
{
|
|
$this->id = $id;
|
|
$this->getList();
|
|
}
|
|
|
|
public function getList()
|
|
{
|
|
$list = MovieListModel::with('movies')
|
|
->find($this->id);
|
|
|
|
if ($list) {
|
|
$this->list = $list;
|
|
$this->movies = $list->movies;
|
|
} else {
|
|
abort(404);
|
|
}
|
|
}
|
|
|
|
public function addMovie(MovieDbInterface $movie_db)
|
|
{
|
|
$this->resetErrorBag();
|
|
|
|
try {
|
|
$movie = $movie_db->search($this->query);
|
|
} catch (NotFoundHttpException $e) {
|
|
$this->addError('query', 'Movie not found');
|
|
return;
|
|
}
|
|
|
|
$this->list->movies()->syncWithoutDetaching($movie->id);
|
|
$this->getList();
|
|
$this->form->reset();
|
|
}
|
|
|
|
public function render()
|
|
{
|
|
return view('livewire.movie-list');
|
|
}
|
|
}
|