· 6 years ago · Mar 31, 2020, 04:28 PM
1<?php
2
3namespace App\Http\Controllers;
4use Illuminate\Http\Request;
5use Illuminate\Support\Facades\DB;
6use Illuminate\Support\Facades\Session;
7class HomeController extends Controller
8{
9 public $jsonHeader;
10 public function __construct(){
11 $this->jsonHeader = ['Content-Type', 'application/json'];
12 }
13
14 // VIEW AREA
15 public function home(){
16 return view('home');
17 }
18
19 // API AREA
20 // GET ALL PRODUCTS
21 public function getProducts(){
22 try {
23 $data_prod = [];
24 $prod = DB::table('products')
25 ->select('id', 'product_name', 'description', 'price')
26 ->get();
27 foreach ($prod as $p) {
28 $data_prod[] = [
29 'id' => $p->id,
30 'product_name' => $p->product_name,
31 'description' => $p->description,
32 'price' => $p->price,
33 'main_img' => DB::table('product_images')
34 ->select('image_name')
35 ->where('image_order', 1)
36 ->where('product_id', $p->id)
37 ->first(),
38 'images' => DB::table('product_images')
39 ->select('id','image_name', 'image_order')
40 ->where('product_id', $p->id)
41 ->orderBy('image_order', 'ASC')
42 ->get()
43 ];
44 }
45 return response()->json(
46 [
47 'success' => true,
48 'data' => $data_prod
49 ],
50 200,
51 $this->jsonHeader
52 );
53 } catch (\Throwable $th) {
54 return response()->json([
55 'success' => false,
56 'error' => $th
57 ],
58 400,
59 $this->jsonHeader
60 );
61 }
62
63 }
64
65 // GET SINGLE PRODUCT
66 public function getProduct(Request $request)
67 {
68 try {
69 $data_prod = [];
70 $prod = DB::table('products')
71 ->select('id', 'product_name', 'description', 'price')
72 ->where('id',$request->product_id)
73 ->get();
74 foreach ($prod as $p) {
75 $data_prod[] = [
76 'id' => $p->id,
77 'product_name' => $p->product_name,
78 'description' => $p->description,
79 'price' => $p->price,
80 'main_img' => DB::table('product_images')
81 ->select('image_name')
82 ->where('image_order',1)
83 ->where('product_id', $p->id)
84 ->first(),
85 'images' => DB::table('product_images')
86 ->select('id','image_name', 'image_order')
87 ->where('product_id', $p->id)
88 ->orderBy('image_order','ASC')
89 ->get()
90 ];
91 }
92 return response()->json(
93 [
94 'success' => true,
95 'data' => $data_prod
96 ],
97 200,
98 $this->jsonHeader
99 );
100 } catch (\Throwable $th) {
101 return response()->json(
102 [
103 'success' => false,
104 'error' => $th
105 ],
106 400,
107 $this->jsonHeader
108 );
109 }
110 }
111
112 // ADD TO CART
113 public function addToCart(Request $request){
114 try {
115 // check if session cart exists
116 if (Session::has('cart') === false) {
117 Session::put(
118 'cart',[
119 [
120 'product_id' => $request->product_id,
121 'product_qty' => $request->product_qty,
122 ]
123 ]
124 );
125 } else {
126 $cart = Session::get('cart');
127 $arr_key = null;
128 // check if there same prod remain
129 foreach ($cart as $key => $value) {
130 if ($value['product_id'] === $request->product_id) {
131 $arr_key = $key;
132 }
133 }
134 // if same product remain we just change the qty
135 if ($arr_key !== null) {
136 $cart[$arr_key] = [
137 'product_id' => $request->product_id,
138 'product_qty' => $request->product_qty,
139 ];
140 Session::put('cart', $cart);
141 } else {
142 array_push($cart, [
143 'product_id' => $request->product_id,
144 'product_qty' => $request->product_qty,
145 ]);
146 Session::put('cart', $cart);
147 }
148 }
149
150 return response()->json(
151 [
152 'success' => true,
153 'data' => Session::get('cart'),
154 ],
155 200,
156 $this->jsonHeader
157 );
158 } catch (\Throwable $th) {
159 return response()->json(
160 [
161 'success' => false,
162 'error' => $th
163 ],
164 400,
165 $this->jsonHeader
166 );
167 }
168 }
169}