@@ -46,54 +46,45 @@ public function index(): AnonymousResourceCollection
46
46
* description="Creates a new product with the provided details.",
47
47
* @OA\RequestBody(
48
48
* required=true,
49
- * @OA\MediaType(
50
- * mediaType="multipart/form-data",
51
- * @OA\Schema(
52
- * @OA\Property(property="name", type="string"),
53
- * @OA\Property(property="description", type="string"),
54
- * @OA\Property(property="slug", type="string", maxLength=15),
55
- * @OA\Property(property="price", type="number", format="float"),
49
+ * @OA\MediaType(
50
+ * mediaType="multipart/form-data",
51
+ * @OA\Schema(
52
+ * type="object",
53
+ * required={"brand_id", "name", "slug", "description", "price"},
54
+ * @OA\Property(property="sub_category_id", type="integer", example=1),
55
+ * @OA\Property(property="brand_id", type="integer", example=1),
56
+ * @OA\Property(property="name", type="string", example="Product Name"),
57
+ * @OA\Property(property="slug", type="string", example="product-name"),
58
+ * @OA\Property(property="description", type="string", example="Product description"),
59
+ * @OA\Property(property="price", type="integer", example=10000),
56
60
* @OA\Property(property="image", type="string", format="binary"),
57
- * @OA\Property(property="sub_category_id", type="integer"),
58
- * @OA\Property(property="brand_id", type="integer"),
59
- * @OA\Property(
61
+ * @OA\Property(
60
62
* property="features",
61
63
* type="array",
62
64
* @OA\Items(
63
65
* type="object",
64
- * @OA\Property(
65
- * property="feature_id",
66
- * type="integer",
67
- * description="ID of the feature"
68
- * ),
69
- * @OA\Property(
70
- * property="value",
71
- * type="string",
72
- * description="Value of the feature"
73
- * )
74
- * ),
75
- * description="Array of product features (optional)"
76
- * ),
66
+ * @OA\Property(property="feature_id", type="integer", example=1),
67
+ * @OA\Property(property="value", type="string", example="Feature value")
68
+ * )
69
+ * )
77
70
* )
78
71
* )
79
- * ),
80
- * @OA\Response(
81
- * response=201,
82
- * description="Product created successfully",
83
- * @OA\JsonContent(
84
- * @OA\Property(property="message", type="string", example="Product created successfully")
85
- * )
86
72
* ),
87
- * @OA\Response(
88
- * response=422,
89
- * description="Validation error",
90
- * @OA\JsonContent(
91
- * @OA\Property(property="message", type="string", example="The given data was invalid."),
92
- * @OA\Property(property="errors", type="object", example={"title": {"The title field is required."}} )
93
- * )
94
- * )
73
+ * @OA\Response(
74
+ * response=201,
75
+ * description="Product created successfully",
76
+ * @OA\JsonContent(
77
+ * @OA\Property(property="message", type="string", example="Product created successfully")
78
+ * )
79
+ * ),
80
+ * @OA\Response(
81
+ * response=400,
82
+ * description="Bad Request",
83
+ * @OA\JsonContent(
84
+ * @OA\Property(property="error", type="string", example="Invalid JSON format for features")
85
+ * )
86
+ * )
95
87
* )
96
- * @throws JsonException
97
88
*/
98
89
public function store (ProductRequest $ request ): JsonResponse
99
90
{
@@ -114,14 +105,21 @@ public function store(ProductRequest $request): JsonResponse
114
105
// Create the product
115
106
$ product = Product::create ($ validatedData );
116
107
117
- // Sync features if provided
118
108
if ($ request ->has ('features ' )) {
119
- foreach (json_decode ($ request ->features , true , 512 , JSON_THROW_ON_ERROR ) as $ feature ) {
120
- $ productFeature = new ProductFeature ();
121
- $ productFeature ->product_id = $ product ->id ;
122
- $ productFeature ->feature_id = $ feature ['feature_id ' ];
123
- $ productFeature ->value = $ feature ['value ' ];
124
- $ productFeature ->save ();
109
+ $ featuresArray = $ request ->input ('features ' );
110
+
111
+ if (is_array ($ featuresArray )) {
112
+ foreach ($ featuresArray as $ featureData ) {
113
+ $ feature = json_decode ($ featureData , true , 512 , JSON_THROW_ON_ERROR );
114
+
115
+ if (json_last_error () === JSON_ERROR_NONE ) {
116
+ $ product ->features ()->attach ($ feature ['feature_id ' ], ['value ' => $ feature ['value ' ]]);
117
+ } else {
118
+ return response ()->json (['error ' => 'Invalid JSON format for features ' ], 400 );
119
+ }
120
+ }
121
+ } else {
122
+ return response ()->json (['error ' => 'Features should be an array ' ], 400 );
125
123
}
126
124
}
127
125
@@ -172,60 +170,44 @@ public function show(Product $product): Response
172
170
* tags={"Products"},
173
171
* summary="Update a product",
174
172
* description="Updates details of an existing product.",
175
- * @OA\Parameter(
176
- * name="id",
177
- * description="Product ID",
178
- * required=true,
179
- * in="path",
180
- * @OA\Schema(
181
- * type="integer"
182
- * )
183
- * ),
173
+ * @OA\Parameter(
174
+ * name="id",
175
+ * in="path",
176
+ * required=true,
177
+ * @OA\Schema(type="integer"),
178
+ * description="Product ID"
179
+ * ),
184
180
* @OA\RequestBody(
185
181
* required=true,
186
- * @OA\MediaType (
187
- * mediaType="multipart/form-data ",
188
- * @OA\Schema(
189
- * @OA\Property(property="_method ", type="string ", example="PATCH" ),
190
- * @OA\Property(property="name", type="string"),
191
- * @OA\Property(property="description ", type="string"),
192
- * @OA\Property(property="slug ", type="string", maxLength=15 ),
193
- * @OA\Property(property="price", type="number ", format="float" ),
194
- * @OA\Property(property="image", type="string", format="binary"),
195
- * @OA\Property(property="sub_category_id", type="integer"),
196
- * @OA\Property( property="brand_id", type="integer") ,
197
- * @OA\Property(
198
- * property="features",
199
- * type="string ",
200
- * description="JSON string representing an array of features. Example: [{"" feature_id"": 1, ""value"": ""string""}]"
201
- * ),
202
- *
182
+ * @OA\JsonContent (
183
+ * type="object ",
184
+ * @OA\Property(property="sub_category_id", type="integer", example=1),
185
+ * @OA\Property(property="brand_id ", type="integer ", example=1 ),
186
+ * @OA\Property(property="name", type="string", example="Product Name "),
187
+ * @OA\Property(property="slug ", type="string", example="product-name "),
188
+ * @OA\Property(property="description ", type="string", example="Product description" ),
189
+ * @OA\Property(property="price", type="integer ", example=10000 ),
190
+ * @OA\Property(property="image", type="string", format="binary"),
191
+ * @OA\Property(
192
+ * property="features" ,
193
+ * type="array",
194
+ * @OA\Items(
195
+ * type="object ",
196
+ * @OA\Property(property=" feature_id", type="integer", example=1),
197
+ * @OA\Property(property="value", type="string", example="Feature value")
198
+ * )
203
199
* )
204
200
* )
205
201
* ),
206
- * @OA\Response(
207
- * response=200,
208
- * description="Product updated successfully",
209
- * @OA\JsonContent(
210
- * @OA\Property(property="message", type="string", example="Product updated successfully")
211
- * )
212
- * ),
213
- * @OA\Response(
214
- * response=400,
215
- * description="Invalid JSON format",
216
- * @OA\JsonContent(
217
- * @OA\Property(property="error", type="string", example="Invalid JSON format for features")
218
- * )
219
- * ),
220
- * @OA\Response(
221
- * response=404,
222
- * description="Product not found",
223
- * @OA\JsonContent(
224
- * @OA\Property(property="message", type="string", example="Product not found")
225
- * )
226
- * ),
202
+ * @OA\Response(
203
+ * response=200,
204
+ * description="Product updated successfully",
205
+ * @OA\JsonContent(
206
+ * @OA\Property(property="message", type="string", example="Product updated successfully")
207
+ * )
208
+ * ),
209
+ * @OA\Response(response=400, description="Bad Request")
227
210
* )
228
- * @throws JsonException
229
211
*/
230
212
public function update (ProductRequest $ request , Product $ product ): JsonResponse
231
213
{
@@ -248,17 +230,14 @@ public function update(ProductRequest $request, Product $product): JsonResponse
248
230
249
231
// Sync features if provided
250
232
if ($ request ->has ('features ' )) {
251
- // Decode JSON string for features into an array
252
233
$ featuresArray = json_decode ($ request ->features , true , 512 , JSON_THROW_ON_ERROR );
234
+
253
235
if ($ featuresArray !== null ) {
236
+ $ syncData = [];
254
237
foreach ($ featuresArray as $ feature ) {
255
- $ productFeature = ProductFeature::whereProductId ($ product ->id )
256
- ->whereFeatureId ($ feature ['feature_id ' ])
257
- ->first ();
258
- $ productFeature ->update (
259
- ['feature_id ' => $ feature ['feature_id ' ], 'value ' => $ feature ['value ' ]]
260
- );
238
+ $ syncData [$ feature ['feature_id ' ]] = ['value ' => $ feature ['value ' ]];
261
239
}
240
+ $ product ->features ()->sync ($ syncData );
262
241
} else {
263
242
// Handle invalid JSON format
264
243
return response ()->json (['error ' => 'Invalid JSON format for features ' ], 400 );
0 commit comments