initial commit

This commit is contained in:
Edward Tirado Jr 2026-02-18 00:15:02 -06:00
commit d96b74e6c1
88 changed files with 15238 additions and 0 deletions

37
app/Data/MovieResult.php Normal file
View file

@ -0,0 +1,37 @@
<?php
namespace App\Data;
use App\Models\Movie;
readonly class MovieResult
{
public function __construct(
public string $imdbId,
public string $title,
public ?int $year,
public ?string $director,
public ?string $actors,
public ?string $plot,
public ?string $genre,
public ?string $mpaaRating,
public ?array $criticScores,
public ?string $poster,
) {}
public static function fromModel(Movie $movie): self
{
return new self(
imdbId: $movie->imdb_id,
title: $movie->title,
year: $movie->year,
director: $movie->director,
actors: $movie->actors,
plot: $movie->plot,
genre: $movie->genre,
mpaaRating: $movie->mpaa_rating,
criticScores: $movie->critic_scores,
poster: $movie->poster,
);
}
}

View file

@ -0,0 +1,14 @@
<?php
namespace App\Data;
readonly class MovieSearchResult
{
public function __construct(
public string $title,
public int $year,
public string $imdbId,
public string $type,
public string $poster,
) {}
}

View file

@ -0,0 +1,13 @@
<?php
namespace App\Exceptions;
use Exception;
class MovieDatabaseException extends Exception
{
public function __construct(string $message = 'Could not connect to movie database. Please try again later.')
{
parent::__construct($message);
}
}

View file

@ -0,0 +1,13 @@
<?php
namespace App\Exceptions;
use Exception;
class MovieNotFoundException extends Exception
{
public function __construct(string $message = 'Movie not found')
{
parent::__construct($message);
}
}

View file

@ -0,0 +1,8 @@
<?php
namespace App\Http\Controllers;
abstract class Controller
{
//
}

View file

@ -0,0 +1,49 @@
<?php
namespace App\Http\Controllers;
use App\Models\Invitation;
use Illuminate\Http\Request;
class InvitationController extends Controller
{
/**
* Display a listing of the resource.
*/
public function index()
{
//
}
/**
* Store a newly created resource in storage.
*/
public function store(Request $request)
{
//
}
/**
* Display the specified resource.
*/
public function show(Invitation $invitation)
{
//
}
/**
* Update the specified resource in storage.
*/
public function update(Request $request, Invitation $invitation)
{
//
}
/**
* Remove the specified resource from storage.
*/
public function destroy(Invitation $invitation)
{
//
}
}

View file

@ -0,0 +1,66 @@
<?php
namespace App\Http\Controllers;
use App\Exceptions\MovieDatabaseException;
use App\Exceptions\MovieNotFoundException;
use App\Interfaces\MovieDbInterface;
use App\Models\Movie;
use Illuminate\Http\Request;
class MovieController extends Controller
{
public function __construct(private MovieDbInterface $movieDb) {}
/**
* Display a listing of the resource.
*/
public function index()
{
//
}
/**
* Store a newly created resource in storage.
*/
public function store(Request $request)
{
//
}
/**
* Display the specified resource.
*/
public function show(Movie $movie)
{
//
}
/**
* Update the specified resource in storage.
*/
public function update(Request $request, Movie $movie)
{
//
}
/**
* Remove the specified resource from storage.
*/
public function destroy(Movie $movie)
{
//
}
/**
* @throws MovieNotFoundException
* @throws MovieDatabaseException
*/
public function search(Request $request)
{
$searchTerm = $request->input('term');
$movie = $this->movieDb->search($searchTerm);
return response()->json(['results' => $movie]);
}
}

View file

