added support for adding/removing movies from a movie list
This commit is contained in:
parent
8970e82780
commit
95712abdb6
12 changed files with 138 additions and 262 deletions
|
|
@ -38,7 +38,6 @@ class AuthController extends Controller
|
|||
Auth::guard('web')->logout();
|
||||
|
||||
$request->session()->invalidate();
|
||||
$request->session()->regenerateToken();
|
||||
|
||||
return response()->json(['message' => 'Logged out.']);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -2,7 +2,9 @@
|
|||
|
||||
namespace App\Http\Controllers;
|
||||
|
||||
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
|
||||
|
||||
abstract class Controller
|
||||
{
|
||||
//
|
||||
use AuthorizesRequests;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -56,11 +56,10 @@ class MovieController extends Controller
|
|||
* @throws MovieNotFoundException
|
||||
* @throws MovieDatabaseException
|
||||
*/
|
||||
public function search(Request $request)
|
||||
public function search(MovieDbInterface $movieDb, Request $request, string $query)
|
||||
{
|
||||
$searchTerm = $request->input('term');
|
||||
$movie = $this->movieDb->search($searchTerm);
|
||||
$movies = $movieDb->search($query, $request->input('options', []));
|
||||
|
||||
return response()->json(['results' => $movie]);
|
||||
return response()->json(['results' => $movies]);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -3,6 +3,9 @@
|
|||
namespace App\Http\Controllers;
|
||||
|
||||
use App\Http\Requests\CreateMovieListRequest;
|
||||
use App\Http\Requests\UpdateMovieListRequest;
|
||||
use App\Interfaces\MovieDbInterface;
|
||||
use App\Models\Movie;
|
||||
use App\Models\MovieList;
|
||||
use Illuminate\Database\Eloquent\ModelNotFoundException;
|
||||
use Illuminate\Http\Request;
|
||||
|
|
@ -23,6 +26,8 @@ class MovieListController extends Controller
|
|||
*/
|
||||
public function store(CreateMovieListRequest $request)
|
||||
{
|
||||
$this->authorize('create', MovieList::class);
|
||||
|
||||
$validated = $request->validated();
|
||||
$movieList = MovieList::create([
|
||||
...$validated,
|
||||
|
|
@ -38,6 +43,7 @@ class MovieListController extends Controller
|
|||
*/
|
||||
public function show(MovieList $movieList)
|
||||
{
|
||||
$this->authorize('view', $movieList);
|
||||
try {
|
||||
return $movieList->load('movies');
|
||||
} catch (ModelNotFoundException $e) {
|
||||
|
|
@ -48,7 +54,7 @@ class MovieListController extends Controller
|
|||
/**
|
||||
* Update the specified resource in storage.
|
||||
*/
|
||||
public function update(Request $request, MovieList $movieList)
|
||||
public function update(UpdateMovieListRequest $request, MovieList $movieList)
|
||||
{
|
||||
$validated = $request->validated();
|
||||
$movieList->update($validated);
|
||||
|
|
@ -61,6 +67,29 @@ class MovieListController extends Controller
|
|||
*/
|
||||
public function destroy(MovieList $movieList)
|
||||
{
|
||||
$this->authorize('delete', $movieList);
|
||||
$movieList->delete();
|
||||
}
|
||||
|
||||
public function addMovie(MovieDbInterface $movieDb, Request $request, MovieList $movieList)
|
||||
{
|
||||
$this->authorize('update', $movieList);
|
||||
$movieResult = $movieDb->find($request->input('movie')['imdbId'], ['type' => 'imdb']);
|
||||
$movie = Movie::where('imdb_id', $movieResult->imdbId)->first();
|
||||
|
||||
$movieList->movies()->attach($movie);
|
||||
$movieList->load('movies');
|
||||
|
||||
return response()->json($movieList);
|
||||
}
|
||||
|
||||
public function removeMovie(MovieDbInterface $movieDb, Request $request, MovieList $movieList, Movie $movie)
|
||||
{
|
||||
$this->authorize('update', $movieList);
|
||||
|
||||
$movieList->movies()->detach($movie);
|
||||
$movieList->load('movies');
|
||||
|
||||
return response()->json($movieList);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
31
app/Http/Requests/UpdateMovieListRequest.php
Normal file
31
app/Http/Requests/UpdateMovieListRequest.php
Normal file
|
|
@ -0,0 +1,31 @@
|
|||
<?php
|
||||
|
||||
namespace App\Http\Requests;
|
||||
|
||||
use Illuminate\Foundation\Http\FormRequest;
|
||||
|
||||
class UpdateMovieListRequest extends FormRequest
|
||||
{
|
||||
/**
|
||||
* Determine if the user is authorized to make this request.
|
||||
*/
|
||||
public function authorize(): bool
|
||||
{
|
||||
return $this->user()->can('update', $this->route('movieList'));
|
||||
}
|
||||
|
||||
/**
|
||||
* 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',
|
||||
'movies' => 'array',
|
||||
'slug' => 'string',
|
||||
];
|
||||
}
|
||||
}
|
||||
|
|
@ -19,7 +19,7 @@ interface MovieDbInterface
|
|||
* @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;
|
||||
public function search(string $query, array $options): Collection;
|
||||
|
||||
/**
|
||||
* Find a specific movie by title or external ID.
|
||||
|
|
|
|||
|
|
@ -22,4 +22,11 @@ class Movie extends Model
|
|||
'poster',
|
||||
'added_by',
|
||||
];
|
||||
|
||||
protected function casts(): array
|
||||
{
|
||||
return [
|
||||
'critic_scores' => 'array',
|
||||
];
|
||||
}
|
||||
}
|
||||
|
|
|
|||
49
app/Policies/MovieListPolicy.php
Normal file
49
app/Policies/MovieListPolicy.php
Normal file
|
|
@ -0,0 +1,49 @@
|
|||
<?php
|
||||
|
||||
namespace App\Policies;
|
||||
|
||||
use App\Models\MovieList;
|
||||
use App\Models\User;
|
||||
|
||||
class MovieListPolicy
|
||||
{
|
||||
/**
|
||||
* Create a new policy instance.
|
||||
*/
|
||||
public function __construct()
|
||||
{
|
||||
//
|
||||
}
|
||||
|
||||
public function create(User $user): bool
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
public function view(User $user, MovieList $movieList): bool
|
||||
{
|
||||
if ($movieList->owner === $user->getKey() || $movieList->isPublic) {
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
public function update(User $user, MovieList $movieList): bool
|
||||
{
|
||||
if ($movieList->owner === $user->getKey()) {
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
public function delete(User $user, MovieList $movieList): bool
|
||||
{
|
||||
if ($movieList->owner === $user->getKey()) {
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
|
@ -82,7 +82,7 @@ class OmdbMovieService implements MovieDbInterface
|
|||
'plot' => $movieDetails->plot,
|
||||
'genre' => $movieDetails->genre,
|
||||
'mpaa_rating' => $movieDetails->mpaaRating,
|
||||
'critic_scores' => $movieDetails->criticScores,
|
||||
'critic_scores' => json_encode($movieDetails->criticScores),
|
||||
'poster' => $movieDetails->poster,
|
||||
'added_by' => auth()->id(),
|
||||
]);
|
||||
|
|
@ -169,7 +169,7 @@ class OmdbMovieService implements MovieDbInterface
|
|||
'plot' => $movieDetails->plot,
|
||||
'genre' => $movieDetails->genre,
|
||||
'mpaa_rating' => $movieDetails->mpaaRating,
|
||||
'critic_scores' => $movieDetails->criticScores,
|
||||
'critic_scores' => json_encode($movieDetails->criticScores),
|
||||
'poster' => $movieDetails->poster,
|
||||
'added_by' => auth()->id(),
|
||||
]);
|
||||
|
|
@ -182,9 +182,9 @@ class OmdbMovieService implements MovieDbInterface
|
|||
*
|
||||
* @throws ConnectionException If connection to OMDB fails
|
||||
*/
|
||||
public function search(string $query): Collection
|
||||
public function search(string $query, array $options = []): Collection
|
||||
{
|
||||
return $this->searchByTitle($query);
|
||||
return $this->searchByTitle($query, $options);
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -197,9 +197,13 @@ class OmdbMovieService implements MovieDbInterface
|
|||
* @throws MovieDatabaseException If OMDB API returns an error
|
||||
* @throws MovieNotFoundException If no movies are found
|
||||
*/
|
||||
private function searchByTitle(string $title): Collection
|
||||
private function searchByTitle(string $title, array $options): Collection
|
||||
{
|
||||
$searchResults = $this->makeOmdbRequest(['apikey' => $this->apiKey, 's' => $title, 'type' => 'movie']);
|
||||
$searchResults = $this->makeOmdbRequest([
|
||||
'apikey' => $this->apiKey, 's' => $title,
|
||||
'type' => 'movie',
|
||||
...$options,
|
||||
]);
|
||||
|
||||
return collect($searchResults['Search'] ?? [])
|
||||
->map(fn ($movie) => new MovieSearchResult(
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue