Skip to content

Commit 994e7f3

Browse files
committed
Add product trash management functionality in ProductController
This commit introduces methods to manage product visibility in a trash state. The 'putInTrash' method allows products to be marked as trashed, while 'productListOfTrash' retrieves products that are in the trash. Additionally, the 'is_trashed' attribute is added to the Product model and migration, and the API routes are updated to include these new functionalities, enhancing product management capabilities.
1 parent 89d0dc6 commit 994e7f3

File tree

4 files changed

+32
-4
lines changed

4 files changed

+32
-4
lines changed

app/Http/Controllers/Seller/ProductController.php

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,18 @@ class ProductController extends Controller
2525
public function index()
2626
{
2727
$shop = Shop::where('user_id', Auth::guard('api')->user()->id)->first();
28-
$products = Product::where('shop_id', $shop->id)->orderBy('created_at', 'desc')->get();
28+
$products = Product::where('shop_id', $shop->id)->orderBy('created_at', 'desc')
29+
->where("is_trashed",0)
30+
->get();
31+
32+
return ProductResource::collection($products);
33+
}
34+
35+
public function productListOfTrash(){
36+
$shop = Shop::where('user_id', Auth::guard('api')->user()->id)->first();
37+
$products = Product::where('shop_id', $shop->id)->orderBy('created_at', 'desc')
38+
->where("is_trashed",1)
39+
->get();
2940

3041
return ProductResource::collection($products);
3142
}
@@ -120,12 +131,18 @@ public function store(Request $request)
120131

121132
// Gestion des images pour cette couleur
122133
$colorImageKey = "color_" . $colorGroup['color']['id'] . "_image_";
134+
123135
$imageIndex = 0;
136+
Log::info("img:",[
137+
'request'=>$request->hasFile($colorImageKey . $imageIndex)
138+
]);
124139
while ($request->hasFile($colorImageKey . $imageIndex)) {
140+
125141
$imagePath = $request->file($colorImageKey . $imageIndex)
126142
->store('product/variations', 'public');
127143
$variation->images()->create(['image_path' => $imagePath]);
128144
$imageIndex++;
145+
Log::info("Ended imageIndex: ".$imageIndex);
129146
}
130147

131148
// Gestion des sous-variations (tailles/pointures)
@@ -207,4 +224,12 @@ public function destroy(string $id)
207224
{
208225
//
209226
}
227+
228+
public function putInTrash($id){
229+
$product=Product::find($id);
230+
$product->is_trashed=1;
231+
$product->status=0;
232+
$product->save();
233+
return response()->json(['message' => 'Product put in trash successfully']);
234+
}
210235
}

app/Models/Product.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,8 @@ class Product extends Model
2828
'product_price',
2929
'product_quantity',
3030
'is_wholesale',
31-
'is_only_wholesale'
31+
'is_only_wholesale',
32+
"is_trashed"
3233
];
3334
protected $keyType = 'string';
3435
public $incrementing = false;

database/migrations/2024_11_26_191119_create_products_table.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ public function up(): void
3535
$table->string('product_gender');
3636
$table->string('whatsapp_number')->nullable();
3737
$table->string('product_residence')->nullable();
38+
$table->boolean('is_trashed')->default(0);
3839
$table->boolean('type')->default(0);
3940
$table->timestamps();
4041
});

routes/api.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -181,8 +181,9 @@
181181
Route::post('update/categories',[UpdateSellerController::class,'updateCategories']);
182182
Route::post('update/images',[UpdateSellerController::class,'updateGalerieImages']);
183183
Route::post("/boost/shop",[BoostShopController::class,'boost']);
184-
185-
184+
185+
Route::post('/put/in/trash/{id}',[ProductController::class,'putInTrash']);
186+
Route::get('/seller/products/trash',[ProductController::class,'productListOfTrash']);
186187

187188
// Routes pour la gestion des commandes du vendeur
188189
Route::get('/seller/orders', [OrderListController::class, 'listOrders']);

0 commit comments

Comments
 (0)