@ -0,0 +1,66 @@
<?php
namespace App\Http\Controllers;
use App\Http\Requests\CreateMovieListRequest;
use App\Models\MovieList;
use Illuminate\Database\Eloquent\ModelNotFoundException;
use Illuminate\Http\Request;
use Illuminate\Support\Str;
class MovieListController extends Controller
{
/**
* Display a listing of the resource.
*/
public function index()
{
return MovieList::all();
}
/**
* Store a newly created resource in storage.
*/
public function store(CreateMovieListRequest $request)
{
$validated = $request->validated();
$movieList = MovieList::create([
...$validated,
'owner' => 1, // auth()->id(),
'slug' => Str::slug($validated['name']),
]);
return response()->json($movieList, 201);
}
/**
* Display the specified resource.
*/
public function show(MovieList $movieList)
{
try {
return $movieList->load('movies');
} catch (ModelNotFoundException $e) {
return response()->json(['message' => 'Movie list not found'], 404);
}
}
/**
* Update the specified resource in storage.
*/
public function update(Request $request, MovieList $movieList)
{
$validated = $request->validated();
$movieList->update($validated);
return response()->json($movieList, 200);
}
/**
* Remove the specified resource from storage.
*/
public function destroy(MovieList $movieList)
{
$movieList->delete();
}
}

View file

@ -0,0 +1,49 @@
<?php
namespace App\Http\Controllers;
use App\Models\Profile;
use Illuminate\Http\Request;
class ProfileController extends Controller
{
/**
* Display a listing of the resource.
*/
public function index()
{
}
/**
* Store a newly created resource in storage.
*/
public function store(Request $request)
{
//
}
/**
* Display the specified resource.
*/
public function show(Profile $profile)
{
//
}
/**
* Update the specified resource in storage.
*/
public function update(Request $request, Profile $profile)
{
//
}
/**
* Remove the specified resource from storage.
*/
public function destroy(Profile $profile)
{
//
}
}

View file

@ -0,0 +1,29 @@
<?php
namespace App\Http\Requests;
use Illuminate\Foundation\Http\FormRequest;
class CreateMovieListRequest extends FormRequest
{
/**
* Determine if the user is authorized to make this request.
*/
public function authorize(): bool
{
return true;
}
/**
* Get the validation rules that apply to the request.
*
* @return array<string, \Illuminate\Contracts\Validation\ValidationRule|array<mixed>|string>
*/
public function rules(): array
{
return [
'name' => 'required|string|max:255',
'is_public' => 'boolean',
];
}
}

View file

@ -0,0 +1,34 @@
<?php
namespace App\Interfaces;
use App\Data\MovieResult;
use App\Data\MovieSearchResult;
use App\Exceptions\MovieDatabaseException;
use App\Exceptions\MovieNotFoundException;
use Illuminate\Support\Collection;
interface MovieDbInterface
{
/**
* Search for movies matching the given query.
*
* @param string $query The search term (e.g., a movie title)
* @return Collection<int, MovieSearchResult>
*
* @throws MovieNotFoundException If no movies match the query
* @throws MovieDatabaseException If the external movie database is unreachable or returns an error
*/
public function search(string $query): Collection;
/**
* Find a specific movie by title or external ID.
*
* @param string $query The search value (a movie title or IMDB ID)
* @param array<string, mixed> $options Search options (e.g., ['type' => 'imdb'] to search by IMDB ID)
*
* @throws MovieNotFoundException If the movie cannot be found
* @throws MovieDatabaseException If the external movie database is unreachable or returns an error
*/
public function find(string $query, array $options): MovieResult;
}

10
app/Models/Invitation.php Normal file
View file

@ -0,0 +1,10 @@
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class Invitation extends Model
{
//
}

25
app/Models/Movie.php Normal file
View file

@ -0,0 +1,25 @@
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
/**
* @method static self create(array $attributes)
*/
class Movie extends Model
{
protected $fillable = [
'title',
'imdb_id',
'year',
'director',
'actors',
'plot',
'genre',
'mpaa_rating',
'critic_scores',
'poster',
'added_by',
];
}

21
app/Models/MovieList.php Normal file
View file

