@@ -15,16 +15,6 @@ class ProductController extends Controller
15
15
*/
16
16
public function index (Request $ request )
17
17
{
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
- }
28
18
29
19
// Retrieve all products
30
20
$ products = Product::all ();
@@ -49,16 +39,6 @@ public function index(Request $request)
49
39
*/
50
40
public function store (Request $ request )
51
41
{
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
- }
62
42
63
43
try {
64
44
$ validatedData = $ request ->validate ([
@@ -101,22 +81,111 @@ public function store(Request $request)
101
81
*/
102
82
public function show (string $ id )
103
83
{
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 );
105
98
}
106
99
107
100
/**
108
101
* Update the specified resource in storage.
109
102
*/
110
103
public function update (Request $ request , string $ id )
111
104
{
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
+ }
113
158
}
114
159
115
160
/**
116
161
* Remove the specified resource from storage.
117
162
*/
118
163
public function destroy (string $ id )
119
164
{
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 );
121
190
}
122
191
}
0 commit comments