Compare commits

...

2 commits

Author SHA1 Message Date
Lance Pioch
c2c3465dc4 Fix tests 2022-10-24 00:37:21 -04:00
Lance Pioch
126c9e940f Replace location repository 2022-10-23 04:25:49 -04:00
13 changed files with 41 additions and 159 deletions

View file

@ -4,8 +4,8 @@ namespace Pterodactyl\Console\Commands\Location;
use Illuminate\Console\Command; use Illuminate\Console\Command;
use Illuminate\Support\Collection; use Illuminate\Support\Collection;
use Pterodactyl\Models\Location;
use Pterodactyl\Services\Locations\LocationDeletionService; use Pterodactyl\Services\Locations\LocationDeletionService;
use Pterodactyl\Contracts\Repository\LocationRepositoryInterface;
class DeleteLocationCommand extends Command class DeleteLocationCommand extends Command
{ {
@ -18,10 +18,8 @@ class DeleteLocationCommand extends Command
/** /**
* DeleteLocationCommand constructor. * DeleteLocationCommand constructor.
*/ */
public function __construct( public function __construct(private LocationDeletionService $deletionService)
private LocationDeletionService $deletionService, {
private LocationRepositoryInterface $repository
) {
parent::__construct(); parent::__construct();
} }
@ -33,7 +31,7 @@ class DeleteLocationCommand extends Command
*/ */
public function handle() public function handle()
{ {
$this->locations = $this->locations ?? $this->repository->all(); $this->locations = $this->locations ?? Location::all();
$short = $this->option('short') ?? $this->anticipate( $short = $this->option('short') ?? $this->anticipate(
trans('command/messages.location.ask_short'), trans('command/messages.location.ask_short'),
$this->locations->pluck('short')->toArray() $this->locations->pluck('short')->toArray()

View file

@ -1,33 +0,0 @@
<?php
namespace Pterodactyl\Contracts\Repository;
use Pterodactyl\Models\Location;
use Illuminate\Support\Collection;
interface LocationRepositoryInterface extends RepositoryInterface
{
/**
* Return locations with a count of nodes and servers attached to it.
*/
public function getAllWithDetails(): Collection;
/**
* Return all the available locations with the nodes as a relationship.
*/
public function getAllWithNodes(): Collection;
/**
* Return all the nodes and their respective count of servers for a location.
*
* @throws \Pterodactyl\Exceptions\Repository\RecordNotFoundException
*/
public function getWithNodes(int $id): Location;
/**
* Return a location and the count of nodes in that location.
*
* @throws \Pterodactyl\Exceptions\Repository\RecordNotFoundException
*/
public function getWithNodeCount(int $id): Location;
}

View file

@ -10,12 +10,12 @@ use Illuminate\Http\RedirectResponse;
use Prologue\Alerts\AlertsMessageBag; use Prologue\Alerts\AlertsMessageBag;
use Illuminate\View\Factory as ViewFactory; use Illuminate\View\Factory as ViewFactory;
use Pterodactyl\Http\Controllers\Controller; use Pterodactyl\Http\Controllers\Controller;
use Pterodactyl\Models\Location;
use Pterodactyl\Services\Databases\Hosts\HostUpdateService; use Pterodactyl\Services\Databases\Hosts\HostUpdateService;
use Pterodactyl\Http\Requests\Admin\DatabaseHostFormRequest; use Pterodactyl\Http\Requests\Admin\DatabaseHostFormRequest;
use Pterodactyl\Services\Databases\Hosts\HostCreationService; use Pterodactyl\Services\Databases\Hosts\HostCreationService;
use Pterodactyl\Services\Databases\Hosts\HostDeletionService; use Pterodactyl\Services\Databases\Hosts\HostDeletionService;
use Pterodactyl\Contracts\Repository\DatabaseRepositoryInterface; use Pterodactyl\Contracts\Repository\DatabaseRepositoryInterface;
use Pterodactyl\Contracts\Repository\LocationRepositoryInterface;
use Pterodactyl\Contracts\Repository\DatabaseHostRepositoryInterface; use Pterodactyl\Contracts\Repository\DatabaseHostRepositoryInterface;
class DatabaseController extends Controller class DatabaseController extends Controller
@ -30,7 +30,6 @@ class DatabaseController extends Controller
private HostCreationService $creationService, private HostCreationService $creationService,
private HostDeletionService $deletionService, private HostDeletionService $deletionService,
private HostUpdateService $updateService, private HostUpdateService $updateService,
private LocationRepositoryInterface $locationRepository,
private ViewFactory $view private ViewFactory $view
) { ) {
} }
@ -40,8 +39,10 @@ class DatabaseController extends Controller
*/ */
public function index(): View public function index(): View
{ {
$locations = Location::with('nodes')->get();
return $this->view->make('admin.databases.index', [ return $this->view->make('admin.databases.index', [
'locations' => $this->locationRepository->getAllWithNodes(), 'locations' => $locations,
'hosts' => $this->repository->getWithViewDetails(), 'hosts' => $this->repository->getWithViewDetails(),
]); ]);
} }
@ -53,8 +54,10 @@ class DatabaseController extends Controller
*/ */
public function view(int $host): View public function view(int $host): View
{ {
$locations = Location::with('nodes')->get();
return $this->view->make('admin.databases.view', [ return $this->view->make('admin.databases.view', [
'locations' => $this->locationRepository->getAllWithNodes(), 'locations' => $locations,
'host' => $this->repository->find($host), 'host' => $this->repository->find($host),
'databases' => $this->databaseRepository->getDatabasesForHost($host), 'databases' => $this->databaseRepository->getDatabasesForHost($host),
]); ]);

View file

@ -13,7 +13,6 @@ use Pterodactyl\Http\Requests\Admin\LocationFormRequest;
use Pterodactyl\Services\Locations\LocationUpdateService; use Pterodactyl\Services\Locations\LocationUpdateService;
use Pterodactyl\Services\Locations\LocationCreationService; use Pterodactyl\Services\Locations\LocationCreationService;
use Pterodactyl\Services\Locations\LocationDeletionService; use Pterodactyl\Services\Locations\LocationDeletionService;
use Pterodactyl\Contracts\Repository\LocationRepositoryInterface;
class LocationController extends Controller class LocationController extends Controller
{ {
@ -24,7 +23,6 @@ class LocationController extends Controller
protected AlertsMessageBag $alert, protected AlertsMessageBag $alert,
protected LocationCreationService $creationService, protected LocationCreationService $creationService,
protected LocationDeletionService $deletionService, protected LocationDeletionService $deletionService,
protected LocationRepositoryInterface $repository,
protected LocationUpdateService $updateService, protected LocationUpdateService $updateService,
protected ViewFactory $view protected ViewFactory $view
) { ) {
@ -35,20 +33,23 @@ class LocationController extends Controller
*/ */
public function index(): View public function index(): View
{ {
$locations = Location::query()->withCount(['nodes', 'servers'])->get();
return $this->view->make('admin.locations.index', [ return $this->view->make('admin.locations.index', [
'locations' => $this->repository->getAllWithDetails(), 'locations' => $locations,
]); ]);
} }
/** /**
* Return the location view page. * Return the location view page.
* *
* @throws \Pterodactyl\Exceptions\Repository\RecordNotFoundException
*/ */
public function view(int $id): View public function view(int $id): View
{ {
$location = Location::with('nodes.servers')->findOrFail($id);
return $this->view->make('admin.locations.view', [ return $this->view->make('admin.locations.view', [
'location' => $this->repository->getWithNodes($id), 'location' => $location,
]); ]);
} }

View file

@ -16,7 +16,6 @@ use Pterodactyl\Http\Controllers\Controller;
use Pterodactyl\Http\Requests\Admin\MountFormRequest; use Pterodactyl\Http\Requests\Admin\MountFormRequest;
use Pterodactyl\Repositories\Eloquent\MountRepository; use Pterodactyl\Repositories\Eloquent\MountRepository;
use Pterodactyl\Contracts\Repository\NestRepositoryInterface; use Pterodactyl\Contracts\Repository\NestRepositoryInterface;
use Pterodactyl\Contracts\Repository\LocationRepositoryInterface;
class MountController extends Controller class MountController extends Controller
{ {
@ -26,7 +25,6 @@ class MountController extends Controller
public function __construct( public function __construct(
protected AlertsMessageBag $alert, protected AlertsMessageBag $alert,
protected NestRepositoryInterface $nestRepository, protected NestRepositoryInterface $nestRepository,
protected LocationRepositoryInterface $locationRepository,
protected MountRepository $repository, protected MountRepository $repository,
protected ViewFactory $view protected ViewFactory $view
) { ) {

View file

@ -5,6 +5,7 @@ namespace Pterodactyl\Http\Controllers\Admin\Nodes;
use Illuminate\View\View; use Illuminate\View\View;
use Illuminate\Http\Request; use Illuminate\Http\Request;
use Pterodactyl\Models\Node; use Pterodactyl\Models\Node;
use Pterodactyl\Models\Location;
use Illuminate\Support\Collection; use Illuminate\Support\Collection;
use Pterodactyl\Models\Allocation; use Pterodactyl\Models\Allocation;
use Pterodactyl\Http\Controllers\Controller; use Pterodactyl\Http\Controllers\Controller;
@ -13,7 +14,6 @@ use Pterodactyl\Repositories\Eloquent\NodeRepository;
use Pterodactyl\Repositories\Eloquent\ServerRepository; use Pterodactyl\Repositories\Eloquent\ServerRepository;
use Pterodactyl\Traits\Controllers\JavascriptInjection; use Pterodactyl\Traits\Controllers\JavascriptInjection;
use Pterodactyl\Services\Helpers\SoftwareVersionService; use Pterodactyl\Services\Helpers\SoftwareVersionService;
use Pterodactyl\Repositories\Eloquent\LocationRepository;
use Pterodactyl\Repositories\Eloquent\AllocationRepository; use Pterodactyl\Repositories\Eloquent\AllocationRepository;
class NodeViewController extends Controller class NodeViewController extends Controller
@ -25,7 +25,6 @@ class NodeViewController extends Controller
*/ */
public function __construct( public function __construct(
private AllocationRepository $allocationRepository, private AllocationRepository $allocationRepository,
private LocationRepository $locationRepository,
private NodeRepository $repository, private NodeRepository $repository,
private ServerRepository $serverRepository, private ServerRepository $serverRepository,
private SoftwareVersionService $versionService, private SoftwareVersionService $versionService,
@ -54,7 +53,7 @@ class NodeViewController extends Controller
{ {
return $this->view->make('admin.nodes.view.settings', [ return $this->view->make('admin.nodes.view.settings', [
'node' => $node, 'node' => $node,
'locations' => $this->locationRepository->all(), 'locations' => Location::all(),
]); ]);
} }

View file

@ -4,6 +4,7 @@ namespace Pterodactyl\Http\Controllers\Admin;
use Illuminate\View\View; use Illuminate\View\View;
use Illuminate\Http\Request; use Illuminate\Http\Request;
use Pterodactyl\Models\Location;
use Pterodactyl\Models\Node; use Pterodactyl\Models\Node;
use Illuminate\Http\Response; use Illuminate\Http\Response;
use Pterodactyl\Models\Allocation; use Pterodactyl\Models\Allocation;
@ -22,7 +23,6 @@ use Pterodactyl\Contracts\Repository\NodeRepositoryInterface;
use Pterodactyl\Contracts\Repository\ServerRepositoryInterface; use Pterodactyl\Contracts\Repository\ServerRepositoryInterface;
use Pterodactyl\Http\Requests\Admin\Node\AllocationFormRequest; use Pterodactyl\Http\Requests\Admin\Node\AllocationFormRequest;
use Pterodactyl\Services\Allocations\AllocationDeletionService; use Pterodactyl\Services\Allocations\AllocationDeletionService;
use Pterodactyl\Contracts\Repository\LocationRepositoryInterface;
use Pterodactyl\Contracts\Repository\AllocationRepositoryInterface; use Pterodactyl\Contracts\Repository\AllocationRepositoryInterface;
use Pterodactyl\Http\Requests\Admin\Node\AllocationAliasFormRequest; use Pterodactyl\Http\Requests\Admin\Node\AllocationAliasFormRequest;
@ -39,7 +39,6 @@ class NodesController extends Controller
protected CacheRepository $cache, protected CacheRepository $cache,
protected NodeCreationService $creationService, protected NodeCreationService $creationService,
protected NodeDeletionService $deletionService, protected NodeDeletionService $deletionService,
protected LocationRepositoryInterface $locationRepository,
protected NodeRepositoryInterface $repository, protected NodeRepositoryInterface $repository,
protected ServerRepositoryInterface $serverRepository, protected ServerRepositoryInterface $serverRepository,
protected NodeUpdateService $updateService, protected NodeUpdateService $updateService,
@ -53,7 +52,7 @@ class NodesController extends Controller
*/ */
public function create(): View|RedirectResponse public function create(): View|RedirectResponse
{ {
$locations = $this->locationRepository->all(); $locations = Location::all();
if (count($locations) < 1) { if (count($locations) < 1) {
$this->alert->warning(trans('admin/node.notices.location_required'))->flash(); $this->alert->warning(trans('admin/node.notices.location_required'))->flash();

View file

@ -7,6 +7,7 @@ use Illuminate\View\View;
use Illuminate\Http\Request; use Illuminate\Http\Request;
use Pterodactyl\Models\Nest; use Pterodactyl\Models\Nest;
use Pterodactyl\Models\Server; use Pterodactyl\Models\Server;
use Pterodactyl\Models\Location;
use Pterodactyl\Exceptions\DisplayException; use Pterodactyl\Exceptions\DisplayException;
use Pterodactyl\Http\Controllers\Controller; use Pterodactyl\Http\Controllers\Controller;
use Pterodactyl\Services\Servers\EnvironmentService; use Pterodactyl\Services\Servers\EnvironmentService;
@ -16,7 +17,6 @@ use Pterodactyl\Repositories\Eloquent\NodeRepository;
use Pterodactyl\Repositories\Eloquent\MountRepository; use Pterodactyl\Repositories\Eloquent\MountRepository;
use Pterodactyl\Repositories\Eloquent\ServerRepository; use Pterodactyl\Repositories\Eloquent\ServerRepository;
use Pterodactyl\Traits\Controllers\JavascriptInjection; use Pterodactyl\Traits\Controllers\JavascriptInjection;
use Pterodactyl\Repositories\Eloquent\LocationRepository;
use Pterodactyl\Repositories\Eloquent\DatabaseHostRepository; use Pterodactyl\Repositories\Eloquent\DatabaseHostRepository;
class ServerViewController extends Controller class ServerViewController extends Controller
@ -28,7 +28,6 @@ class ServerViewController extends Controller
*/ */
public function __construct( public function __construct(
private DatabaseHostRepository $databaseHostRepository, private DatabaseHostRepository $databaseHostRepository,
private LocationRepository $locationRepository,
private MountRepository $mountRepository, private MountRepository $mountRepository,
private NestRepository $nestRepository, private NestRepository $nestRepository,
private NodeRepository $nodeRepository, private NodeRepository $nodeRepository,
@ -140,7 +139,7 @@ class ServerViewController extends Controller
return $this->view->make('admin.servers.view.manage', [ return $this->view->make('admin.servers.view.manage', [
'server' => $server, 'server' => $server,
'locations' => $this->locationRepository->all(), 'locations' => Location::all(),
'canTransfer' => $canTransfer, 'canTransfer' => $canTransfer,
]); ]);
} }

View file

@ -13,7 +13,6 @@ use Pterodactyl\Repositories\Eloquent\ServerRepository;
use Pterodactyl\Repositories\Eloquent\SessionRepository; use Pterodactyl\Repositories\Eloquent\SessionRepository;
use Pterodactyl\Repositories\Eloquent\SubuserRepository; use Pterodactyl\Repositories\Eloquent\SubuserRepository;
use Pterodactyl\Repositories\Eloquent\DatabaseRepository; use Pterodactyl\Repositories\Eloquent\DatabaseRepository;
use Pterodactyl\Repositories\Eloquent\LocationRepository;
use Pterodactyl\Repositories\Eloquent\ScheduleRepository; use Pterodactyl\Repositories\Eloquent\ScheduleRepository;
use Pterodactyl\Repositories\Eloquent\SettingsRepository; use Pterodactyl\Repositories\Eloquent\SettingsRepository;
use Pterodactyl\Repositories\Eloquent\AllocationRepository; use Pterodactyl\Repositories\Eloquent\AllocationRepository;
@ -30,7 +29,6 @@ use Pterodactyl\Repositories\Eloquent\ServerVariableRepository;
use Pterodactyl\Contracts\Repository\SessionRepositoryInterface; use Pterodactyl\Contracts\Repository\SessionRepositoryInterface;
use Pterodactyl\Contracts\Repository\SubuserRepositoryInterface; use Pterodactyl\Contracts\Repository\SubuserRepositoryInterface;
use Pterodactyl\Contracts\Repository\DatabaseRepositoryInterface; use Pterodactyl\Contracts\Repository\DatabaseRepositoryInterface;
use Pterodactyl\Contracts\Repository\LocationRepositoryInterface;
use Pterodactyl\Contracts\Repository\ScheduleRepositoryInterface; use Pterodactyl\Contracts\Repository\ScheduleRepositoryInterface;
use Pterodactyl\Contracts\Repository\SettingsRepositoryInterface; use Pterodactyl\Contracts\Repository\SettingsRepositoryInterface;
use Pterodactyl\Contracts\Repository\AllocationRepositoryInterface; use Pterodactyl\Contracts\Repository\AllocationRepositoryInterface;
@ -52,7 +50,6 @@ class RepositoryServiceProvider extends ServiceProvider
$this->app->bind(DatabaseHostRepositoryInterface::class, DatabaseHostRepository::class); $this->app->bind(DatabaseHostRepositoryInterface::class, DatabaseHostRepository::class);
$this->app->bind(EggRepositoryInterface::class, EggRepository::class); $this->app->bind(EggRepositoryInterface::class, EggRepository::class);
$this->app->bind(EggVariableRepositoryInterface::class, EggVariableRepository::class); $this->app->bind(EggVariableRepositoryInterface::class, EggVariableRepository::class);
$this->app->bind(LocationRepositoryInterface::class, LocationRepository::class);
$this->app->bind(NestRepositoryInterface::class, NestRepository::class); $this->app->bind(NestRepositoryInterface::class, NestRepository::class);
$this->app->bind(NodeRepositoryInterface::class, NodeRepository::class); $this->app->bind(NodeRepositoryInterface::class, NodeRepository::class);
$this->app->bind(ScheduleRepositoryInterface::class, ScheduleRepository::class); $this->app->bind(ScheduleRepositoryInterface::class, ScheduleRepository::class);

View file

@ -1,64 +0,0 @@
<?php
namespace Pterodactyl\Repositories\Eloquent;
use Pterodactyl\Models\Location;
use Illuminate\Support\Collection;
use Illuminate\Database\Eloquent\ModelNotFoundException;
use Pterodactyl\Exceptions\Repository\RecordNotFoundException;
use Pterodactyl\Contracts\Repository\LocationRepositoryInterface;
class LocationRepository extends EloquentRepository implements LocationRepositoryInterface
{
/**
* Return the model backing this repository.
*/
public function model(): string
{
return Location::class;
}
/**
* Return locations with a count of nodes and servers attached to it.
*/
public function getAllWithDetails(): Collection
{
return $this->getBuilder()->withCount('nodes', 'servers')->get($this->getColumns());
}
/**
* Return all the available locations with the nodes as a relationship.
*/
public function getAllWithNodes(): Collection
{
return $this->getBuilder()->with('nodes')->get($this->getColumns());
}
/**
* Return all the nodes and their respective count of servers for a location.
*
* @throws \Pterodactyl\Exceptions\Repository\RecordNotFoundException
*/
public function getWithNodes(int $id): Location
{
try {
return $this->getBuilder()->with('nodes.servers')->findOrFail($id, $this->getColumns());
} catch (ModelNotFoundException) {
throw new RecordNotFoundException();
}
}
/**
* Return a location and the count of nodes in that location.
*
* @throws \Pterodactyl\Exceptions\Repository\RecordNotFoundException
*/
public function getWithNodeCount(int $id): Location
{
try {
return $this->getBuilder()->withCount('nodes')->findOrFail($id, $this->getColumns());
} catch (ModelNotFoundException) {
throw new RecordNotFoundException();
}
}
}

View file

@ -3,24 +3,18 @@
namespace Pterodactyl\Services\Locations; namespace Pterodactyl\Services\Locations;
use Pterodactyl\Models\Location; use Pterodactyl\Models\Location;
use Pterodactyl\Contracts\Repository\LocationRepositoryInterface;
class LocationCreationService class LocationCreationService
{ {
/**
* LocationCreationService constructor.
*/
public function __construct(protected LocationRepositoryInterface $repository)
{
}
/** /**
* Create a new location. * Create a new location.
* *
* @throws \Pterodactyl\Exceptions\Model\DataValidationException
*/ */
public function handle(array $data): Location public function handle(array $data): Location
{ {
return $this->repository->create($data); /** @var Location $location */
$location = Location::query()->create($data);
return $location;
} }
} }

View file

@ -5,7 +5,6 @@ namespace Pterodactyl\Services\Locations;
use Webmozart\Assert\Assert; use Webmozart\Assert\Assert;
use Pterodactyl\Models\Location; use Pterodactyl\Models\Location;
use Pterodactyl\Contracts\Repository\NodeRepositoryInterface; use Pterodactyl\Contracts\Repository\NodeRepositoryInterface;
use Pterodactyl\Contracts\Repository\LocationRepositoryInterface;
use Pterodactyl\Exceptions\Service\Location\HasActiveNodesException; use Pterodactyl\Exceptions\Service\Location\HasActiveNodesException;
class LocationDeletionService class LocationDeletionService
@ -13,28 +12,25 @@ class LocationDeletionService
/** /**
* LocationDeletionService constructor. * LocationDeletionService constructor.
*/ */
public function __construct( public function __construct(protected NodeRepositoryInterface $nodeRepository)
protected LocationRepositoryInterface $repository, {
protected NodeRepositoryInterface $nodeRepository
) {
} }
/** /**
* Delete an existing location. * Delete an existing location.
* *
* @throws \Pterodactyl\Exceptions\Service\Location\HasActiveNodesException * @throws HasActiveNodesException
*/ */
public function handle(Location|int $location): ?int public function handle(Location|int $location): ?int
{ {
$location = ($location instanceof Location) ? $location->id : $location; $location = ($location instanceof Location) ? $location : $location->id;
Assert::integerish($location, 'First argument passed to handle must be numeric or an instance of ' . Location::class . ', received %s.'); $count = $location->nodes()->count();
$count = $this->nodeRepository->findCountWhere([['location_id', '=', $location]]);
if ($count > 0) { if ($count > 0) {
throw new HasActiveNodesException(trans('exceptions.locations.has_nodes')); throw new HasActiveNodesException(trans('exceptions.locations.has_nodes'));
} }
return $this->repository->delete($location); return $location->delete();
} }
} }

View file

@ -3,27 +3,22 @@
namespace Pterodactyl\Services\Locations; namespace Pterodactyl\Services\Locations;
use Pterodactyl\Models\Location; use Pterodactyl\Models\Location;
use Pterodactyl\Contracts\Repository\LocationRepositoryInterface;
class LocationUpdateService class LocationUpdateService
{ {
/**
* LocationUpdateService constructor.
*/
public function __construct(protected LocationRepositoryInterface $repository)
{
}
/** /**
* Update an existing location. * Update an existing location.
* *
* @throws \Pterodactyl\Exceptions\Model\DataValidationException
* @throws \Pterodactyl\Exceptions\Repository\RecordNotFoundException
*/ */
public function handle(Location|int $location, array $data): Location public function handle(Location|int $location, array $data): Location
{ {
$location = ($location instanceof Location) ? $location->id : $location; /** @var Location $location */
if (is_int($location)) {
$location = Location::query()->findOrFail($location);
}
return $this->repository->update($location, $data); $location->update($data);
return $location;
} }
} }