From 5e81c63d6ee70ea5698d234233e138741a8cd513 Mon Sep 17 00:00:00 2001 From: spencerrlongg Date: Wed, 11 Jun 2025 11:39:14 -0500 Subject: [PATCH] some notes and things moved --- app/Actions/Categories/DestroyCategoryAction.php | 11 +++++++---- app/Exceptions/ModelIsNotDeletable.php | 10 ++++++++++ app/Http/Controllers/Api/CategoriesController.php | 12 ++++++++++-- 3 files changed, 27 insertions(+), 6 deletions(-) create mode 100644 app/Exceptions/ModelIsNotDeletable.php 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'))); }