Replace location repository

This commit is contained in:
Lance Pioch 2022-10-23 04:25:49 -04:00
parent 860b2d890b
commit 126c9e940f
13 changed files with 33 additions and 154 deletions

View file

@ -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()

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 Illuminate\View\Factory as ViewFactory;
use Pterodactyl\Http\Controllers\Controller;
use Pterodactyl\Models\Location;
use Pterodactyl\Services\Databases\Hosts\HostUpdateService;
use Pterodactyl\Http\Requests\Admin\DatabaseHostFormRequest;
use Pterodactyl\Services\Databases\Hosts\HostCreationService;
use Pterodactyl\Services\Databases\Hosts\HostDeletionService;
use Pterodactyl\Contracts\Repository\DatabaseRepositoryInterface;
use Pterodactyl\Contracts\Repository\LocationRepositoryInterface;
use Pterodactyl\Contracts\Repository\DatabaseHostRepositoryInterface;
class DatabaseController extends Controller
@ -30,7 +30,6 @@ class DatabaseController extends Controller
private HostCreationService $creationService,
private HostDeletionService $deletionService,
private HostUpdateService $updateService,
private LocationRepositoryInterface $locationRepository,
private ViewFactory $view
) {
}
@ -40,8 +39,10 @@ class DatabaseController extends Controller
*/
public function index(): View
{
$locations = Location::with('nodes')->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),
]);

View file

@ -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,
]);
}

View file

@ -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
) {

View file

@ -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(),
]);
}

View file

@ -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();

View file

@ -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,
]);
}

View file

@ -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);

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;
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;
}
}

View file

@ -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();
}
}

View file

@ -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);
}
}