55 lines
1.4 KiB
PHP
55 lines
1.4 KiB
PHP
|
|
<?php
|
||
|
|
|
||
|
|
namespace App\Livewire;
|
||
|
|
|
||
|
|
use App\Models\Interfaces\MovieDbInterface;
|
||
|
|
use App\Models\Movie;
|
||
|
|
use App\Models\MovieList as MovieListModel;
|
||
|
|
use App\Services\OmdbService;
|
||
|
|
use Livewire\Component;
|
||
|
|
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
|
||
|
|
|
||
|
|
class SearchPanel extends Component
|
||
|
|
{
|
||
|
|
public $showSearch = false;
|
||
|
|
public $query = '';
|
||
|
|
public $results = [];
|
||
|
|
public int $listId;
|
||
|
|
|
||
|
|
protected $listeners = ['openSearch' => 'openSearchPanel'];
|
||
|
|
|
||
|
|
public function openSearchPanel(): void
|
||
|
|
{
|
||
|
|
$this->showSearch = true;
|
||
|
|
}
|
||
|
|
|
||
|
|
public function findMovies(MovieDbInterface $movie_db): void
|
||
|
|
{
|
||
|
|
try {
|
||
|
|
//$movie = $movie_db->search($this->query, ["type" => "imdb"]);
|
||
|
|
$this->results = $movie_db->search($this->query, ["type" => "title"]);
|
||
|
|
} catch (NotFoundHttpException $e) {
|
||
|
|
$this->addError('query', 'Movie not found');
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
public function addMovie(MovieDbInterface $movieDb, string $imdbId): void
|
||
|
|
{
|
||
|
|
$this->resetErrorBag();
|
||
|
|
$movie = $movieDb->searchByImdbId($imdbId);
|
||
|
|
|
||
|
|
$list = MovieListModel::find($this->listId);
|
||
|
|
$list->movies()->syncWithoutDetaching($movie->id);
|
||
|
|
|
||
|
|
$this->dispatch('movie-added');
|
||
|
|
$this->showSearch = false;
|
||
|
|
$this->query = '';
|
||
|
|
$this->results = [];
|
||
|
|
}
|
||
|
|
|
||
|
|
public function render()
|
||
|
|
{
|
||
|
|
return view('livewire.search-panel');
|
||
|
|
}
|
||
|
|
}
|