1 | <?php
|
---|
2 |
|
---|
3 | namespace App\Http\Controllers;
|
---|
4 |
|
---|
5 | use App\Enum\OfferStatus;
|
---|
6 | use App\Models\Artist;
|
---|
7 | use App\Models\Offer;
|
---|
8 | use App\Models\Organizer;
|
---|
9 | use App\Models\Review;
|
---|
10 | use Illuminate\Http\Request;
|
---|
11 | use Illuminate\Support\Facades\Auth;
|
---|
12 |
|
---|
13 | class ReviewController extends Controller
|
---|
14 | {
|
---|
15 | //
|
---|
16 | public function __construct()
|
---|
17 | {
|
---|
18 | $this->middleware(['auth', 'verified', 'onboarding', 'role:organizer']);
|
---|
19 | }
|
---|
20 |
|
---|
21 | /**
|
---|
22 | * Store a newly created resource in storage.
|
---|
23 | *
|
---|
24 | * @return \Illuminate\Http\RedirectResponse
|
---|
25 | */
|
---|
26 | public function store(Request $request)
|
---|
27 | {
|
---|
28 | $hasFinishedOfferForTheArtist = Offer::join('events', 'events.id', '=', 'offers.event_id')
|
---|
29 | ->join('organizers', 'organizers.user_id', '=', 'events.organizer_id')
|
---|
30 | ->where('organizers.user_id', '=', Auth::id())
|
---|
31 | ->where('offers.artist_id', '=', $request->artist_id)
|
---|
32 | ->where('offers.status', '=', OfferStatus::COMPLETED->value)
|
---|
33 | ->exists();
|
---|
34 |
|
---|
35 | if ($hasFinishedOfferForTheArtist) {
|
---|
36 | $review = new Review();
|
---|
37 | $review->content = $request->comment;
|
---|
38 | $review->rating = $request->rating;
|
---|
39 |
|
---|
40 | $organizer = Organizer::find(Auth::id());
|
---|
41 | $review->organizer()->associate($organizer);
|
---|
42 |
|
---|
43 | $artist = Artist::find($request->artist_id);
|
---|
44 | $review->artist()->associate($artist);
|
---|
45 |
|
---|
46 | $review->save();
|
---|
47 |
|
---|
48 | return redirect()->back();
|
---|
49 | } else {
|
---|
50 | return redirect()->back()
|
---|
51 | ->with('warning', 'You can not post a review for this artist!');
|
---|
52 | }
|
---|
53 | }
|
---|
54 | }
|
---|