From 126c9e940f1439ed2879a4e9acc140ca6d050251 Mon Sep 17 00:00:00 2001 From: Lance Pioch Date: Sun, 23 Oct 2022 04:25:49 -0400 Subject: [PATCH] Replace location repository --- .../Location/DeleteLocationCommand.php | 10 ++- .../LocationRepositoryInterface.php | 33 ---------- .../Controllers/Admin/DatabaseController.php | 11 ++-- .../Controllers/Admin/LocationController.php | 11 ++-- .../Controllers/Admin/MountController.php | 2 - .../Admin/Nodes/NodeViewController.php | 5 +- .../Controllers/Admin/NodesController.php | 5 +- .../Admin/Servers/ServerViewController.php | 5 +- app/Providers/RepositoryServiceProvider.php | 3 - .../Eloquent/LocationRepository.php | 64 ------------------- .../Locations/LocationCreationService.php | 14 ++-- .../Locations/LocationDeletionService.php | 12 ++-- .../Locations/LocationUpdateService.php | 12 +--- 13 files changed, 33 insertions(+), 154 deletions(-) delete mode 100644 app/Contracts/Repository/LocationRepositoryInterface.php delete mode 100644 app/Repositories/Eloquent/LocationRepository.php diff --git a/app/Console/Commands/Location/DeleteLocationCommand.php b/app/Console/Commands/Location/DeleteLocationCommand.php index 3193e0c3d..c9113d852 100644 --- a/app/Console/Commands/Location/DeleteLocationCommand.php +++ b/app/Console/Commands/Location/DeleteLocationCommand.php @@ -4,8 +4,8 @@ namespace Pterodactyl\Console\Commands\Location; use Illuminate\Console\Command; use Illuminate\Support\Collection; +use Pterodactyl\Models\Location; use Pterodactyl\Services\Locations\LocationDeletionService; -use Pterodactyl\Contracts\Repository\LocationRepositoryInterface; class DeleteLocationCommand extends Command { @@ -18,10 +18,8 @@ class DeleteLocationCommand extends Command /** * DeleteLocationCommand constructor. */ - public function __construct( - private LocationDeletionService $deletionService, - private LocationRepositoryInterface $repository - ) { + public function __construct(private LocationDeletionService $deletionService) + { parent::__construct(); } @@ -33,7 +31,7 @@ class DeleteLocationCommand extends Command */ public function handle() { - $this->locations = $this->locations ?? $this->repository->all(); + $this->locations = $this->locations ?? Location::all(); $short = $this->option('short') ?? $this->anticipate( trans('command/messages.location.ask_short'), $this->locations->pluck('short')->toArray() diff --git a/app/Contracts/Repository/LocationRepositoryInterface.php b/app/Contracts/Repository/LocationRepositoryInterface.php deleted file mode 100644 index 066a2e7c6..000000000 --- a/app/Contracts/Repository/LocationRepositoryInterface.php +++ /dev/null @@ -1,33 +0,0 @@ -get(); + return $this->view->make('admin.databases.index', [ - 'locations' => $this->locationRepository->getAllWithNodes(), + 'locations' => $locations, 'hosts' => $this->repository->getWithViewDetails(), ]); } @@ -53,8 +54,10 @@ class DatabaseController extends Controller */ public function view(int $host): View { + $locations = Location::with('nodes')->get(); + return $this->view->make('admin.databases.view', [ - 'locations' => $this->locationRepository->getAllWithNodes(), + 'locations' => $locations, 'host' => $this->repository->find($host), 'databases' => $this->databaseRepository->getDatabasesForHost($host), ]); diff --git a/app/Http/Controllers/Admin/LocationController.php b/app/Http/Controllers/Admin/LocationController.php index ea01cbaa9..94e5f0814 100644 --- a/app/Http/Controllers/Admin/LocationController.php +++ b/app/Http/Controllers/Admin/LocationController.php @@ -13,7 +13,6 @@ use Pterodactyl\Http\Requests\Admin\LocationFormRequest; use Pterodactyl\Services\Locations\LocationUpdateService; use Pterodactyl\Services\Locations\LocationCreationService; use Pterodactyl\Services\Locations\LocationDeletionService; -use Pterodactyl\Contracts\Repository\LocationRepositoryInterface; class LocationController extends Controller { @@ -24,7 +23,6 @@ class LocationController extends Controller protected AlertsMessageBag $alert, protected LocationCreationService $creationService, protected LocationDeletionService $deletionService, - protected LocationRepositoryInterface $repository, protected LocationUpdateService $updateService, protected ViewFactory $view ) { @@ -35,20 +33,23 @@ class LocationController extends Controller */ public function index(): View { + $locations = Location::query()->withCount(['nodes', 'servers'])->get(); + return $this->view->make('admin.locations.index', [ - 'locations' => $this->repository->getAllWithDetails(), + 'locations' => $locations, ]); } /** * Return the location view page. * - * @throws \Pterodactyl\Exceptions\Repository\RecordNotFoundException */ public function view(int $id): View { + $location = Location::with('nodes.servers')->findOrFail($id); + return $this->view->make('admin.locations.view', [ - 'location' => $this->repository->getWithNodes($id), + 'location' => $location, ]); } diff --git a/app/Http/Controllers/Admin/MountController.php b/app/Http/Controllers/Admin/MountController.php index 097ad6690..a9e55b34a 100644 --- a/app/Http/Controllers/Admin/MountController.php +++ b/app/Http/Controllers/Admin/MountController.php @@ -16,7 +16,6 @@ use Pterodactyl\Http\Controllers\Controller; use Pterodactyl\Http\Requests\Admin\MountFormRequest; use Pterodactyl\Repositories\Eloquent\MountRepository; use Pterodactyl\Contracts\Repository\NestRepositoryInterface; -use Pterodactyl\Contracts\Repository\LocationRepositoryInterface; class MountController extends Controller { @@ -26,7 +25,6 @@ class MountController extends Controller public function __construct( protected AlertsMessageBag $alert, protected NestRepositoryInterface $nestRepository, - protected LocationRepositoryInterface $locationRepository, protected MountRepository $repository, protected ViewFactory $view ) { diff --git a/app/Http/Controllers/Admin/Nodes/NodeViewController.php b/app/Http/Controllers/Admin/Nodes/NodeViewController.php index 673890323..09c89a6e8 100644 --- a/app/Http/Controllers/Admin/Nodes/NodeViewController.php +++ b/app/Http/Controllers/Admin/Nodes/NodeViewController.php @@ -5,6 +5,7 @@ namespace Pterodactyl\Http\Controllers\Admin\Nodes; use Illuminate\View\View; use Illuminate\Http\Request; use Pterodactyl\Models\Node; +use Pterodactyl\Models\Location; use Illuminate\Support\Collection; use Pterodactyl\Models\Allocation; use Pterodactyl\Http\Controllers\Controller; @@ -13,7 +14,6 @@ use Pterodactyl\Repositories\Eloquent\NodeRepository; use Pterodactyl\Repositories\Eloquent\ServerRepository; use Pterodactyl\Traits\Controllers\JavascriptInjection; use Pterodactyl\Services\Helpers\SoftwareVersionService; -use Pterodactyl\Repositories\Eloquent\LocationRepository; use Pterodactyl\Repositories\Eloquent\AllocationRepository; class NodeViewController extends Controller @@ -25,7 +25,6 @@ class NodeViewController extends Controller */ public function __construct( private AllocationRepository $allocationRepository, - private LocationRepository $locationRepository, private NodeRepository $repository, private ServerRepository $serverRepository, private SoftwareVersionService $versionService, @@ -54,7 +53,7 @@ class NodeViewController extends Controller { return $this->view->make('admin.nodes.view.settings', [ 'node' => $node, - 'locations' => $this->locationRepository->all(), + 'locations' => Location::all(), ]); } diff --git a/app/Http/Controllers/Admin/NodesController.php b/app/Http/Controllers/Admin/NodesController.php index 573a1d9f8..ad77ede8b 100644 --- a/app/Http/Controllers/Admin/NodesController.php +++ b/app/Http/Controllers/Admin/NodesController.php @@ -4,6 +4,7 @@ namespace Pterodactyl\Http\Controllers\Admin; use Illuminate\View\View; use Illuminate\Http\Request; +use Pterodactyl\Models\Location; use Pterodactyl\Models\Node; use Illuminate\Http\Response; use Pterodactyl\Models\Allocation; @@ -22,7 +23,6 @@ use Pterodactyl\Contracts\Repository\NodeRepositoryInterface; use Pterodactyl\Contracts\Repository\ServerRepositoryInterface; use Pterodactyl\Http\Requests\Admin\Node\AllocationFormRequest; use Pterodactyl\Services\Allocations\AllocationDeletionService; -use Pterodactyl\Contracts\Repository\LocationRepositoryInterface; use Pterodactyl\Contracts\Repository\AllocationRepositoryInterface; use Pterodactyl\Http\Requests\Admin\Node\AllocationAliasFormRequest; @@ -39,7 +39,6 @@ class NodesController extends Controller protected CacheRepository $cache, protected NodeCreationService $creationService, protected NodeDeletionService $deletionService, - protected LocationRepositoryInterface $locationRepository, protected NodeRepositoryInterface $repository, protected ServerRepositoryInterface $serverRepository, protected NodeUpdateService $updateService, @@ -53,7 +52,7 @@ class NodesController extends Controller */ public function create(): View|RedirectResponse { - $locations = $this->locationRepository->all(); + $locations = Location::all(); if (count($locations) < 1) { $this->alert->warning(trans('admin/node.notices.location_required'))->flash(); diff --git a/app/Http/Controllers/Admin/Servers/ServerViewController.php b/app/Http/Controllers/Admin/Servers/ServerViewController.php index 7cf64a2f5..30eb2c01b 100644 --- a/app/Http/Controllers/Admin/Servers/ServerViewController.php +++ b/app/Http/Controllers/Admin/Servers/ServerViewController.php @@ -7,6 +7,7 @@ use Illuminate\View\View; use Illuminate\Http\Request; use Pterodactyl\Models\Nest; use Pterodactyl\Models\Server; +use Pterodactyl\Models\Location; use Pterodactyl\Exceptions\DisplayException; use Pterodactyl\Http\Controllers\Controller; use Pterodactyl\Services\Servers\EnvironmentService; @@ -16,7 +17,6 @@ use Pterodactyl\Repositories\Eloquent\NodeRepository; use Pterodactyl\Repositories\Eloquent\MountRepository; use Pterodactyl\Repositories\Eloquent\ServerRepository; use Pterodactyl\Traits\Controllers\JavascriptInjection; -use Pterodactyl\Repositories\Eloquent\LocationRepository; use Pterodactyl\Repositories\Eloquent\DatabaseHostRepository; class ServerViewController extends Controller @@ -28,7 +28,6 @@ class ServerViewController extends Controller */ public function __construct( private DatabaseHostRepository $databaseHostRepository, - private LocationRepository $locationRepository, private MountRepository $mountRepository, private NestRepository $nestRepository, private NodeRepository $nodeRepository, @@ -140,7 +139,7 @@ class ServerViewController extends Controller return $this->view->make('admin.servers.view.manage', [ 'server' => $server, - 'locations' => $this->locationRepository->all(), + 'locations' => Location::all(), 'canTransfer' => $canTransfer, ]); } diff --git a/app/Providers/RepositoryServiceProvider.php b/app/Providers/RepositoryServiceProvider.php index 8a0434f52..7dfa7d8c1 100644 --- a/app/Providers/RepositoryServiceProvider.php +++ b/app/Providers/RepositoryServiceProvider.php @@ -13,7 +13,6 @@ use Pterodactyl\Repositories\Eloquent\ServerRepository; use Pterodactyl\Repositories\Eloquent\SessionRepository; use Pterodactyl\Repositories\Eloquent\SubuserRepository; use Pterodactyl\Repositories\Eloquent\DatabaseRepository; -use Pterodactyl\Repositories\Eloquent\LocationRepository; use Pterodactyl\Repositories\Eloquent\ScheduleRepository; use Pterodactyl\Repositories\Eloquent\SettingsRepository; use Pterodactyl\Repositories\Eloquent\AllocationRepository; @@ -30,7 +29,6 @@ use Pterodactyl\Repositories\Eloquent\ServerVariableRepository; use Pterodactyl\Contracts\Repository\SessionRepositoryInterface; use Pterodactyl\Contracts\Repository\SubuserRepositoryInterface; use Pterodactyl\Contracts\Repository\DatabaseRepositoryInterface; -use Pterodactyl\Contracts\Repository\LocationRepositoryInterface; use Pterodactyl\Contracts\Repository\ScheduleRepositoryInterface; use Pterodactyl\Contracts\Repository\SettingsRepositoryInterface; use Pterodactyl\Contracts\Repository\AllocationRepositoryInterface; @@ -52,7 +50,6 @@ class RepositoryServiceProvider extends ServiceProvider $this->app->bind(DatabaseHostRepositoryInterface::class, DatabaseHostRepository::class); $this->app->bind(EggRepositoryInterface::class, EggRepository::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(NodeRepositoryInterface::class, NodeRepository::class); $this->app->bind(ScheduleRepositoryInterface::class, ScheduleRepository::class); diff --git a/app/Repositories/Eloquent/LocationRepository.php b/app/Repositories/Eloquent/LocationRepository.php deleted file mode 100644 index e25737cb3..000000000 --- a/app/Repositories/Eloquent/LocationRepository.php +++ /dev/null @@ -1,64 +0,0 @@ -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(); - } - } -} diff --git a/app/Services/Locations/LocationCreationService.php b/app/Services/Locations/LocationCreationService.php index b1a3ec995..cdfa0cc08 100644 --- a/app/Services/Locations/LocationCreationService.php +++ b/app/Services/Locations/LocationCreationService.php @@ -3,24 +3,18 @@ namespace Pterodactyl\Services\Locations; use Pterodactyl\Models\Location; -use Pterodactyl\Contracts\Repository\LocationRepositoryInterface; class LocationCreationService { - /** - * LocationCreationService constructor. - */ - public function __construct(protected LocationRepositoryInterface $repository) - { - } - /** * Create a new location. * - * @throws \Pterodactyl\Exceptions\Model\DataValidationException */ public function handle(array $data): Location { - return $this->repository->create($data); + /** @var Location $location */ + $location = Location::query()->create($data); + + return $location; } } diff --git a/app/Services/Locations/LocationDeletionService.php b/app/Services/Locations/LocationDeletionService.php index 5b4b9eba4..628dd82d3 100644 --- a/app/Services/Locations/LocationDeletionService.php +++ b/app/Services/Locations/LocationDeletionService.php @@ -5,7 +5,6 @@ namespace Pterodactyl\Services\Locations; use Webmozart\Assert\Assert; use Pterodactyl\Models\Location; use Pterodactyl\Contracts\Repository\NodeRepositoryInterface; -use Pterodactyl\Contracts\Repository\LocationRepositoryInterface; use Pterodactyl\Exceptions\Service\Location\HasActiveNodesException; class LocationDeletionService @@ -13,16 +12,15 @@ class LocationDeletionService /** * LocationDeletionService constructor. */ - public function __construct( - protected LocationRepositoryInterface $repository, - protected NodeRepositoryInterface $nodeRepository - ) { + public function __construct(protected NodeRepositoryInterface $nodeRepository) + { + } /** * Delete an existing location. * - * @throws \Pterodactyl\Exceptions\Service\Location\HasActiveNodesException + * @throws HasActiveNodesException */ public function handle(Location|int $location): ?int { @@ -35,6 +33,6 @@ class LocationDeletionService throw new HasActiveNodesException(trans('exceptions.locations.has_nodes')); } - return $this->repository->delete($location); + return $location->delete(); } } diff --git a/app/Services/Locations/LocationUpdateService.php b/app/Services/Locations/LocationUpdateService.php index cf24459e9..c394503ca 100644 --- a/app/Services/Locations/LocationUpdateService.php +++ b/app/Services/Locations/LocationUpdateService.php @@ -3,27 +3,17 @@ namespace Pterodactyl\Services\Locations; use Pterodactyl\Models\Location; -use Pterodactyl\Contracts\Repository\LocationRepositoryInterface; class LocationUpdateService { - /** - * LocationUpdateService constructor. - */ - public function __construct(protected LocationRepositoryInterface $repository) - { - } - /** * Update an existing location. * - * @throws \Pterodactyl\Exceptions\Model\DataValidationException - * @throws \Pterodactyl\Exceptions\Repository\RecordNotFoundException */ public function handle(Location|int $location, array $data): Location { $location = ($location instanceof Location) ? $location->id : $location; - return $this->repository->update($location, $data); + return $location->update($data); } }