Skip to content

A free open source IT asset/license management system

more (reusable) exceptions, a couple notes

spencerrlongg1cee7e43ed9e
diff --git a/app/Actions/Categories/DestroyCategoryAction.php b/app/Actions/Categories/DestroyCategoryAction.php
index 65cdec2b83..a7470263c1 100644
--- a/app/Actions/Categories/DestroyCategoryAction.php
+++ b/app/Actions/Categories/DestroyCategoryAction.php
@@ -3,6 +3,12 @@
namespace App\Actions\Categories;
use App\Exceptions\ModelIsNotDeletable;
+use App\Exceptions\ModelStillHasAccessories;
+use App\Exceptions\ModelStillHasAssetModels;
+use App\Exceptions\ModelStillHasAssets;
+use App\Exceptions\ModelStillHasComponents;
+use App\Exceptions\ModelStillHasConsumables;
+use App\Exceptions\ModelStillHasLicenses;
use App\Helpers\Helper;
use App\Models\Category;
@@ -10,8 +16,6 @@ class DestroyCategoryAction
{
static function run(Category $category): bool
{
- // why do we need to do this?
- // hm,
$category->loadCount([
'assets as assets_count',
'accessories as accessories_count',
@@ -21,10 +25,23 @@ 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()) {
- throw new ModelIsNotDeletable($category);
+ if ($category->assets_count > 0) {
+ throw new ModelStillHasAssets($category);
+ }
+ if ($category->accessories_count > 0) {
+ throw new ModelStillHasAccessories($category);
+ }
+ if ($category->consumables_count > 0) {
+ throw new ModelStillHasConsumables($category);
+ }
+ if ($category->components_count > 0) {
+ throw new ModelStillHasComponents($category);
+ }
+ if ($category->licenses_count > 0) {
+ throw new ModelStillHasLicenses($category);
+ }
+ if ($category->models_count > 0) {
+ throw new ModelStillHasAssetModels($category);
}
$category->delete();
diff --git a/app/Exceptions/ModelStillHasAccessories.php b/app/Exceptions/ModelStillHasAccessories.php
new file mode 100644
index 0000000000..f11f06dac4
--- /dev/null
+++ b/app/Exceptions/ModelStillHasAccessories.php
@@ -0,0 +1,10 @@
+<?php
+
+namespace App\Exceptions;
+
+use Exception;
+
+class ModelStillHasAccessories extends Exception
+{
+ //
+}
diff --git a/app/Exceptions/ModelStillHasAssetModels.php b/app/Exceptions/ModelStillHasAssetModels.php
new file mode 100644
index 0000000000..7e68c3ac5e
--- /dev/null
+++ b/app/Exceptions/ModelStillHasAssetModels.php
@@ -0,0 +1,10 @@
+<?php
+
+namespace App\Exceptions;
+
+use Exception;
+
+class ModelStillHasAssetModels extends Exception
+{
+ //
+}
diff --git a/app/Exceptions/ModelStillHasComponents.php b/app/Exceptions/ModelStillHasComponents.php
new file mode 100644
index 0000000000..926c7cea4d
--- /dev/null
+++ b/app/Exceptions/ModelStillHasComponents.php
@@ -0,0 +1,10 @@
+<?php
+
+namespace App\Exceptions;
+
+use Exception;
+
+class ModelStillHasComponents extends Exception
+{
+ //
+}
diff --git a/app/Exceptions/ModelStillHasConsumables.php b/app/Exceptions/ModelStillHasConsumables.php
new file mode 100644
index 0000000000..9839d58d79
--- /dev/null
+++ b/app/Exceptions/ModelStillHasConsumables.php
@@ -0,0 +1,10 @@
+<?php
+
+namespace App\Exceptions;
+
+use Exception;
+
+class ModelStillHasConsumables extends Exception
+{
+ //
+}
diff --git a/app/Http/Controllers/Api/CategoriesController.php b/app/Http/Controllers/Api/CategoriesController.php
index 45cad70cbc..539c16dae4 100644
--- a/app/Http/Controllers/Api/CategoriesController.php
+++ b/app/Http/Controllers/Api/CategoriesController.php
@@ -9,6 +9,7 @@ use App\Http\Controllers\Controller;
use App\Http\Transformers\CategoriesTransformer;
use App\Http\Transformers\SelectlistTransformer;
use App\Models\Category;
+use Illuminate\Database\Eloquent\ModelNotFoundException;
use Illuminate\Http\Request;
use Illuminate\Http\JsonResponse;
use App\Http\Requests\ImageUploadRequest;
@@ -216,10 +217,16 @@ class CategoriesController extends Controller
$this->authorize('delete', Category::class);
try {
DestroyCategoryAction::run(category: $category);
- } catch (ModelIsNotDeletable $e) {
+ } catch (ModelNotFoundException $e) {
return response()->json(
Helper::formatStandardApiResponse('error', null, trans('admin/categories/message.assoc_items', ['asset_type' => $category->category_type]))
);
+ } catch (\Throwable $e) {
+ // oooooooo, using exceptions we don't need to catch these individually unless we need to
+ // actually _behave_ differently, so we can just `$e-getMessage()` and define the message when we actually throw.
+ return response()->json(
+ Helper::formatStandardApiResponse('error', null, $e->getMessage())
+ );
}
return response()->json(Helper::formatStandardApiResponse('success', null, trans('admin/categories/message.delete.success')));