From 43e419fd7e3eeb9d4f9534a926c36c61f37e0bfd Mon Sep 17 00:00:00 2001 From: Lance Pioch Date: Sun, 23 Oct 2022 20:15:11 -0400 Subject: [PATCH] Replace nest repository --- .../Repository/NestRepositoryInterface.php | 30 -------- .../Controllers/Admin/MountController.php | 2 - .../Controllers/Admin/Nests/EggController.php | 10 +-- .../Admin/Nests/NestController.php | 11 ++- .../Admin/Servers/CreateServerController.php | 5 +- .../Admin/Servers/ServerViewController.php | 4 +- .../Controllers/Admin/ServersController.php | 2 - .../Api/Application/Nests/NestController.php | 5 +- app/Providers/RepositoryServiceProvider.php | 3 - app/Repositories/Eloquent/NestRepository.php | 77 ------------------- app/Services/Nests/NestCreationService.php | 10 ++- app/Services/Nests/NestDeletionService.php | 14 ++-- app/Services/Nests/NestUpdateService.php | 14 +--- database/Seeders/NestSeeder.php | 21 ++--- .../Application/Nests/NestControllerTest.php | 18 +---- 15 files changed, 40 insertions(+), 186 deletions(-) delete mode 100644 app/Contracts/Repository/NestRepositoryInterface.php delete mode 100644 app/Repositories/Eloquent/NestRepository.php diff --git a/app/Contracts/Repository/NestRepositoryInterface.php b/app/Contracts/Repository/NestRepositoryInterface.php deleted file mode 100644 index 2f80bedd5..000000000 --- a/app/Contracts/Repository/NestRepositoryInterface.php +++ /dev/null @@ -1,30 +0,0 @@ -nestRepository->getWithEggs(); + $nests = Nest::with('eggs.variables')->get(); JavaScript::put(['nests' => $nests->keyBy('id')]); return $this->view->make('admin.eggs.new', ['nests' => $nests]); @@ -81,7 +79,7 @@ class EggController extends Controller * Handle request to update an Egg. * * @throws \Pterodactyl\Exceptions\Model\DataValidationException - * @throws \Pterodactyl\Exceptions\Repository\RecordNotFoundException + * @throws RecordNotFoundException * @throws \Pterodactyl\Exceptions\Service\Egg\NoParentConfigurationFoundException */ public function update(EggFormRequest $request, Egg $egg): RedirectResponse diff --git a/app/Http/Controllers/Admin/Nests/NestController.php b/app/Http/Controllers/Admin/Nests/NestController.php index 037dd0943..8c4f36946 100644 --- a/app/Http/Controllers/Admin/Nests/NestController.php +++ b/app/Http/Controllers/Admin/Nests/NestController.php @@ -7,10 +7,10 @@ use Illuminate\Http\RedirectResponse; use Prologue\Alerts\AlertsMessageBag; use Illuminate\View\Factory as ViewFactory; use Pterodactyl\Http\Controllers\Controller; +use Pterodactyl\Models\Nest; use Pterodactyl\Services\Nests\NestUpdateService; use Pterodactyl\Services\Nests\NestCreationService; use Pterodactyl\Services\Nests\NestDeletionService; -use Pterodactyl\Contracts\Repository\NestRepositoryInterface; use Pterodactyl\Http\Requests\Admin\Nest\StoreNestFormRequest; class NestController extends Controller @@ -22,7 +22,6 @@ class NestController extends Controller protected AlertsMessageBag $alert, protected NestCreationService $nestCreationService, protected NestDeletionService $nestDeletionService, - protected NestRepositoryInterface $repository, protected NestUpdateService $nestUpdateService, protected ViewFactory $view ) { @@ -31,12 +30,13 @@ class NestController extends Controller /** * Render nest listing page. * - * @throws \Pterodactyl\Exceptions\Repository\RecordNotFoundException */ public function index(): View { + $nests = Nest::query()->withCount(['eggs', 'servers']); + return $this->view->make('admin.nests.index', [ - 'nests' => $this->repository->getWithCounts(), + 'nests' => $nests, ]); } @@ -64,12 +64,11 @@ class NestController extends Controller /** * Return details about a nest including all the eggs and servers per egg. * - * @throws \Pterodactyl\Exceptions\Repository\RecordNotFoundException */ public function view(int $nest): View { return $this->view->make('admin.nests.view', [ - 'nest' => $this->repository->getWithEggServers($nest), + 'nest' => Nest::with('eggs.servers')->findOrFail($nest), ]); } diff --git a/app/Http/Controllers/Admin/Servers/CreateServerController.php b/app/Http/Controllers/Admin/Servers/CreateServerController.php index c7a1653ad..bdde98caa 100644 --- a/app/Http/Controllers/Admin/Servers/CreateServerController.php +++ b/app/Http/Controllers/Admin/Servers/CreateServerController.php @@ -4,13 +4,13 @@ namespace Pterodactyl\Http\Controllers\Admin\Servers; use JavaScript; use Illuminate\View\View; +use Pterodactyl\Models\Nest; use Pterodactyl\Models\Node; use Pterodactyl\Models\Location; use Illuminate\Http\RedirectResponse; use Prologue\Alerts\AlertsMessageBag; use Illuminate\View\Factory as ViewFactory; use Pterodactyl\Http\Controllers\Controller; -use Pterodactyl\Repositories\Eloquent\NestRepository; use Pterodactyl\Repositories\Eloquent\NodeRepository; use Pterodactyl\Http\Requests\Admin\ServerFormRequest; use Pterodactyl\Services\Servers\ServerCreationService; @@ -22,7 +22,6 @@ class CreateServerController extends Controller */ public function __construct( private AlertsMessageBag $alert, - private NestRepository $nestRepository, private NodeRepository $nodeRepository, private ServerCreationService $creationService, private ViewFactory $view @@ -43,7 +42,7 @@ class CreateServerController extends Controller return redirect()->route('admin.nodes'); } - $nests = $this->nestRepository->getWithEggs(); + $nests = Nest::with('eggs.variables')->get(); JavaScript::put([ 'nodeData' => $this->nodeRepository->getNodesForServerCreation(), diff --git a/app/Http/Controllers/Admin/Servers/ServerViewController.php b/app/Http/Controllers/Admin/Servers/ServerViewController.php index 7cf64a2f5..3d1d39f1f 100644 --- a/app/Http/Controllers/Admin/Servers/ServerViewController.php +++ b/app/Http/Controllers/Admin/Servers/ServerViewController.php @@ -11,7 +11,6 @@ use Pterodactyl\Exceptions\DisplayException; use Pterodactyl\Http\Controllers\Controller; use Pterodactyl\Services\Servers\EnvironmentService; use Illuminate\Contracts\View\Factory as ViewFactory; -use Pterodactyl\Repositories\Eloquent\NestRepository; use Pterodactyl\Repositories\Eloquent\NodeRepository; use Pterodactyl\Repositories\Eloquent\MountRepository; use Pterodactyl\Repositories\Eloquent\ServerRepository; @@ -30,7 +29,6 @@ class ServerViewController extends Controller private DatabaseHostRepository $databaseHostRepository, private LocationRepository $locationRepository, private MountRepository $mountRepository, - private NestRepository $nestRepository, private NodeRepository $nodeRepository, private ServerRepository $repository, private EnvironmentService $environmentService, @@ -75,7 +73,7 @@ class ServerViewController extends Controller */ public function startup(Request $request, Server $server): View { - $nests = $this->nestRepository->getWithEggs(); + $nests = Nest::with('eggs.variables')->get(); $variables = $this->environmentService->handle($server); $this->plainInject([ diff --git a/app/Http/Controllers/Admin/ServersController.php b/app/Http/Controllers/Admin/ServersController.php index 3d2c55ac4..98bb67524 100644 --- a/app/Http/Controllers/Admin/ServersController.php +++ b/app/Http/Controllers/Admin/ServersController.php @@ -24,7 +24,6 @@ use Pterodactyl\Services\Servers\BuildModificationService; use Pterodactyl\Services\Databases\DatabasePasswordService; use Pterodactyl\Services\Servers\DetailsModificationService; use Pterodactyl\Services\Servers\StartupModificationService; -use Pterodactyl\Contracts\Repository\NestRepositoryInterface; use Pterodactyl\Repositories\Eloquent\DatabaseHostRepository; use Pterodactyl\Services\Databases\DatabaseManagementService; use Illuminate\Contracts\Config\Repository as ConfigRepository; @@ -54,7 +53,6 @@ class ServersController extends Controller protected ReinstallServerService $reinstallService, protected ServerRepositoryInterface $repository, protected MountRepository $mountRepository, - protected NestRepositoryInterface $nestRepository, protected ServerConfigurationStructureService $serverConfigurationStructureService, protected StartupModificationService $startupModificationService, protected SuspensionService $suspensionService diff --git a/app/Http/Controllers/Api/Application/Nests/NestController.php b/app/Http/Controllers/Api/Application/Nests/NestController.php index f0044f53c..20f6172be 100644 --- a/app/Http/Controllers/Api/Application/Nests/NestController.php +++ b/app/Http/Controllers/Api/Application/Nests/NestController.php @@ -3,7 +3,6 @@ namespace Pterodactyl\Http\Controllers\Api\Application\Nests; use Pterodactyl\Models\Nest; -use Pterodactyl\Contracts\Repository\NestRepositoryInterface; use Pterodactyl\Transformers\Api\Application\NestTransformer; use Pterodactyl\Http\Requests\Api\Application\Nests\GetNestsRequest; use Pterodactyl\Http\Controllers\Api\Application\ApplicationApiController; @@ -13,7 +12,7 @@ class NestController extends ApplicationApiController /** * NestController constructor. */ - public function __construct(private NestRepositoryInterface $repository) + public function __construct() { parent::__construct(); } @@ -23,7 +22,7 @@ class NestController extends ApplicationApiController */ public function index(GetNestsRequest $request): array { - $nests = $this->repository->paginated($request->query('per_page') ?? 50); + $nests = Nest::query()->paginate($request->query('per_page') ?? 50); return $this->fractal->collection($nests) ->transformWith($this->getTransformer(NestTransformer::class)) diff --git a/app/Providers/RepositoryServiceProvider.php b/app/Providers/RepositoryServiceProvider.php index 8a0434f52..48ed58f94 100644 --- a/app/Providers/RepositoryServiceProvider.php +++ b/app/Providers/RepositoryServiceProvider.php @@ -4,7 +4,6 @@ namespace Pterodactyl\Providers; use Illuminate\Support\ServiceProvider; use Pterodactyl\Repositories\Eloquent\EggRepository; -use Pterodactyl\Repositories\Eloquent\NestRepository; use Pterodactyl\Repositories\Eloquent\NodeRepository; use Pterodactyl\Repositories\Eloquent\TaskRepository; use Pterodactyl\Repositories\Eloquent\UserRepository; @@ -19,7 +18,6 @@ use Pterodactyl\Repositories\Eloquent\SettingsRepository; use Pterodactyl\Repositories\Eloquent\AllocationRepository; use Pterodactyl\Contracts\Repository\EggRepositoryInterface; use Pterodactyl\Repositories\Eloquent\EggVariableRepository; -use Pterodactyl\Contracts\Repository\NestRepositoryInterface; use Pterodactyl\Contracts\Repository\NodeRepositoryInterface; use Pterodactyl\Contracts\Repository\TaskRepositoryInterface; use Pterodactyl\Contracts\Repository\UserRepositoryInterface; @@ -53,7 +51,6 @@ class RepositoryServiceProvider extends ServiceProvider $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); $this->app->bind(ServerRepositoryInterface::class, ServerRepository::class); diff --git a/app/Repositories/Eloquent/NestRepository.php b/app/Repositories/Eloquent/NestRepository.php deleted file mode 100644 index f472e4b80..000000000 --- a/app/Repositories/Eloquent/NestRepository.php +++ /dev/null @@ -1,77 +0,0 @@ -getBuilder()->with('eggs', 'eggs.variables'); - - if (!is_null($id)) { - $instance = $instance->find($id, $this->getColumns()); - if (!$instance) { - throw new RecordNotFoundException(); - } - - return $instance; - } - - return $instance->get($this->getColumns()); - } - - /** - * Return a nest or all nests and the count of eggs and servers for that nest. - * - * @throws \Pterodactyl\Exceptions\Repository\RecordNotFoundException - */ - public function getWithCounts(int $id = null): Collection|Nest - { - $instance = $this->getBuilder()->withCount(['eggs', 'servers']); - - if (!is_null($id)) { - $instance = $instance->find($id, $this->getColumns()); - if (!$instance) { - throw new RecordNotFoundException(); - } - - return $instance; - } - - return $instance->get($this->getColumns()); - } - - /** - * Return a nest along with its associated eggs and the servers relation on those eggs. - * - * @throws \Pterodactyl\Exceptions\Repository\RecordNotFoundException - */ - public function getWithEggServers(int $id): Nest - { - $instance = $this->getBuilder()->with('eggs.servers')->find($id, $this->getColumns()); - if (!$instance) { - throw new RecordNotFoundException(); - } - - /* @var Nest $instance */ - return $instance; - } -} diff --git a/app/Services/Nests/NestCreationService.php b/app/Services/Nests/NestCreationService.php index c3513aefe..c3a5dab3e 100644 --- a/app/Services/Nests/NestCreationService.php +++ b/app/Services/Nests/NestCreationService.php @@ -4,7 +4,6 @@ namespace Pterodactyl\Services\Nests; use Ramsey\Uuid\Uuid; use Pterodactyl\Models\Nest; -use Pterodactyl\Contracts\Repository\NestRepositoryInterface; use Illuminate\Contracts\Config\Repository as ConfigRepository; class NestCreationService @@ -12,7 +11,7 @@ class NestCreationService /** * NestCreationService constructor. */ - public function __construct(private ConfigRepository $config, private NestRepositoryInterface $repository) + public function __construct(private ConfigRepository $config) { } @@ -23,11 +22,14 @@ class NestCreationService */ public function handle(array $data, string $author = null): Nest { - return $this->repository->create([ + /** @var Nest $nest */ + $nest = Nest::query()->create([ 'uuid' => Uuid::uuid4()->toString(), 'author' => $author ?? $this->config->get('pterodactyl.service.author'), 'name' => array_get($data, 'name'), 'description' => array_get($data, 'description'), - ], true, true); + ]); + + return $nest; } } diff --git a/app/Services/Nests/NestDeletionService.php b/app/Services/Nests/NestDeletionService.php index 6babf45c3..4a5402f1e 100644 --- a/app/Services/Nests/NestDeletionService.php +++ b/app/Services/Nests/NestDeletionService.php @@ -2,7 +2,7 @@ namespace Pterodactyl\Services\Nests; -use Pterodactyl\Contracts\Repository\NestRepositoryInterface; +use Pterodactyl\Models\Nest; use Pterodactyl\Exceptions\Service\HasActiveServersException; use Pterodactyl\Contracts\Repository\ServerRepositoryInterface; @@ -11,16 +11,14 @@ class NestDeletionService /** * NestDeletionService constructor. */ - public function __construct( - protected ServerRepositoryInterface $serverRepository, - protected NestRepositoryInterface $repository - ) { + public function __construct(protected ServerRepositoryInterface $serverRepository) + { } /** * Delete a nest from the system only if there are no servers attached to it. * - * @throws \Pterodactyl\Exceptions\Service\HasActiveServersException + * @throws HasActiveServersException */ public function handle(int $nest): int { @@ -29,6 +27,8 @@ class NestDeletionService throw new HasActiveServersException(trans('exceptions.nest.delete_has_servers')); } - return $this->repository->delete($nest); + $nest = Nest::query()->findOrFail($nest); + + return $nest->delete(); } } diff --git a/app/Services/Nests/NestUpdateService.php b/app/Services/Nests/NestUpdateService.php index 772421e42..8d76b225b 100644 --- a/app/Services/Nests/NestUpdateService.php +++ b/app/Services/Nests/NestUpdateService.php @@ -2,22 +2,13 @@ namespace Pterodactyl\Services\Nests; -use Pterodactyl\Contracts\Repository\NestRepositoryInterface; +use Pterodactyl\Models\Nest; class NestUpdateService { - /** - * NestUpdateService constructor. - */ - public function __construct(protected NestRepositoryInterface $repository) - { - } - /** * Update a nest and prevent changing the author once it is set. * - * @throws \Pterodactyl\Exceptions\Model\DataValidationException - * @throws \Pterodactyl\Exceptions\Repository\RecordNotFoundException */ public function handle(int $nest, array $data): void { @@ -25,6 +16,7 @@ class NestUpdateService unset($data['author']); } - $this->repository->withoutFreshModel()->update($nest, $data); + $nest = Nest::query()->findOrFail($nest); + $nest->update($data); } } diff --git a/database/Seeders/NestSeeder.php b/database/Seeders/NestSeeder.php index eae7ae21b..9017d0d1b 100644 --- a/database/Seeders/NestSeeder.php +++ b/database/Seeders/NestSeeder.php @@ -3,8 +3,8 @@ namespace Database\Seeders; use Illuminate\Database\Seeder; +use Pterodactyl\Models\Nest; use Pterodactyl\Services\Nests\NestCreationService; -use Pterodactyl\Contracts\Repository\NestRepositoryInterface; class NestSeeder extends Seeder { @@ -13,20 +13,12 @@ class NestSeeder extends Seeder */ private $creationService; - /** - * @var \Pterodactyl\Contracts\Repository\NestRepositoryInterface - */ - private $repository; - /** * NestSeeder constructor. */ - public function __construct( - NestCreationService $creationService, - NestRepositoryInterface $repository - ) { + public function __construct(NestCreationService $creationService) + { $this->creationService = $creationService; - $this->repository = $repository; } /** @@ -36,9 +28,10 @@ class NestSeeder extends Seeder */ public function run() { - $items = $this->repository->findWhere([ - 'author' => 'support@pterodactyl.io', - ])->keyBy('name')->toArray(); + $items = Nest::query() + ->where('author', 'support@pterodactyl.io') + ->get() + ->keyBy('name')->toArray(); $this->createMinecraftNest(array_get($items, 'Minecraft')); $this->createSourceEngineNest(array_get($items, 'Source Engine')); diff --git a/tests/Integration/Api/Application/Nests/NestControllerTest.php b/tests/Integration/Api/Application/Nests/NestControllerTest.php index 799fc18ac..9ae74a7de 100644 --- a/tests/Integration/Api/Application/Nests/NestControllerTest.php +++ b/tests/Integration/Api/Application/Nests/NestControllerTest.php @@ -3,31 +3,19 @@ namespace Pterodactyl\Tests\Integration\Api\Application\Nests; use Illuminate\Http\Response; -use Pterodactyl\Contracts\Repository\NestRepositoryInterface; +use Pterodactyl\Models\Nest; use Pterodactyl\Transformers\Api\Application\NestTransformer; use Pterodactyl\Tests\Integration\Api\Application\ApplicationApiIntegrationTestCase; class NestControllerTest extends ApplicationApiIntegrationTestCase { - private NestRepositoryInterface $repository; - - /** - * Setup tests. - */ - public function setUp(): void - { - parent::setUp(); - - $this->repository = $this->app->make(NestRepositoryInterface::class); - } - /** * Test that the expected nests are returned by the request. */ public function testNestResponse() { - /** @var \Pterodactyl\Models\Nest[] $nests */ - $nests = $this->repository->all(); + /** @var Nest[] $nests */ + $nests = Nest::all(); $response = $this->getJson('/api/application/nests'); $response->assertStatus(Response::HTTP_OK);