more (reusable) exceptions, a couple notes
This commit is contained in:
@@ -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();
|
||||
|
||||
|
||||
10
app/Exceptions/ModelStillHasAccessories.php
Normal file
10
app/Exceptions/ModelStillHasAccessories.php
Normal file
@@ -0,0 +1,10 @@
|
||||
<?php
|
||||
|
||||
namespace App\Exceptions;
|
||||
|
||||
use Exception;
|
||||
|
||||
class ModelStillHasAccessories extends Exception
|
||||
{
|
||||
//
|
||||
}
|
||||
10
app/Exceptions/ModelStillHasAssetModels.php
Normal file
10
app/Exceptions/ModelStillHasAssetModels.php
Normal file
@@ -0,0 +1,10 @@
|
||||
<?php
|
||||
|
||||
namespace App\Exceptions;
|
||||
|
||||
use Exception;
|
||||
|
||||
class ModelStillHasAssetModels extends Exception
|
||||
{
|
||||
//
|
||||
}
|
||||
10
app/Exceptions/ModelStillHasComponents.php
Normal file
10
app/Exceptions/ModelStillHasComponents.php
Normal file
@@ -0,0 +1,10 @@
|
||||
<?php
|
||||
|
||||
namespace App\Exceptions;
|
||||
|
||||
use Exception;
|
||||
|
||||
class ModelStillHasComponents extends Exception
|
||||
{
|
||||
//
|
||||
}
|
||||
10
app/Exceptions/ModelStillHasConsumables.php
Normal file
10
app/Exceptions/ModelStillHasConsumables.php
Normal file
@@ -0,0 +1,10 @@
|
||||
<?php
|
||||
|
||||
namespace App\Exceptions;
|
||||
|
||||
use Exception;
|
||||
|
||||
class ModelStillHasConsumables extends Exception
|
||||
{
|
||||
//
|
||||
}
|
||||
@@ -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')));
|
||||
|
||||
Reference in New Issue
Block a user