@@ -15,16 +15,6 @@ class ProductController extends Controller
1515 */
1616 public function index (Request $ request )
1717 {
18- // Get the Authorization header
19- $ token = $ request ->header ('Authorization ' );
20-
21- // Optional: log or return token for debugging
22- if (!$ token ) {
23- return response ()->json ([
24- 'message ' => 'No token provided in Authorization header ' ,
25- 'status ' => 'error '
26- ], 401 );
27- }
2818
2919 // Retrieve all products
3020 $ products = Product::all ();
@@ -49,16 +39,6 @@ public function index(Request $request)
4939 */
5040 public function store (Request $ request )
5141 {
52- // Get the Authorization header
53- $ token = $ request ->header ('Authorization ' );
54-
55- // Optional: log or return token for debugging
56- if (!$ token ) {
57- return response ()->json ([
58- 'message ' => 'No token provided in Authorization header ' ,
59- 'status ' => 'error '
60- ], 401 );
61- }
6242
6343 try {
6444 $ validatedData = $ request ->validate ([
@@ -101,22 +81,111 @@ public function store(Request $request)
10181 */
10282 public function show (string $ id )
10383 {
104- //
84+ $ product = Product::find ($ id );
85+
86+ if (!$ product ) {
87+ return response ()->json ([
88+ 'message ' => 'Product not found ' ,
89+ 'status ' => 'error '
90+ ], 404 );
91+ }
92+ // Return response
93+ return response ()->json ([
94+ 'message ' => 'Product retrieved successfully ' ,
95+ 'product ' => $ product ,
96+ 'status ' => 'success '
97+ ], 200 );
10598 }
10699
107100 /**
108101 * Update the specified resource in storage.
109102 */
110103 public function update (Request $ request , string $ id )
111104 {
112- //
105+
106+ // Check if product ID is provided
107+ if (!$ id ) {
108+ return response ()->json ([
109+ 'message ' => 'Product ID is required ' ,
110+ 'status ' => 'error '
111+ ], 400 );
112+ }
113+
114+ // Find the product
115+ $ product = Product::find ($ id );
116+
117+ // Check if product exists
118+ if (!$ product ) {
119+ return response ()->json ([
120+ 'message ' => 'Product not found ' ,
121+ 'status ' => 'error '
122+ ], 404 );
123+ }
124+ try {
125+ $ validatedData = $ request ->validate ([
126+ 'name ' => 'sometimes|required|string|max:255 ' ,
127+ 'description ' => 'nullable|string ' ,
128+ 'price ' => 'sometimes|required|numeric|min:0 ' ,
129+ 'quantity ' => 'sometimes|required|integer|min:0 ' ,
130+ 'category ' => 'sometimes|required|string|max:255 ' ,
131+ 'status ' => 'sometimes|boolean '
132+ ]);
133+
134+ // Update product
135+ $ product ->update ($ validatedData );
136+
137+ // Return response
138+ return response ()->json ([
139+ 'message ' => 'Product updated successfully ' ,
140+ 'product ' => $ product ,
141+ 'status ' => 'success '
142+ ], 200 );
143+
144+ } catch (ValidationException $ e ) {
145+ return response ()->json ([
146+ 'message ' => 'Validation failed ' ,
147+ 'errors ' => $ e ->errors (),
148+ 'status ' => 'error '
149+ ], 422 );
150+
151+ } catch (\Exception $ e ) {
152+ return response ()->json ([
153+ 'message ' => 'An error occurred while updating the product ' ,
154+ 'error ' => $ e ->getMessage (),
155+ 'status ' => 'error '
156+ ], 500 );
157+ }
113158 }
114159
115160 /**
116161 * Remove the specified resource from storage.
117162 */
118163 public function destroy (string $ id )
119164 {
120- //
165+ // Check if product ID is provided
166+ if (!$ id ) {
167+ return response ()->json ([
168+ 'message ' => 'Product ID is required ' ,
169+ 'status ' => 'error '
170+ ], 400 );
171+ }
172+
173+ // Find the product
174+ $ product = Product::find ($ id );
175+ if (!$ product ) {
176+ return response ()->json ([
177+ 'message ' => 'Product not found ' ,
178+ 'status ' => 'error '
179+ ], 404 );
180+ }
181+
182+ // Delete product
183+ $ product ->delete ();
184+
185+ // Return response
186+ return response ()->json ([
187+ 'message ' => 'Product deleted successfully ' ,
188+ 'status ' => 'success '
189+ ], 200 );
121190 }
122191}
0 commit comments