Skip to content

Commit b2d8f60

Browse files
committed
Implement safe category deletion and cascade deletion methods in CategoryController
This commit enhances the CategoryController by adding a safe deletion method that checks for subcategories and associated products before deleting a category. It also introduces a force deletion method that recursively deletes a category and its subcategories. Both methods return appropriate JSON responses for success and error handling, improving the overall category management functionality.
1 parent 71a5e31 commit b2d8f60

File tree

2 files changed

+98
-1
lines changed

2 files changed

+98
-1
lines changed

app/Http/Controllers/Admin/CategoryController.php

Lines changed: 97 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,9 +83,105 @@ public function update(Request $request, string $id)
8383

8484
/**
8585
* Remove the specified resource from storage.
86+
*
87+
* This method safely deletes a category by checking dependencies first.
88+
* For cascade deletion, use forceDestroy method instead.
8689
*/
8790
public function destroy(string $id)
8891
{
89-
//
92+
try {
93+
$category = Category::findOrFail($id);
94+
95+
// Vérifier s'il y a des sous-catégories
96+
if($category->children()->count() > 0) {
97+
return response()->json([
98+
'success' => false,
99+
'message' => 'Cannot delete category with subcategories. Please delete subcategories first.'
100+
], 400);
101+
}
102+
103+
// Vérifier s'il y a des produits associés
104+
if($category->products()->count() > 0) {
105+
return response()->json([
106+
'success' => false,
107+
'message' => 'Cannot delete category with associated products. Please remove products first.'
108+
], 400);
109+
}
110+
111+
// Supprimer les relations avec les genres
112+
$category->genders()->detach();
113+
114+
// Supprimer la catégorie
115+
$category->delete();
116+
117+
return response()->json([
118+
'success' => true,
119+
'message' => 'Category deleted successfully'
120+
], 200);
121+
122+
} catch (\Illuminate\Database\Eloquent\ModelNotFoundException $e) {
123+
return response()->json([
124+
'success' => false,
125+
'message' => 'Category not found'
126+
], 404);
127+
128+
} catch (\Exception $e) {
129+
return response()->json([
130+
'success' => false,
131+
'message' => 'Something went wrong',
132+
'errors' => $e->getMessage()
133+
], 500);
134+
}
135+
}
136+
137+
/**
138+
* Force delete a category and all its subcategories (cascade deletion).
139+
* Use with caution as this will delete all related data.
140+
*/
141+
public function forceDestroy(string $id)
142+
{
143+
try {
144+
$category = Category::findOrFail($id);
145+
146+
// Supprimer récursivement toutes les sous-catégories
147+
$this->deleteCategoryRecursively($category);
148+
149+
return response()->json([
150+
'success' => true,
151+
'message' => 'Category and all subcategories deleted successfully'
152+
], 200);
153+
154+
} catch (\Illuminate\Database\Eloquent\ModelNotFoundException $e) {
155+
return response()->json([
156+
'success' => false,
157+
'message' => 'Category not found'
158+
], 404);
159+
160+
} catch (\Exception $e) {
161+
return response()->json([
162+
'success' => false,
163+
'message' => 'Something went wrong',
164+
'errors' => $e->getMessage()
165+
], 500);
166+
}
167+
}
168+
169+
/**
170+
* Helper method to recursively delete categories and their relationships
171+
*/
172+
private function deleteCategoryRecursively(Category $category)
173+
{
174+
// Supprimer d'abord les sous-catégories
175+
foreach ($category->children as $child) {
176+
$this->deleteCategoryRecursively($child);
177+
}
178+
179+
// Supprimer les relations
180+
$category->genders()->detach();
181+
$category->products()->detach();
182+
$category->shops()->detach();
183+
184+
// Supprimer la catégorie
185+
$category->delete();
90186
}
91187
}

routes/api.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,7 @@
214214
Route::post('/delivery/confirm/{id}', [ConfirmStatusDeliveryController::class, 'confirmStatusDelivery']);
215215
Route::apiResource('sellers', SellerController::class);
216216
Route::apiResource('categories', CategoryController::class);
217+
Route::delete('/category/{id}', [CategoryController::class, 'destroy']);
217218
Route::apiResource('towns', TownController::class);
218219
Route::apiResource('quarters', QuarterController::class);
219220
Route::apiResource('admin/customers', CustomerController::class);

0 commit comments

Comments
 (0)