feat: Implement client-facing service management API with UUID for services and bulk actions for product services.
This commit is contained in:
@@ -78,10 +78,10 @@ class ProductCategoryController extends Controller
|
||||
|
||||
$validated['slug'] = Str::slug($validated['name']);
|
||||
|
||||
// Ensure unique slug
|
||||
// Ensure unique slug (include soft-deleted to avoid DB unique constraint violation)
|
||||
$originalSlug = $validated['slug'];
|
||||
$count = 1;
|
||||
while (ProductCategories::where('slug', $validated['slug'])->exists()) {
|
||||
while (ProductCategories::withTrashed()->where('slug', $validated['slug'])->exists()) {
|
||||
$validated['slug'] = "{$originalSlug}-{$count}";
|
||||
$count++;
|
||||
}
|
||||
@@ -115,7 +115,7 @@ class ProductCategoryController extends Controller
|
||||
$slug = Str::slug($validated['name']);
|
||||
$originalSlug = $slug;
|
||||
$count = 1;
|
||||
while (ProductCategories::where('slug', $slug)->where('id', '!=', $id)->exists()) {
|
||||
while (ProductCategories::withTrashed()->where('slug', $slug)->where('id', '!=', $id)->exists()) {
|
||||
$slug = "{$originalSlug}-{$count}";
|
||||
$count++;
|
||||
}
|
||||
@@ -153,4 +153,118 @@ class ProductCategoryController extends Controller
|
||||
'message' => 'Xoá nhóm sản phẩm thành công.',
|
||||
]);
|
||||
}
|
||||
|
||||
public function trashed(): JsonResponse
|
||||
{
|
||||
$items = ProductCategories::onlyTrashed()
|
||||
->withCount('products')
|
||||
->orderBy('deleted_at', 'desc')
|
||||
->get();
|
||||
|
||||
return response()->json([
|
||||
'error' => false,
|
||||
'data' => ProductCategoryResource::collection($items),
|
||||
]);
|
||||
}
|
||||
|
||||
public function restore(int $id): JsonResponse
|
||||
{
|
||||
$category = ProductCategories::onlyTrashed()->find($id);
|
||||
|
||||
if (!$category) {
|
||||
return response()->json(['error' => true, 'message' => 'Không tìm thấy nhóm trong thùng rác.'], 404);
|
||||
}
|
||||
|
||||
$category->restore();
|
||||
|
||||
return response()->json([
|
||||
'error' => false,
|
||||
'message' => 'Khôi phục nhóm sản phẩm thành công.',
|
||||
]);
|
||||
}
|
||||
|
||||
public function forceDelete(int $id): JsonResponse
|
||||
{
|
||||
$category = ProductCategories::onlyTrashed()->find($id);
|
||||
|
||||
if (!$category) {
|
||||
return response()->json(['error' => true, 'message' => 'Không tìm thấy nhóm trong thùng rác.'], 404);
|
||||
}
|
||||
|
||||
$category->forceDelete();
|
||||
|
||||
return response()->json([
|
||||
'error' => false,
|
||||
'message' => 'Xoá vĩnh viễn nhóm sản phẩm thành công.',
|
||||
]);
|
||||
}
|
||||
|
||||
public function bulkUpdateStatus(Request $request): JsonResponse
|
||||
{
|
||||
$validated = $request->validate([
|
||||
'ids' => 'required|array',
|
||||
'ids.*' => 'integer',
|
||||
'is_active' => 'required|boolean',
|
||||
]);
|
||||
|
||||
$updated = ProductCategories::whereIn('id', $validated['ids'])
|
||||
->update(['is_active' => $validated['is_active']]);
|
||||
|
||||
$label = $validated['is_active'] ? 'kích hoạt' : 'ẩn';
|
||||
|
||||
return response()->json([
|
||||
'error' => false,
|
||||
'message' => "Đã {$label} {$updated} nhóm sản phẩm.",
|
||||
]);
|
||||
}
|
||||
|
||||
public function bulkDestroy(Request $request): JsonResponse
|
||||
{
|
||||
$ids = $request->validate(['ids' => 'required|array', 'ids.*' => 'integer'])['ids'];
|
||||
|
||||
$blocked = ProductCategories::withCount('products')
|
||||
->whereIn('id', $ids)
|
||||
->get()
|
||||
->filter(fn ($c) => $c->products_count > 0);
|
||||
|
||||
if ($blocked->isNotEmpty()) {
|
||||
$names = $blocked->pluck('name')->implode(', ');
|
||||
return response()->json([
|
||||
'error' => true,
|
||||
'message' => "Không thể xoá các nhóm đang chứa sản phẩm: {$names}.",
|
||||
], 422);
|
||||
}
|
||||
|
||||
$deleted = ProductCategories::whereIn('id', $ids)->delete();
|
||||
|
||||
return response()->json([
|
||||
'error' => false,
|
||||
'message' => "Đã xoá {$deleted} nhóm sản phẩm.",
|
||||
]);
|
||||
}
|
||||
|
||||
public function bulkRestore(Request $request): JsonResponse
|
||||
{
|
||||
$ids = $request->validate(['ids' => 'required|array', 'ids.*' => 'integer'])['ids'];
|
||||
|
||||
$restored = ProductCategories::onlyTrashed()->whereIn('id', $ids)->restore();
|
||||
|
||||
return response()->json([
|
||||
'error' => false,
|
||||
'message' => "Đã khôi phục {$restored} nhóm sản phẩm.",
|
||||
]);
|
||||
}
|
||||
|
||||
public function bulkForceDelete(Request $request): JsonResponse
|
||||
{
|
||||
$ids = $request->validate(['ids' => 'required|array', 'ids.*' => 'integer'])['ids'];
|
||||
|
||||
$count = ProductCategories::onlyTrashed()->whereIn('id', $ids)->count();
|
||||
ProductCategories::onlyTrashed()->whereIn('id', $ids)->forceDelete();
|
||||
|
||||
return response()->json([
|
||||
'error' => false,
|
||||
'message' => "Đã xoá vĩnh viễn {$count} nhóm sản phẩm.",
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user