Skip to content

Commit e5a4ebb

Browse files
authored
feat: create review modal (#384)
2 parents 504b44f + 6175f73 commit e5a4ebb

File tree

5 files changed

+96
-14
lines changed

5 files changed

+96
-14
lines changed

app/Http/Controllers/MovieController.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,11 @@ public function store(Request $request)
3636
/**
3737
* Display the specified resource.
3838
*/
39-
public function show(Movie $movie)
39+
public function show($id)
4040
{
41-
return view('movie');
41+
$movie = Movie::findOrFail($id);
42+
43+
return view('movie', ['movie' => $movie]);
4244
}
4345

4446
/**

app/Http/Controllers/ReviewController.php

Lines changed: 26 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use App\Models\Review;
66
use App\Models\User;
7+
use Exception;
78
use Illuminate\Http\Request;
89
use Illuminate\Support\Facades\Auth;
910

@@ -25,20 +26,35 @@ public function index($username)
2526
]);
2627
}
2728

28-
/**
29-
* Show the form for creating a new resource.
30-
*/
31-
public function create()
32-
{
33-
//
34-
}
35-
3629
/**
3730
* Store a newly created resource in storage.
3831
*/
39-
public function store(Request $request)
32+
public function store(Request $request, $id)
4033
{
41-
//
34+
$request->validateWithBag('createReviewValidation', [
35+
'rating' => ['required', 'integer', 'min:1', 'max:10'],
36+
'content' => ['nullable', 'string', 'max:800'],
37+
]);
38+
39+
try {
40+
$user = Auth::user();
41+
42+
$review = Review::create([
43+
'rating' => $request->rating,
44+
'content' => $request->content,
45+
'user_id' => $user->id,
46+
'movie_id' => $id,
47+
]);
48+
49+
return redirect(route('review', ['id' => $review->id]));
50+
} catch (Exception $error) {
51+
dd($error);
52+
53+
return redirect()
54+
->back()
55+
->withInput()
56+
->withErrors('Something went wrong when publishing the review!', 'createReview');
57+
}
4258
}
4359

4460
/**

resources/views/components/input/rating.blade.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,11 @@
1-
<div x-data="{ rating: 0 }" class="flex flex-col gap-1">
1+
@props([
2+
'name',
3+
'label',
4+
'value' => 0,
5+
'error',
6+
])
7+
8+
<div x-data="{ rating: @js($value) }" class="flex flex-col gap-1">
29
<x-input.label for="{{ $name }}">
310
{{ $label }}
411
</x-input.label>

resources/views/movie.blade.php

Lines changed: 57 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,59 @@
11
<x-layout>
2-
<h1>movie</h1>
2+
<x-button
3+
x-data
4+
@click="$dispatch('open-modal', 'create-review')"
5+
variant="secondary"
6+
size="sm"
7+
>
8+
Write a review
9+
</x-button>
10+
11+
<x-modal.base
12+
name="create-review"
13+
:show="$errors->createReview->isNotEmpty() || $errors->createReviewValidation->isNotEmpty()"
14+
>
15+
<x-modal.input>
16+
<x-slot:title>
17+
{{ $movie->title }}
18+
</x-slot>
19+
<form
20+
method="post"
21+
action="{{ route('review.store', ['id' => $movie->id, 'title' => $movie->title]) }}"
22+
class="flex flex-col gap-6"
23+
>
24+
@csrf
25+
<div class="flex flex-col gap-4">
26+
<x-input.rating
27+
name="rating"
28+
:value="old('rating')"
29+
required
30+
:error="$errors->createReviewValidation->first('rating')"
31+
label="Your rating"
32+
/>
33+
<x-input.textarea
34+
name="content"
35+
:value="old('content')"
36+
:error="$errors->createReviewValidation->first('content')"
37+
label="Review"
38+
placeholder="Let others know why they should (or shouldn't) watch this film."
39+
color="light"
40+
/>
41+
</div>
42+
43+
<x-input.error :message="$errors->createReview->first()" />
44+
45+
<div class="flex gap-2">
46+
<x-button
47+
x-data
48+
@click="$dispatch('close-modal', 'create-list')"
49+
type="button"
50+
variant="secondary"
51+
>
52+
Cancel
53+
</x-button>
54+
<x-button class="flex-1">Publish review</x-button>
55+
</div>
56+
</form>
57+
</x-modal.input>
58+
</x-modal.base>
359
</x-layout>

routes/web.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
Route::get('/u/{username}/reviews', 'index')->name('reviews.user');
2727
Route::get('/m/{id}/{title}/reviews', 'index')->name('reviews.movie');
2828
Route::get('/review/{id}', 'show')->name('review');
29+
Route::post('/m/{id}/{title}', 'store')->middleware(['auth'])->name('review.store');
2930
});
3031

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

0 commit comments

Comments
 (0)