From 1d96ed8869f85f9b4ec5b6d75d1b1a7e35f8638c Mon Sep 17 00:00:00 2001 From: Lance Pioch Date: Tue, 19 Mar 2024 15:48:11 -0400 Subject: [PATCH] Replace service --- .../Controllers/Admin/NodesController.php | 4 +--- .../Nodes/AllocationController.php | 6 +---- app/Models/Allocation.php | 8 +++++++ .../Allocations/AllocationDeletionService.php | 24 ------------------- 4 files changed, 10 insertions(+), 32 deletions(-) delete mode 100644 app/Services/Allocations/AllocationDeletionService.php diff --git a/app/Http/Controllers/Admin/NodesController.php b/app/Http/Controllers/Admin/NodesController.php index deda130f8..66ba487f6 100644 --- a/app/Http/Controllers/Admin/NodesController.php +++ b/app/Http/Controllers/Admin/NodesController.php @@ -19,7 +19,6 @@ use App\Services\Allocations\AssignmentService; use App\Services\Helpers\SoftwareVersionService; use App\Http\Requests\Admin\Node\NodeFormRequest; use App\Http\Requests\Admin\Node\AllocationFormRequest; -use App\Services\Allocations\AllocationDeletionService; use App\Http\Requests\Admin\Node\AllocationAliasFormRequest; class NodesController extends Controller @@ -29,7 +28,6 @@ class NodesController extends Controller */ public function __construct( protected AlertsMessageBag $alert, - protected AllocationDeletionService $allocationDeletionService, protected AssignmentService $assignmentService, protected CacheRepository $cache, protected NodeCreationService $creationService, @@ -82,7 +80,7 @@ class NodesController extends Controller */ public function allocationRemoveSingle(int $node, Allocation $allocation): Response { - $this->allocationDeletionService->handle($allocation); + $allocation->delete(); return response('', 204); } diff --git a/app/Http/Controllers/Api/Application/Nodes/AllocationController.php b/app/Http/Controllers/Api/Application/Nodes/AllocationController.php index 908d7700f..18e2f5cfc 100644 --- a/app/Http/Controllers/Api/Application/Nodes/AllocationController.php +++ b/app/Http/Controllers/Api/Application/Nodes/AllocationController.php @@ -9,7 +9,6 @@ use Spatie\QueryBuilder\QueryBuilder; use Spatie\QueryBuilder\AllowedFilter; use Illuminate\Database\Eloquent\Builder; use App\Services\Allocations\AssignmentService; -use App\Services\Allocations\AllocationDeletionService; use App\Transformers\Api\Application\AllocationTransformer; use App\Http\Controllers\Api\Application\ApplicationApiController; use App\Http\Requests\Api\Application\Allocations\GetAllocationsRequest; @@ -23,7 +22,6 @@ class AllocationController extends ApplicationApiController */ public function __construct( private AssignmentService $assignmentService, - private AllocationDeletionService $deletionService ) { parent::__construct(); } @@ -71,12 +69,10 @@ class AllocationController extends ApplicationApiController /** * Delete a specific allocation from the Panel. - * - * @throws \App\Exceptions\Service\Allocation\ServerUsingAllocationException */ public function delete(DeleteAllocationRequest $request, Node $node, Allocation $allocation): JsonResponse { - $this->deletionService->handle($allocation); + $allocation->delete(); return new JsonResponse([], JsonResponse::HTTP_NO_CONTENT); } diff --git a/app/Models/Allocation.php b/app/Models/Allocation.php index 98c3941c9..3430d7537 100644 --- a/app/Models/Allocation.php +++ b/app/Models/Allocation.php @@ -2,6 +2,7 @@ namespace App\Models; +use App\Exceptions\Service\Allocation\ServerUsingAllocationException; use Illuminate\Database\Eloquent\Relations\BelongsTo; /** @@ -74,6 +75,13 @@ class Allocation extends Model 'notes' => 'nullable|string|max:256', ]; + protected static function booted(): void + { + static::deleting(function (self $allocation) { + throw_if($allocation->server_id, new ServerUsingAllocationException(trans('exceptions.allocations.server_using'))); + }); + } + /** * {@inheritDoc} */ diff --git a/app/Services/Allocations/AllocationDeletionService.php b/app/Services/Allocations/AllocationDeletionService.php deleted file mode 100644 index a42f36b56..000000000 --- a/app/Services/Allocations/AllocationDeletionService.php +++ /dev/null @@ -1,24 +0,0 @@ -server_id)) { - throw new ServerUsingAllocationException(trans('exceptions.allocations.server_using')); - } - - return (int) $allocation->delete(); - } -}