Replace service

This commit is contained in:
Lance Pioch 2024-03-19 15:48:11 -04:00
parent 627442e40f
commit 1d96ed8869
4 changed files with 10 additions and 32 deletions

View File

@ -19,7 +19,6 @@ use App\Services\Allocations\AssignmentService;
use App\Services\Helpers\SoftwareVersionService; use App\Services\Helpers\SoftwareVersionService;
use App\Http\Requests\Admin\Node\NodeFormRequest; use App\Http\Requests\Admin\Node\NodeFormRequest;
use App\Http\Requests\Admin\Node\AllocationFormRequest; use App\Http\Requests\Admin\Node\AllocationFormRequest;
use App\Services\Allocations\AllocationDeletionService;
use App\Http\Requests\Admin\Node\AllocationAliasFormRequest; use App\Http\Requests\Admin\Node\AllocationAliasFormRequest;
class NodesController extends Controller class NodesController extends Controller
@ -29,7 +28,6 @@ class NodesController extends Controller
*/ */
public function __construct( public function __construct(
protected AlertsMessageBag $alert, protected AlertsMessageBag $alert,
protected AllocationDeletionService $allocationDeletionService,
protected AssignmentService $assignmentService, protected AssignmentService $assignmentService,
protected CacheRepository $cache, protected CacheRepository $cache,
protected NodeCreationService $creationService, protected NodeCreationService $creationService,
@ -82,7 +80,7 @@ class NodesController extends Controller
*/ */
public function allocationRemoveSingle(int $node, Allocation $allocation): Response public function allocationRemoveSingle(int $node, Allocation $allocation): Response
{ {
$this->allocationDeletionService->handle($allocation); $allocation->delete();
return response('', 204); return response('', 204);
} }

View File

@ -9,7 +9,6 @@ use Spatie\QueryBuilder\QueryBuilder;
use Spatie\QueryBuilder\AllowedFilter; use Spatie\QueryBuilder\AllowedFilter;
use Illuminate\Database\Eloquent\Builder; use Illuminate\Database\Eloquent\Builder;
use App\Services\Allocations\AssignmentService; use App\Services\Allocations\AssignmentService;
use App\Services\Allocations\AllocationDeletionService;
use App\Transformers\Api\Application\AllocationTransformer; use App\Transformers\Api\Application\AllocationTransformer;
use App\Http\Controllers\Api\Application\ApplicationApiController; use App\Http\Controllers\Api\Application\ApplicationApiController;
use App\Http\Requests\Api\Application\Allocations\GetAllocationsRequest; use App\Http\Requests\Api\Application\Allocations\GetAllocationsRequest;
@ -23,7 +22,6 @@ class AllocationController extends ApplicationApiController
*/ */
public function __construct( public function __construct(
private AssignmentService $assignmentService, private AssignmentService $assignmentService,
private AllocationDeletionService $deletionService
) { ) {
parent::__construct(); parent::__construct();
} }
@ -71,12 +69,10 @@ class AllocationController extends ApplicationApiController
/** /**
* Delete a specific allocation from the Panel. * Delete a specific allocation from the Panel.
*
* @throws \App\Exceptions\Service\Allocation\ServerUsingAllocationException
*/ */
public function delete(DeleteAllocationRequest $request, Node $node, Allocation $allocation): JsonResponse public function delete(DeleteAllocationRequest $request, Node $node, Allocation $allocation): JsonResponse
{ {
$this->deletionService->handle($allocation); $allocation->delete();
return new JsonResponse([], JsonResponse::HTTP_NO_CONTENT); return new JsonResponse([], JsonResponse::HTTP_NO_CONTENT);
} }

View File

@ -2,6 +2,7 @@
namespace App\Models; namespace App\Models;
use App\Exceptions\Service\Allocation\ServerUsingAllocationException;
use Illuminate\Database\Eloquent\Relations\BelongsTo; use Illuminate\Database\Eloquent\Relations\BelongsTo;
/** /**
@ -74,6 +75,13 @@ class Allocation extends Model
'notes' => 'nullable|string|max:256', '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} * {@inheritDoc}
*/ */

View File

@ -1,24 +0,0 @@
<?php
namespace App\Services\Allocations;
use App\Models\Allocation;
use App\Exceptions\Service\Allocation\ServerUsingAllocationException;
class AllocationDeletionService
{
/**
* Delete an allocation from the database only if it does not have a server
* that is actively attached to it.
*
* @throws \App\Exceptions\Service\Allocation\ServerUsingAllocationException
*/
public function handle(Allocation $allocation): int
{
if (!is_null($allocation->server_id)) {
throw new ServerUsingAllocationException(trans('exceptions.allocations.server_using'));
}
return (int) $allocation->delete();
}
}