mirror of
				https://github.com/pelican-dev/panel.git
				synced 2025-11-04 04:16:52 +01:00 
			
		
		
		
	Replace service
This commit is contained in:
		
							parent
							
								
									627442e40f
								
							
						
					
					
						commit
						1d96ed8869
					
				@ -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);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
@ -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);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
@ -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}
 | 
			
		||||
     */
 | 
			
		||||
 | 
			
		||||
@ -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();
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user