diff --git a/assets/controllers/search_bar_controller.js b/assets/controllers/search_bar_controller.js index 0d919b3..44774bb 100644 --- a/assets/controllers/search_bar_controller.js +++ b/assets/controllers/search_bar_controller.js @@ -12,6 +12,11 @@ export default class extends Controller { const autocompleteController = this.application.getControllerForElementAndIdentifier(this.element, 'symfony--ux-autocomplete--autocomplete') window.location.href = `/search?term=${autocompleteController.tomSelect.lastValue}` } + document.querySelector("#search-button").addEventListener('click', (event) => { + event.preventDefault(); + const autocompleteController = this.application.getControllerForElementAndIdentifier(this.element, 'symfony--ux-autocomplete--autocomplete') + window.location.href = `/search?term=${autocompleteController.tomSelect.lastQuery}` + }); this.element.addEventListener('autocomplete:pre-connect', this._onPreConnect); this.element.addEventListener('autocomplete:connect', this._onConnect); } diff --git a/src/Search/Action/Handler/SearchHandler.php b/src/Search/Action/Handler/SearchHandler.php index 2949923..adf1c15 100644 --- a/src/Search/Action/Handler/SearchHandler.php +++ b/src/Search/Action/Handler/SearchHandler.php @@ -2,6 +2,8 @@ namespace App\Search\Action\Handler; +use App\Base\Util\ImdbMatcher; +use App\Search\Action\Result\RedirectToMediaResult; use App\Search\Action\Result\SearchResult; use App\Tmdb\Tmdb; use OneToMany\RichBundle\Contract\CommandInterface; @@ -17,6 +19,13 @@ class SearchHandler implements HandlerInterface public function handle(CommandInterface $command): ResultInterface { + if (ImdbMatcher::isMatch($command->term)) { + $result = $this->tmdb->findByImdbId($command->term); + return new RedirectToMediaResult( + imdbId: $result->imdbId, + mediaType: $result->mediaType, + ); + } return new SearchResult( term: $command->term, results: $this->tmdb->search($command->term) diff --git a/src/Search/Action/Result/RedirectToMediaResult.php b/src/Search/Action/Result/RedirectToMediaResult.php new file mode 100644 index 0000000..558a237 --- /dev/null +++ b/src/Search/Action/Result/RedirectToMediaResult.php @@ -0,0 +1,13 @@ +searchHandler->handle($searchInput->toCommand()); + if ($results instanceof RedirectToMediaResult) { + return $this->redirectToRoute('app_search_result', [ + 'mediaType' => $results->mediaType, + 'imdbId' => $results->imdbId, + ]); + } + return $this->render('search/results.html.twig', [ 'results' => $results, ]); diff --git a/src/Tmdb/Framework/Controller/ApiController.php b/src/Tmdb/Framework/Controller/ApiController.php index 3c86b68..524bb64 100644 --- a/src/Tmdb/Framework/Controller/ApiController.php +++ b/src/Tmdb/Framework/Controller/ApiController.php @@ -2,6 +2,7 @@ namespace App\Tmdb\Framework\Controller; +use App\Base\Util\ImdbMatcher; use App\Tmdb\Tmdb; use App\Tmdb\TmdbResult; use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; @@ -17,17 +18,28 @@ class ApiController extends AbstractController $results = []; $term = $request->query->get('query') ?? null; + $term = trim($term); if (null !== $term) { - $tmdbResults = $tmdb->search($term); - - foreach ($tmdbResults as $tmdbResult) { - /** @var TmdbResult $tmdbResult */ - $results[] = [ - 'data' => $tmdbResult, - 'text' => $tmdbResult->title, - 'value' => "$tmdbResult->mediaType|$tmdbResult->imdbId", + if (ImdbMatcher::isMatch($term)) { + $tmdbResult = $tmdb->findByImdbId($term); + $results = [ + [ + 'data' => $tmdbResult, + 'text' => $tmdbResult->title, + 'value' => "$tmdbResult->mediaType|$tmdbResult->imdbId", + ] ]; + } else { + $tmdbResults = $tmdb->search($term); + foreach ($tmdbResults as $tmdbResult) { + /** @var TmdbResult $tmdbResult */ + $results[] = [ + 'data' => $tmdbResult, + 'text' => $tmdbResult->title, + 'value' => "$tmdbResult->mediaType|$tmdbResult->imdbId", + ]; + } } } diff --git a/src/Tmdb/Tmdb.php b/src/Tmdb/Tmdb.php index f36dc01..b14eba3 100644 --- a/src/Tmdb/Tmdb.php +++ b/src/Tmdb/Tmdb.php @@ -185,6 +185,28 @@ class Tmdb throw new \Exception("No results found for $id"); } + public function findByImdbId(string $imdbId) + { + $finder = new Find($this->client); + $result = $finder->findBy($imdbId, ['external_source' => 'imdb_id']); + + if (count($result['movie_results']) > 0) { + $result = $result['movie_results'][0]; + $mediaType = MediaType::Movie->value; + } elseif (count($result['tv_results']) > 0) { + $result = $result['tv_results'][0]; + $mediaType = MediaType::TvShow->value; + } elseif (count($result['tv_episode_results']) > 0) { + $result = $result['tv_episode_results'][0]; + $mediaType = MediaType::TvShow->value; + } + + $result['media_type'] = $mediaType; + $result = $this->mediaDetails($imdbId, $result['media_type']); + + return $result; + } + public function movieDetails(string $id) { $client = new MovieRepository($this->client); diff --git a/templates/components/SearchBar.html.twig b/templates/components/SearchBar.html.twig index bcd2695..325e287 100644 --- a/templates/components/SearchBar.html.twig +++ b/templates/components/SearchBar.html.twig @@ -20,6 +20,7 @@ >