GOOD SHELL MAS BOY
Server: Apache/2.4.52 (Ubuntu)
System: Linux vmi1836763.contaboserver.net 5.15.0-130-generic #140-Ubuntu SMP Wed Dec 18 17:59:53 UTC 2024 x86_64
User: www-data (33)
PHP: 8.4.10
Disabled: NONE
Upload Files
File: /var/www/console.fixgini.com/app/Http/Controllers/ReviewController.php
<?php

namespace App\Http\Controllers;

use App\Models\BookingForm;
use App\Models\BookingPayment;
use App\Models\Gig;
use App\Models\Review;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;

class ReviewController extends Controller
{
    public function store(Request $request)
    {
        try {
            // Validate input data
            $user = Auth::user();
            $validatedData = $request->validate([
                'gig_id' => 'required|string|exists:gigs,id',
                'comment' => 'required|string|max:255',
                'star_rating' => 'required|numeric|min:1|max:5',
                'photo_url' => 'nullable|string',
                'photo_public_id' => 'nullable|string',
            ]);
            // check if the user_id exist in the booking form and status as completed
            $check = BookingForm::where('customer_id', $user->id)->where('status', 'completed')->first();
            if ($check) {
                $validatedData['user_id'] = $user->id;
                $review = Review::create($validatedData);
                return response()->json(['status' => 'success', 'message' => 'Review shared. Thank you', 'data' => $review], 200);
            } else {
                return response()->json(['status' => 'error', 'message' => 'Engagement not found'], 404);
            }
        } catch (\Throwable $th) {
            return response()->json(['status' => 'error', 'message' => $th->getMessage()], 400);
        }
    }

    public function update(Request $request)
    {
        try {
            // Validate input data
            $validatedData = $request->validate([
                'id' => 'required|exists:reviews,id',
                'comment' => 'sometimes|string',
                'star_rating' => 'sometimes|numeric|min:1|max:5',
                'photo_url' => 'nullable|string',
                'photo_public_id' => 'nullable|string',
            ]);

            // Find the review by ID
            $review = Review::findOrFail($validatedData['id']);

            // Update the review
            $review->update($validatedData);

            return response()->json(['status' => 'success', 'message' => 'Updated successfully', 'data' => $review], 200);
        } catch (\Throwable $th) {
            return response()->json(['status' => 'error', 'message' => $th->getMessage()], 400);
        }
    }

    // Show a specific user's review for a gig
    public function listCustomerReview()
    {
        try {
            $user_id = Auth::user()->id;
            $gigIds = BookingPayment::where('payment_status', 'completed')
                ->whereHas('booking', function ($query) {
                    $query->where('status', 'completed');  // Ensure the booking form is completed
                })
                ->with('booking')  // Load the booking relationship
                ->get()
                ->pluck('booking.gig_id')
                ->unique();  // Avoid duplicate gig IDs

                info($gigIds);
            // Retrieve reviews for the completed gigs
            $reviews = Review::with('gig')
                ->where('user_id', $user_id)  // Ensure reviews belong to the user
                ->whereIn('gig_id', $gigIds)  // Filter by gig IDs
                ->latest()
                ->limit(10)
                ->get();

            // Retrieve gigs pending review for the user
            $pending_reviews = BookingForm::with('gig')
                ->where('customer_id', $user_id)
                ->where('status', 'completed')  // Ensure the booking is completed
                ->whereDoesntHave('review')  // Ensure no review exists for this booking
                ->latest()
                ->limit(10)
                ->get();

                info($reviews . $pending_reviews);
            // Return response with both completed and pending reviews
            return response()->json([
                'status' => 'success',
                'data' => $reviews,
                'pending' => $pending_reviews,
                'message' => 'Reviews found',
            ], 200);
        } catch (\Throwable $th) {
            return response()->json(['status' => 'error', 'message' => $th->getMessage()], 400);
        }
    }

    // Show a seller's reviews by customers
    public function showSellerReview()
    {
        try {
            $user_id = Auth::user()->id;
            // Get all gig IDs for the authenticated user
            $gig_ids = Gig::where('user_id', $user_id)->pluck('id');
            // Retrieve reviews for these gig IDs
            $reviews = Review::with('user', 'gig')->whereIn('gig_id', $gig_ids)->latest()->limit(10)->get();

            if ($reviews->isEmpty()) {
                return response()->json(['status' => 'error', 'message' => 'No reviews found'], 404);
            }

            return response()->json(['status' => 'success', 'data' => $reviews], 200);
        } catch (\Throwable $th) {
            return response()->json(['status' => 'error', 'message' => $th->getMessage()], 400);
        }
    }

    public function deleteReview(Request $request)
    {
        try {
            $validatedData = $request->validate([
                'user_id' => 'required|exists:reviews,user_id',
                'id' => 'required|exists:reviews,id',
            ]);
            $id = $validatedData['id'];
            $user_id = $validatedData['user_id'];
            // Find the review for the specific gig by the user
            $review = Review::where('user_id', $user_id)
                ->where('id', $id)
                ->first();

            if (!$review) {
                return response()->json(['status' => 'error', 'message' => 'Review not found'], 404);
            }

            $review->delete();
            return response()->json(['status' => 'success', 'message' => 'Review deleted successfully'], 200);
        } catch (\Throwable $th) {
            return response()->json(['status' => 'error', 'message' => $th->getMessage()], 400);
        }
    }

    // Show all reviews for a gig
    public function showGigReviews(Request $request)
    {
        try {
            $validatedData = $request->validate([
                'gig_id' => 'required|exists:gigs,id',
            ]);
            $gig_id = $validatedData['gig_id'];
            // Find all reviews for the specific gig
            $reviews = Review::with('user')->where('gig_id', $gig_id)->get();

            if ($reviews->isEmpty()) {
                return response()->json(['status' => 'error', 'message' => 'No reviews found'], 404);
            }

            return response()->json(['status' => 'success', 'data' => $reviews], 200);
        } catch (\Throwable $th) {
            return response()->json(['status' => 'error', 'message' => $th->getMessage(), 'data' => []], 400);
        }
    }
}