Skip to content

Commit 11c9f20

Browse files
committed
🎯َ improve codes
1 parent 8a25512 commit 11c9f20

File tree

4 files changed

+146
-187
lines changed

4 files changed

+146
-187
lines changed

app/Http/Controllers/Api/v1/Product/ProductController.php

Lines changed: 80 additions & 101 deletions
Original file line numberDiff line numberDiff line change
@@ -46,54 +46,45 @@ public function index(): AnonymousResourceCollection
4646
* description="Creates a new product with the provided details.",
4747
* @OA\RequestBody(
4848
* 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),
5660
* @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(
6062
* property="features",
6163
* type="array",
6264
* @OA\Items(
6365
* 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+
* )
7770
* )
7871
* )
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-
* )
8672
* ),
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+
* )
9587
* )
96-
* @throws JsonException
9788
*/
9889
public function store(ProductRequest $request): JsonResponse
9990
{
@@ -114,14 +105,21 @@ public function store(ProductRequest $request): JsonResponse
114105
// Create the product
115106
$product = Product::create($validatedData);
116107

117-
// Sync features if provided
118108
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);
125123
}
126124
}
127125

@@ -172,60 +170,44 @@ public function show(Product $product): Response
172170
* tags={"Products"},
173171
* summary="Update a product",
174172
* 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+
* ),
184180
* @OA\RequestBody(
185181
* 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+
* )
203199
* )
204200
* )
205201
* ),
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")
227210
* )
228-
* @throws JsonException
229211
*/
230212
public function update(ProductRequest $request, Product $product): JsonResponse
231213
{
@@ -248,17 +230,14 @@ public function update(ProductRequest $request, Product $product): JsonResponse
248230

249231
// Sync features if provided
250232
if ($request->has('features')) {
251-
// Decode JSON string for features into an array
252233
$featuresArray = json_decode($request->features, true, 512, JSON_THROW_ON_ERROR);
234+
253235
if ($featuresArray !== null) {
236+
$syncData = [];
254237
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']];
261239
}
240+
$product->features()->sync($syncData);
262241
} else {
263242
// Handle invalid JSON format
264243
return response()->json(['error' => 'Invalid JSON format for features'], 400);

app/Models/Feature.php

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,7 @@ class Feature extends Model
1818

1919
public function products(): BelongsToMany
2020
{
21-
return $this->belongsToMany(Product::class, 'product_features');
22-
}
23-
24-
public function product_features()
25-
{
26-
return $this->hasOne(ProductFeature::class);
21+
return $this->belongsToMany(Product::class, 'product_features')->withPivot('value');
2722
}
2823

2924
}

app/Models/Product.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ public function brand(): BelongsTo
3333

3434
public function features(): BelongsToMany
3535
{
36-
return $this->belongsToMany(Feature::class, 'product_features');
36+
return $this->belongsToMany(Feature::class, 'product_features')->withPivot('value');
3737
}
3838

3939

0 commit comments

Comments
 (0)