set up list management
This commit is contained in:
parent
0c42bef077
commit
73d6578857
26 changed files with 495 additions and 230 deletions
|
|
@ -8,6 +8,7 @@ use App\Models\User;
|
|||
use Exception;
|
||||
use Illuminate\Support\Facades\Http;
|
||||
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
|
||||
use function Symfony\Component\Translation\t;
|
||||
|
||||
class OmdbService implements MovieDbInterface
|
||||
{
|
||||
|
|
@ -20,14 +21,26 @@ class OmdbService implements MovieDbInterface
|
|||
$this->user = auth()->user();
|
||||
}
|
||||
|
||||
public function search(string $query, array $options = [])
|
||||
public function search(string $query, array $options = []): array|Movie
|
||||
{
|
||||
$movie = Http::get($this->url . "&t=" . $query)->json();
|
||||
$searchType = $options["type"] ?? "title";
|
||||
|
||||
$result = match ($searchType) {
|
||||
'imdb' => $this->searchByImdbId($query),
|
||||
'title' => $this->searchByTitle($query),
|
||||
default => $this->searchByTitle($query),
|
||||
};
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
public function searchByImdbId(string $imdbId)
|
||||
{
|
||||
$movie = Http::get($this->url . "&i=" . $imdbId)->json();
|
||||
if ($movie['Response'] !== 'True') {
|
||||
throw new NotFoundHttpException("Movie not found");
|
||||
}
|
||||
|
||||
logger()->debug("Movie from OMDB: " . json_encode($movie));
|
||||
try {
|
||||
$existing_movie = $this->movie->where('imdb_id', $movie['imdbID'])->firstOrFail();
|
||||
return $existing_movie;
|
||||
|
|
@ -38,4 +51,25 @@ class OmdbService implements MovieDbInterface
|
|||
);
|
||||
}
|
||||
}
|
||||
|
||||
public function searchByTitle(string $title)
|
||||
{
|
||||
$search_results = Http::get($this->url . "&s=" . $title . "&type=movie")->json();
|
||||
if ($search_results['Response'] !== 'True') {
|
||||
throw new NotFoundHttpException("Movie not found");
|
||||
}
|
||||
|
||||
$movies = [];
|
||||
foreach ($search_results['Search'] as $result) {
|
||||
$movies[] = [
|
||||
'title' => $result['Title'] ?? "",
|
||||
'year' => $result['Year'] ?? "",
|
||||
'imdb_id' => $result['imdbID'] ?? "",
|
||||
'type' => $result['Type'] ?? "",
|
||||
'poster' => $result['Poster'] ?? ""
|
||||
];
|
||||
}
|
||||
|
||||
return $movies;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue