File size: 2,847 Bytes
6966756
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
<?php

namespace App\Http\Controllers;

use App\Models\Cart;
use App\Models\Prompt;
use Illuminate\Http\Request;

class CartController extends Controller {

    public function addToCart(Request $request) {
        if (!auth()->check()) {
            return response()->json(['success' => false, 'message' => 'You must be logged in to add to cart'], 401);
        }

        $promptId = $request->input('prompt_id');

        $userId = auth()->id();

        $cartItem = Cart::where('user_id', $userId)
            ->where('prompt_id', $promptId)
            ->first();

        if ($cartItem) {
            return response()->json(['success' => false, 'message' => 'Item is already in the cart']);
        }

        $prompt = Prompt::find($promptId);

        if ($userId == $prompt->user_id) {
            return response()->json(['success' => false, 'message' => 'You cannot add to cart own prompt'], 400);
        }

        $cart = new Cart;
        $cart->user_id = $userId;
        $cart->prompt_id = $promptId;
        $cart->price = $prompt->price;
        $cart->save();

        return response()->json(['success' => true, 'message' => 'Added to cart successfully']);
    }

    public function cartCount() {

        $userId = auth()->id();
        $cartCount = Cart::where('user_id', $userId)->count();

        return response()->json(['success' => true, 'cart_count' => $cartCount]);
    }

    public function viewCart() {
        if (!auth()->check()) {
            return redirect()->route('user.login');
        }
        $pageTitle = 'Cart';
        $userId = auth()->id();

        $cartItems = Cart::where('user_id', $userId)
            ->with('prompt')
            ->get();

        $subtotal = $cartItems->sum('price');

        $total = $subtotal;

        return view('Template::prompt.cart', compact('cartItems', 'subtotal', 'total', 'pageTitle'));
    }

    public function deleteCart($id) {
        $userId = auth()->id();
        $cartItem = Cart::where('id', $id)->where('user_id', $userId)->first();

        if (!$cartItem) {
            return redirect()->back()->with('error', 'Item not found or unauthorized action');
        }

        $cartItem->delete();

        session()->forget('coupon');

        $notify[] = ['success', 'Item removed from cart successfully'];
        return back()->withNotify($notify);
    }

    public function clearCart() {
        $userId = auth()->id();

        $cartItems = Cart::where('user_id', $userId)->get();

        if ($cartItems->isEmpty()) {
            $notify[] = ['error', 'No items in the cart'];
            return back()->withNotify($notify);
        }

        Cart::where('user_id', $userId)->delete();

        session()->forget('coupon');

        $notify[] = ['success', 'Cart cleared successfully'];
        return back()->withNotify($notify);
    }
}