diff --git a/app/Actions/Categories/DestroyCategoryAction.php b/app/Actions/Categories/DestroyCategoryAction.php index 82be14afcc..65cdec2b83 100644 --- a/app/Actions/Categories/DestroyCategoryAction.php +++ b/app/Actions/Categories/DestroyCategoryAction.php @@ -2,12 +2,13 @@ namespace App\Actions\Categories; +use App\Exceptions\ModelIsNotDeletable; use App\Helpers\Helper; use App\Models\Category; class DestroyCategoryAction { - static function run(Category $category) + static function run(Category $category): bool { // why do we need to do this? // hm, @@ -20,11 +21,13 @@ class DestroyCategoryAction 'models as models_count' ]); + // this should give better errors, do we throw down in the model for this one, or move that logic up here? + // one of those fat-model vs action things... if (!$category->isDeletable()) { - return response()->json( - Helper::formatStandardApiResponse('error', null, trans('admin/categories/message.assoc_items', ['asset_type' => $category->category_type])) - ); + throw new ModelIsNotDeletable($category); } $category->delete(); + + return true; } } \ No newline at end of file diff --git a/app/Exceptions/ModelIsNotDeletable.php b/app/Exceptions/ModelIsNotDeletable.php new file mode 100644 index 0000000000..31da7914ef --- /dev/null +++ b/app/Exceptions/ModelIsNotDeletable.php @@ -0,0 +1,10 @@ +authorize('delete', Category::class); - + try { + DestroyCategoryAction::run(category: $category); + } catch (ModelIsNotDeletable $e) { + return response()->json( + Helper::formatStandardApiResponse('error', null, trans('admin/categories/message.assoc_items', ['asset_type' => $category->category_type])) + ); + } return response()->json(Helper::formatStandardApiResponse('success', null, trans('admin/categories/message.delete.success'))); }