@ -0,0 +1,21 @@
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
class MovieList extends Model
{
protected $fillable = [
'name',
'is_public',
'owner',
'slug',
];
public function movies(): BelongsToMany
{
return $this->belongsToMany(Movie::class);
}
}

10
app/Models/Profile.php Normal file
View file

@ -0,0 +1,10 @@
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class Profile extends Model
{
//
}

53
app/Models/User.php Normal file
View file

@ -0,0 +1,53 @@
<?php
namespace App\Models;
// use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Relations\HasOne;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
class User extends Authenticatable
{
/** @use HasFactory<\Database\Factories\UserFactory> */
use HasFactory, Notifiable;
/**
* The attributes that are mass assignable.
*
* @var list<string>
*/
protected $fillable = [
'name',
'email',
'password',
];
/**
* The attributes that should be hidden for serialization.
*
* @var list<string>
*/
protected $hidden = [
'password',
'remember_token',
];
/**
* Get the attributes that should be cast.
*
* @return array<string, string>
*/
protected function casts(): array
{
return [
'email_verified_at' => 'datetime',
'password' => 'hashed',
];
}
public function profile(): HasOne {
return $this->hasOne(Profile::class);
}
}

View file

@ -0,0 +1,26 @@
<?php
namespace App\Providers;
use App\Interfaces\MovieDbInterface;
use App\Services\OmdbMovieService;
use Illuminate\Support\ServiceProvider;
class AppServiceProvider extends ServiceProvider
{
/**
* Register any application services.
*/
public function register(): void
{
$this->app->bind(MovieDbInterface::class, OmdbMovieService::class);
}
/**
* Bootstrap any application services.
*/
public function boot(): void
{
//
}
}

View file

