Skip to content

Commit

Permalink
feat: create review modal (#384)
Browse files Browse the repository at this point in the history
  • Loading branch information
ell-ska authored Feb 13, 2025
2 parents 504b44f + 6175f73 commit e5a4ebb
Show file tree
Hide file tree
Showing 5 changed files with 96 additions and 14 deletions.
6 changes: 4 additions & 2 deletions app/Http/Controllers/MovieController.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,11 @@ public function store(Request $request)
/**
* Display the specified resource.
*/
public function show(Movie $movie)
public function show($id)
{
return view('movie');
$movie = Movie::findOrFail($id);

return view('movie', ['movie' => $movie]);
}

/**
Expand Down
36 changes: 26 additions & 10 deletions app/Http/Controllers/ReviewController.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use App\Models\Review;
use App\Models\User;
use Exception;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;

Expand All @@ -25,20 +26,35 @@ public function index($username)
]);
}

/**
* Show the form for creating a new resource.
*/
public function create()
{
//
}

/**
* Store a newly created resource in storage.
*/
public function store(Request $request)
public function store(Request $request, $id)
{
//
$request->validateWithBag('createReviewValidation', [
'rating' => ['required', 'integer', 'min:1', 'max:10'],
'content' => ['nullable', 'string', 'max:800'],
]);

try {
$user = Auth::user();

$review = Review::create([
'rating' => $request->rating,
'content' => $request->content,
'user_id' => $user->id,
'movie_id' => $id,
]);

return redirect(route('review', ['id' => $review->id]));
} catch (Exception $error) {
dd($error);

return redirect()
->back()
->withInput()
->withErrors('Something went wrong when publishing the review!', 'createReview');
}
}

/**
Expand Down
9 changes: 8 additions & 1 deletion resources/views/components/input/rating.blade.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,11 @@
<div x-data="{ rating: 0 }" class="flex flex-col gap-1">
@props([
'name',
'label',
'value' => 0,
'error',
])

<div x-data="{ rating: @js($value) }" class="flex flex-col gap-1">
<x-input.label for="{{ $name }}">
{{ $label }}
</x-input.label>
Expand Down
58 changes: 57 additions & 1 deletion resources/views/movie.blade.php
Original file line number Diff line number Diff line change
@@ -1,3 +1,59 @@
<x-layout>
<h1>movie</h1>
<x-button
x-data
@click="$dispatch('open-modal', 'create-review')"
variant="secondary"
size="sm"
>
Write a review
</x-button>

<x-modal.base
name="create-review"
:show="$errors->createReview->isNotEmpty() || $errors->createReviewValidation->isNotEmpty()"
>
<x-modal.input>
<x-slot:title>
{{ $movie->title }}
</x-slot>
<form
method="post"
action="{{ route('review.store', ['id' => $movie->id, 'title' => $movie->title]) }}"
class="flex flex-col gap-6"
>
@csrf
<div class="flex flex-col gap-4">
<x-input.rating
name="rating"
:value="old('rating')"
required
:error="$errors->createReviewValidation->first('rating')"
label="Your rating"
/>
<x-input.textarea
name="content"
:value="old('content')"
:error="$errors->createReviewValidation->first('content')"
label="Review"
placeholder="Let others know why they should (or shouldn't) watch this film."
color="light"
/>
</div>

<x-input.error :message="$errors->createReview->first()" />

<div class="flex gap-2">
<x-button
x-data
@click="$dispatch('close-modal', 'create-list')"
type="button"
variant="secondary"
>
Cancel
</x-button>
<x-button class="flex-1">Publish review</x-button>
</div>
</form>
</x-modal.input>
</x-modal.base>
</x-layout>
1 change: 1 addition & 0 deletions routes/web.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
Route::get('/u/{username}/reviews', 'index')->name('reviews.user');
Route::get('/m/{id}/{title}/reviews', 'index')->name('reviews.movie');
Route::get('/review/{id}', 'show')->name('review');
Route::post('/m/{id}/{title}', 'store')->middleware(['auth'])->name('review.store');
});

Route::middleware(['auth', AdminMiddleware::class])->prefix('/admin')->group(function () {
Expand Down

0 comments on commit e5a4ebb

Please sign in to comment.