10
10
use Illuminate \Support \Facades \DB ;
11
11
use Illuminate \Support \Facades \Session ;
12
12
use Illuminate \Support \Facades \Storage ;
13
+ use Spatie \QueryBuilder \AllowedFilter ;
14
+ use Spatie \QueryBuilder \QueryBuilder ;
13
15
14
16
class ProductController extends Controller
15
17
{
@@ -22,7 +24,17 @@ public function __construct(RecommendationService $recommendationService)
22
24
23
25
public function index (Request $ request )
24
26
{
25
- $ products = Product::paginate ();
27
+ $ products = QueryBuilder::for (Product::class)
28
+ ->allowedFilters ([
29
+ 'name ' ,
30
+ 'price ' ,
31
+ 'created_at ' ,
32
+ AllowedFilter::scope ('price_min ' ),
33
+ AllowedFilter::scope ('price_max ' ),
34
+ ])
35
+ ->allowedSorts (['name ' , 'price ' , 'created_at ' ])
36
+ ->paginate ();
37
+
26
38
return view ('products.index ' , compact ('products ' ));
27
39
}
28
40
@@ -50,157 +62,118 @@ public function show(Product $product)
50
62
return view ('products.show ' , compact ('product ' ));
51
63
}
52
64
53
- public function create (Request $ request )
54
- {
55
- // Handle Product File Upload
56
- if ($ request ->hasFile ('product_file ' )) {
57
- $ file = $ request ->file ('product_file ' );
58
- $ filePath = $ file ->store ('public/downloadable_products ' );
59
- $ fileUrl = Storage::url ($ filePath );
60
- } else {
61
- $ fileUrl = null ;
62
- }
63
-
64
- $ validatedData = $ request ->validate ([
65
- 'name ' => 'required|string|max:255 ' ,
66
- 'description ' => 'required|string ' ,
67
- 'price ' => 'required|numeric ' ,
68
- 'category ' => 'required|string|max:255 ' ,
69
- 'inventory_count ' => 'required|integer ' ,
70
- // Include download limit in validation
71
- 'download_limit ' => 'integer|nullable ' ,
72
- ]);
73
-
74
- $ product = Product::create ($ validatedData );
75
-
76
- // Create an initial inventory log entry
77
- $ product ->inventoryLogs ()->create ([
78
- 'quantity_change ' => $ validatedData ['inventory_count ' ],
79
- 'reason ' => 'Initial stock setup ' ,
80
- ]);
81
-
82
- return response ()->json ($ product , Response::HTTP_CREATED );
83
- }
84
-
85
-
86
-
87
-
88
-
89
- public function update (Request $ request , $ id )
90
- {
91
- $ product = Product::find ($ id );
92
-
93
- if (!$ product ) {
94
- return response ()->json (['message ' => 'Product not found ' ], Response::HTTP_NOT_FOUND );
95
- }
96
-
97
- $ validatedData = $ request ->validate ([
98
- 'name ' => 'string|max:255 ' ,
99
- 'description ' => 'string ' ,
100
- 'price ' => 'numeric ' ,
101
- 'category ' => 'string|max:255 ' ,
102
- 'inventory_count ' => 'integer ' ,
103
- ]);
104
-
105
- // Handle Product File Upload for Update
106
- if ($ request ->hasFile ('product_file ' )) {
107
- $ file = $ request ->file ('product_file ' );
108
- $ filePath = $ file ->store ('public/downloadable_products ' );
109
- $ fileUrl = Storage::url ($ filePath );
110
- // Update Downloadable Product entry
111
- $ product ->downloadable ()->updateOrCreate (['product_id ' => $ product ->id ], ['file_url ' => $ fileUrl , 'download_limit ' => $ request ->download_limit ]);
112
- }
113
-
114
- $ product ->update ($ validatedData );
115
-
116
- return response ()->json ($ product );
117
- }
118
-
119
- public function delete ($ id )
120
- {
121
- $ product = Product::find ($ id );
122
-
123
- if (!$ product ) {
124
- return response ()->json (['message ' => 'Product not found ' ], Response::HTTP_NOT_FOUND );
125
- }
126
-
127
- $ product ->delete ();
128
-
129
- return response ()->json (['message ' => 'Product deleted successfully ' ]);
130
- }
131
-
132
- public function search (Request $ request )
133
- {
134
- $ query = Product::query ();
135
-
136
- if ($ request ->has ('keyword ' )) {
137
- $ query ->where (function ($ q ) use ($ request ) {
138
- $ q ->where ('name ' , 'like ' , '% ' . $ request ->keyword . '% ' )
139
- ->orWhere ('description ' , 'like ' , '% ' . $ request ->keyword . '% ' );
140
- });
141
- }
142
-
143
- if ($ request ->has ('category ' )) {
144
- $ query ->where ('category ' , $ request ->category );
145
- }
146
-
147
- if ($ request ->has ('min_price ' )) {
148
- $ query ->where ('price ' , '>= ' , $ request ->min_price );
149
- }
150
65
151
- if ($ request ->has ('max_price ' )) {
152
- $ query ->where ('price ' , '<= ' , $ request ->max_price );
153
- }
154
-
155
- $ products = $ query ->paginate (12 );
156
-
157
- return view ('products.search ' , compact ('products ' ));
158
- }
159
-
160
- public function addToCompare (Request $ request , $ id )
161
- {
162
- $ product = Product::findOrFail ($ id );
163
- $ compareList = Session::get ('compare_list ' , []);
164
-
165
- if (!in_array ($ id , $ compareList ) && count ($ compareList ) < 4 ) {
166
- $ compareList [] = $ id ;
167
- Session::put ('compare_list ' , $ compareList );
168
- return redirect ()->back ()->with ('success ' , 'Product added to comparison. ' );
169
- } elseif (in_array ($ id , $ compareList )) {
170
- return redirect ()->back ()->with ('info ' , 'Product is already in the comparison list. ' );
171
- } else {
172
- return redirect ()->back ()->with ('error ' , 'You can compare up to 4 products at a time. ' );
173
- }
174
- }
175
-
176
- public function compare ()
177
- {
178
- $ compareList = Session::get ('compare_list ' , []);
179
- $ products = Product::whereIn ('id ' , $ compareList )->get ();
180
-
181
- return view ('products.compare ' , compact ('products ' ));
182
- }
183
-
184
- public function removeFromCompare ($ id )
185
- {
186
- $ compareList = Session::get ('compare_list ' , []);
187
- $ compareList = array_diff ($ compareList , [$ id ]);
188
- Session::put ('compare_list ' , $ compareList );
189
-
190
- return redirect ()->back ()->with ('success ' , 'Product removed from comparison. ' );
191
- }
192
-
193
- public function clearCompare ()
194
- {
195
- Session::forget ('compare_list ' );
196
- return redirect ()->route ('products.list ' )->with ('success ' , 'Comparison list cleared. ' );
197
- }
66
+ // public function create(Request $request)
67
+ // {
68
+ // // Handle Product File Upload
69
+ // if ($request->hasFile('product_file')) {
70
+ // $file = $request->file('product_file');
71
+ // $filePath = $file->store('public/downloadable_products');
72
+ // $fileUrl = Storage::url($filePath);
73
+ // } else {
74
+ // $fileUrl = null;
75
+ // }
76
+
77
+ // $validatedData = $request->validate([
78
+ // 'name' => 'required|string|max:255',
79
+ // 'description' => 'required|string',
80
+ // 'price' => 'required|numeric',
81
+ // 'category' => 'required|string|max:255',
82
+ // 'inventory_count' => 'required|integer',
83
+ // // Include download limit in validation
84
+ // 'download_limit' => 'integer|nullable',
85
+ // ]);
86
+
87
+ // $product = Product::create($validatedData);
88
+
89
+ // // Create an initial inventory log entry
90
+ // $product->inventoryLogs()->create([
91
+ // 'quantity_change' => $validatedData['inventory_count'],
92
+ // 'reason' => 'Initial stock setup',
93
+ // ]);
94
+
95
+ // return response()->json($product, Response::HTTP_CREATED);
96
+ // }
97
+
98
+ // public function update(Request $request, $id)
99
+ // {
100
+ // $product = Product::find($id);
101
+
102
+ // if (!$product) {
103
+ // return response()->json(['message' => 'Product not found'], Response::HTTP_NOT_FOUND);
104
+ // }
105
+
106
+ // $validatedData = $request->validate([
107
+ // 'name' => 'string|max:255',
108
+ // 'description' => 'string',
109
+ // 'price' => 'numeric',
110
+ // 'category' => 'string|max:255',
111
+ // 'inventory_count' => 'integer',
112
+ // ]);
113
+
114
+ // // Handle Product File Upload for Update
115
+ // if ($request->hasFile('product_file')) {
116
+ // $file = $request->file('product_file');
117
+ // $filePath = $file->store('public/downloadable_products');
118
+ // $fileUrl = Storage::url($filePath);
119
+ // // Update Downloadable Product entry
120
+ // $product->downloadable()->updateOrCreate(['product_id' => $product->id], ['file_url' => $fileUrl, 'download_limit' => $request->download_limit]);
121
+ // }
122
+
123
+ // $product->update($validatedData);
124
+
125
+ // return response()->json($product);
126
+ // }
127
+
128
+ // public function delete($id)
129
+ // {
130
+ // $product = Product::find($id);
131
+
132
+ // if (!$product) {
133
+ // return response()->json(['message' => 'Product not found'], Response::HTTP_NOT_FOUND);
134
+ // }
135
+
136
+ // $product->delete();
137
+
138
+ // return response()->json(['message' => 'Product deleted successfully']);
139
+ // }
140
+
141
+ // public function addToCompare(Request $request, $id)
142
+ // {
143
+ // $product = Product::findOrFail($id);
144
+ // $compareList = Session::get('compare_list', []);
145
+
146
+ // if (!in_array($id, $compareList) && count($compareList) < 4) {
147
+ // $compareList[] = $id;
148
+ // Session::put('compare_list', $compareList);
149
+ // return redirect()->back()->with('success', 'Product added to comparison.');
150
+ // } elseif (in_array($id, $compareList)) {
151
+ // return redirect()->back()->with('info', 'Product is already in the comparison list.');
152
+ // } else {
153
+ // return redirect()->back()->with('error', 'You can compare up to 4 products at a time.');
154
+ // }
155
+ // }
156
+
157
+ // public function compare()
158
+ // {
159
+ // $compareList = Session::get('compare_list', []);
160
+ // $products = Product::whereIn('id', $compareList)->get();
161
+
162
+ // return view('products.compare', compact('products'));
163
+ // }
164
+
165
+ // public function removeFromCompare($id)
166
+ // {
167
+ // $compareList = Session::get('compare_list', []);
168
+ // $compareList = array_diff($compareList, [$id]);
169
+ // Session::put('compare_list', $compareList);
170
+
171
+ // return redirect()->back()->with('success', 'Product removed from comparison.');
172
+ // }
173
+
174
+ // public function clearCompare()
175
+ // {
176
+ // Session::forget('compare_list');
177
+ // return redirect()->route('products.list')->with('success', 'Comparison list cleared.');
178
+ // }
198
179
}
199
- // Check if inventory_count is being updated and log the change
200
- if (isset ($ validatedData ['inventory_count ' ])) {
201
- $ quantityChange = $ validatedData ['inventory_count ' ] - $ product ->getOriginal ('inventory_count ' );
202
- $ product ->inventoryLogs ()->create ([
203
- 'quantity_change ' => $ quantityChange ,
204
- 'reason ' => 'Inventory adjustment ' ,
205
- ]);
206
- }
0 commit comments