@ -0,0 +1,213 @@
<?php
namespace App\Services;
use App\Data\MovieResult;
use App\Data\MovieSearchResult;
use App\Exceptions\MovieDatabaseException;
use App\Exceptions\MovieNotFoundException;
use App\Interfaces\MovieDbInterface;
use App\Models\Movie;
use Illuminate\Http\Client\ConnectionException;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\Http;
class OmdbMovieService implements MovieDbInterface
{
private string $url;
private string $apiKey;
/**
* Initialize the OMDB service with API credentials
*
* @throws MovieDatabaseException If API URL or key is not configured
*/
public function __construct()
{
$apiUrl = config('services.omdb.api_url');
$apiKey = config('services.omdb.api_key');
if (! $apiUrl || ! $apiKey) {
throw new MovieDatabaseException('Could not authenticate with external movie database.');
}
$this->url = $apiUrl;
$this->apiKey = $apiKey;
}
/**
* {@inheritDoc}
*
* @throws ConnectionException If connection to OMDB fails
*/
public function find(string $query, array $options = []): MovieResult
{
$searchType = $options['type'] ?? 'title';
return match ($searchType) {
'imdb' => $this->findByImdbId($query),
'title' => $this->findByTitle($query),
default => $this->findByTitle($query),
};
}
/**
* Find a movie by IMDB ID, checking the local database first then fetching from OMDB
*
* @param string $imdbId The IMDB ID to search for
* @return MovieResult The found movie details
*
* @throws ConnectionException If connection to OMDB fails
* @throws MovieDatabaseException If OMDB API returns an error
* @throws MovieNotFoundException If the movie is not found
*/
private function findByImdbId(string $imdbId): MovieResult
{
$movie = Movie::query()->where('imdb_id', $imdbId)->first();
if ($movie) {
return MovieResult::fromModel($movie);
}
$result = $this->makeOmdbRequest(['apikey' => $this->apiKey, 'i' => $imdbId, 'type' => 'movie']);
$movieDetails = $this->mapToMovieResult($result);
Movie::create([
'title' => $movieDetails->title,
'imdb_id' => $movieDetails->imdbId,
'year' => $movieDetails->year,
'director' => $movieDetails->director,
'actors' => $movieDetails->actors,
'plot' => $movieDetails->plot,
'genre' => $movieDetails->genre,
'mpaa_rating' => $movieDetails->mpaaRating,
'critic_scores' => $movieDetails->criticScores,
'poster' => $movieDetails->poster,
'added_by' => auth()->id(),
]);
return $movieDetails;
}
/**
* Make an HTTP request to the OMDB API
*
* @param array<string, mixed> $params Query parameters for the OMDB API request
* @return array<string, mixed> The JSON response from OMDB
*
* @throws ConnectionException If connection to OMDB fails or times out
* @throws MovieDatabaseException If OMDB API returns an error response
* @throws MovieNotFoundException If OMDB indicates the movie was not found
*/
private function makeOmdbRequest(array $params): array
{
try {
$result = Http::get($this->url, $params)
->onError(fn () => throw new MovieDatabaseException('Could not fetch movie details from external database.'))
->json();
} catch (ConnectionException) {
throw new MovieDatabaseException('Could not connect to external movie database.');
}
if ($result['Response'] !== 'True') {
throw new MovieNotFoundException;
}
return $result;
}
/**
* Map OMDB API response to MovieResult data object
*
* @param array<string, mixed> $result The OMDB API response data
* @return MovieResult The mapped movie result object
*/
private function mapToMovieResult(array $result): MovieResult
{
return new MovieResult(
imdbId: $result['imdbID'],
title: $result['Title'],
year: $result['Year'],
director: $result['Director'],
actors: $result['Actors'],
plot: $result['Plot'],
genre: $result['Genre'],
mpaaRating: $result['Rated'],
criticScores: $result['Ratings'],
poster: $result['Poster'],
);
}
/**
* Find a movie by title, checking the local database first, then fetching from OMDB
*
* @param string $title The movie title to search for
* @return MovieResult The found movie details
*
* @throws ConnectionException If connection to OMDB fails
* @throws MovieDatabaseException If OMDB API returns an error
* @throws MovieNotFoundException If the movie is not found
*/
private function findByTitle(string $title): MovieResult
{
$movie = Movie::query()->where('title', $title)->first();
if ($movie) {
return MovieResult::fromModel($movie);
}
$movieResult = $this->makeOmdbRequest(['apikey' => $this->apiKey, 't' => $title]);
$movieDetails = $this->mapToMovieResult($movieResult);
Movie::create([
'title' => $movieDetails->title,
'imdb_id' => $movieDetails->imdbId,
'year' => $movieDetails->year,
'director' => $movieDetails->director,
'actors' => $movieDetails->actors,
'plot' => $movieDetails->plot,
'genre' => $movieDetails->genre,
'mpaa_rating' => $movieDetails->mpaaRating,
'critic_scores' => $movieDetails->criticScores,
'poster' => $movieDetails->poster,
'added_by' => auth()->id(),
]);
return $movieDetails;
}
/**
* {@inheritDoc}
*
* @throws ConnectionException If connection to OMDB fails
*/
public function search(string $query): Collection
{
return $this->searchByTitle($query);
}
/**
* Search for movies by title from OMDB API
*
* @param string $title The movie title to search for
* @return Collection<int, MovieSearchResult> Collection of matching movie search results
*
* @throws ConnectionException If connection to OMDB fails
* @throws MovieDatabaseException If OMDB API returns an error
* @throws MovieNotFoundException If no movies are found
*/
private function searchByTitle(string $title): Collection
{
$searchResults = $this->makeOmdbRequest(['apikey' => $this->apiKey, 's' => $title, 'type' => 'movie']);
return collect($searchResults['Search'] ?? [])
->map(fn ($movie) => new MovieSearchResult(
title: $movie['Title'],
year: $movie['Year'],
imdbId: $movie['imdbID'],
type: $movie['Type'],
poster: $movie['Poster']
));
}
}