|
2 | 2 |
|
3 | 3 | namespace App\Http\Controllers; |
4 | 4 |
|
| 5 | +use App\Models\orderItem; |
5 | 6 | use App\Models\Review; |
6 | 7 | use App\Models\transaction; |
7 | 8 | use Illuminate\Http\Request; |
8 | 9 | use Illuminate\Support\Facades\Auth; |
9 | 10 |
|
10 | 11 | class reviewController extends Controller |
11 | 12 | { |
12 | | - public function storeReview(Request $request){ |
| 13 | + public function store(Request $request) |
| 14 | + { |
13 | 15 | $request->validate([ |
14 | | - 'transaction_id' => 'required|exists:transactions,id', |
15 | | - 'product_id' => 'required|exists:order_items,product_id', |
| 16 | + 'order_item_id' => 'required|exists:order_items,id', |
16 | 17 | 'rating' => 'required|integer|min:1|max:5', |
17 | 18 | 'comment' => 'nullable|string|max:500', |
18 | 19 | ]); |
19 | 20 |
|
20 | | - $trasaction = transaction::where('id', $request->transaction_id) |
21 | | - ->where('user_id', Auth::id()) |
22 | | - ->whereNotNull('confirmed_received_at') |
23 | | - ->firstOrFail(); |
| 21 | + $user = Auth::user(); |
24 | 22 |
|
25 | | - if(!$trasaction){ |
| 23 | + $orderItem = orderItem::with('transaction')->findOrFail($request->order_item_id); |
| 24 | + |
| 25 | + if($orderItem->transaction->user_id !== $user->id) { |
26 | 26 | return response()->json([ |
27 | | - 'message' => 'Transaction not found or you are not authorized to review this transaction or transaction not confirmed received', |
28 | | - ], 404); |
| 27 | + 'message' => 'You are not authorized to review this order item', |
| 28 | + ], 403); |
29 | 29 | } |
30 | 30 |
|
31 | | - $alreadyReviewed = Review::where('transaction_id', $trasaction->id) |
32 | | - ->where('product_id', $request->product_id) |
33 | | - ->where('user_id', Auth::id()) |
34 | | - ->exists(); |
| 31 | + if($orderItem->transaction->shipping_status !== 'completed') { |
| 32 | + return response()->json([ |
| 33 | + 'message' => 'You can only review completed transactions', |
| 34 | + ], 400); |
| 35 | + } |
35 | 36 |
|
36 | | - if ($alreadyReviewed) { |
| 37 | + if($orderItem->review) { |
37 | 38 | return response()->json([ |
38 | | - 'message' => 'You have already reviewed this product', |
| 39 | + 'message' => 'You have already reviewed this order item', |
39 | 40 | ], 400); |
40 | 41 | } |
41 | 42 |
|
42 | | - Review::create([ |
43 | | - 'transaction_id' => $trasaction->id, |
44 | | - 'product_id' => $request->product_id, |
45 | | - 'user_id' => Auth::id(), |
| 43 | + $review = Review::create([ |
| 44 | + 'user_id' => $user->id, |
| 45 | + 'product_id' => $orderItem->product_id, |
| 46 | + 'order_item_id' => $orderItem->id, |
46 | 47 | 'rating' => $request->rating, |
47 | 48 | 'comment' => $request->comment, |
48 | 49 | ]); |
49 | 50 |
|
50 | 51 | return response()->json([ |
51 | | - 'message' => 'Review submitted successfully', |
| 52 | + 'message' => 'Review created successfully', |
| 53 | + 'review' => $review, |
52 | 54 | ], 201); |
53 | 55 | } |
54 | 56 |
|
|
0 commit comments