Remove egg repository

This commit is contained in:
Lance Pioch 2024-03-16 20:14:50 -04:00
parent 89477421e1
commit df37de4d2d
12 changed files with 33 additions and 184 deletions

View File

@ -1,33 +0,0 @@
<?php
namespace App\Contracts\Repository;
use App\Models\Egg;
use Illuminate\Database\Eloquent\Collection;
interface EggRepositoryInterface extends RepositoryInterface
{
/**
* Return an egg with the variables relation attached.
*
* @throws \App\Exceptions\Repository\RecordNotFoundException
*/
public function getWithVariables(int $id): Egg;
/**
* Return all eggs and their relations to be used in the daemon API.
*/
public function getAllWithCopyAttributes(): Collection;
/**
* Return an egg with the scriptFrom and configFrom relations loaded onto the model.
*/
public function getWithCopyAttributes(int|string $value, string $column = 'id'): Egg;
/**
* Return all the data needed to export a service.
*
* @throws \App\Exceptions\Repository\RecordNotFoundException
*/
public function getWithExportAttributes(int $id): Egg;
}

View File

@ -12,7 +12,6 @@ use App\Services\Eggs\EggUpdateService;
use App\Services\Eggs\EggCreationService; use App\Services\Eggs\EggCreationService;
use App\Services\Eggs\EggDeletionService; use App\Services\Eggs\EggDeletionService;
use App\Http\Requests\Admin\Egg\EggFormRequest; use App\Http\Requests\Admin\Egg\EggFormRequest;
use App\Contracts\Repository\EggRepositoryInterface;
class EggController extends Controller class EggController extends Controller
{ {
@ -23,7 +22,6 @@ class EggController extends Controller
protected AlertsMessageBag $alert, protected AlertsMessageBag $alert,
protected EggCreationService $creationService, protected EggCreationService $creationService,
protected EggDeletionService $deletionService, protected EggDeletionService $deletionService,
protected EggRepositoryInterface $repository,
protected EggUpdateService $updateService, protected EggUpdateService $updateService,
protected ViewFactory $view protected ViewFactory $view
) { ) {
@ -31,8 +29,6 @@ class EggController extends Controller
/** /**
* Render eggs listing page. * Render eggs listing page.
*
* @throws \App\Exceptions\Repository\RecordNotFoundException
*/ */
public function index(): View public function index(): View
{ {

View File

@ -9,7 +9,6 @@ use Prologue\Alerts\AlertsMessageBag;
use Illuminate\View\Factory as ViewFactory; use Illuminate\View\Factory as ViewFactory;
use App\Http\Controllers\Controller; use App\Http\Controllers\Controller;
use App\Services\Eggs\Scripts\InstallScriptService; use App\Services\Eggs\Scripts\InstallScriptService;
use App\Contracts\Repository\EggRepositoryInterface;
use App\Http\Requests\Admin\Egg\EggScriptFormRequest; use App\Http\Requests\Admin\Egg\EggScriptFormRequest;
class EggScriptController extends Controller class EggScriptController extends Controller
@ -19,7 +18,6 @@ class EggScriptController extends Controller
*/ */
public function __construct( public function __construct(
protected AlertsMessageBag $alert, protected AlertsMessageBag $alert,
protected EggRepositoryInterface $repository,
protected InstallScriptService $installScriptService, protected InstallScriptService $installScriptService,
protected ViewFactory $view protected ViewFactory $view
) { ) {
@ -30,15 +28,17 @@ class EggScriptController extends Controller
*/ */
public function index(int $egg): View public function index(int $egg): View
{ {
$egg = $this->repository->getWithCopyAttributes($egg); $egg = Egg::with('scriptFrom', 'configFrom')
$copy = $this->repository->findWhere([ ->where('id', $egg)
['copy_script_from', '=', null], ->firstOrFail();
['id', '!=', $egg],
]);
$rely = $this->repository->findWhere([ $copy = Egg::query()
['copy_script_from', '=', $egg->id], ->whereNull('copy_script_from')
]); ->where('nest_id', $egg->nest_id)
->whereNot('id', $egg->id)
->firstOrFail();
$rely = Egg::query()->where('copy_script_from', $egg->id)->firstOrFail();
return $this->view->make('admin.eggs.scripts', [ return $this->view->make('admin.eggs.scripts', [
'copyFromOptions' => $copy, 'copyFromOptions' => $copy,

View File

@ -9,7 +9,6 @@ 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 App\Http\Controllers\Controller; use App\Http\Controllers\Controller;
use App\Contracts\Repository\EggRepositoryInterface;
use App\Services\Eggs\Variables\VariableUpdateService; use App\Services\Eggs\Variables\VariableUpdateService;
use App\Http\Requests\Admin\Egg\EggVariableFormRequest; use App\Http\Requests\Admin\Egg\EggVariableFormRequest;
use App\Services\Eggs\Variables\VariableCreationService; use App\Services\Eggs\Variables\VariableCreationService;
@ -24,7 +23,6 @@ class EggVariableController extends Controller
protected AlertsMessageBag $alert, protected AlertsMessageBag $alert,
protected VariableCreationService $creationService, protected VariableCreationService $creationService,
protected VariableUpdateService $updateService, protected VariableUpdateService $updateService,
protected EggRepositoryInterface $repository,
protected EggVariableRepositoryInterface $variableRepository, protected EggVariableRepositoryInterface $variableRepository,
protected ViewFactory $view protected ViewFactory $view
) { ) {
@ -32,12 +30,10 @@ class EggVariableController extends Controller
/** /**
* Handle request to view the variables attached to an Egg. * Handle request to view the variables attached to an Egg.
*
* @throws \App\Exceptions\Repository\RecordNotFoundException
*/ */
public function view(int $egg): View public function view(int $egg): View
{ {
$egg = $this->repository->getWithVariables($egg); $egg = Egg::with('variables')->findOrFail($egg);
return $this->view->make('admin.eggs.variables', ['egg' => $egg]); return $this->view->make('admin.eggs.variables', ['egg' => $egg]);
} }

View File

@ -4,7 +4,6 @@ namespace App\Http\Controllers\Admin\Servers;
use App\Models\Egg; use App\Models\Egg;
use App\Models\Node; use App\Models\Node;
use App\Repositories\Eloquent\EggRepository;
use Illuminate\View\View; use Illuminate\View\View;
use Illuminate\Http\Request; use Illuminate\Http\Request;
use App\Models\Server; use App\Models\Server;
@ -27,7 +26,6 @@ class ServerViewController extends Controller
public function __construct( public function __construct(
private DatabaseHostRepository $databaseHostRepository, private DatabaseHostRepository $databaseHostRepository,
private MountRepository $mountRepository, private MountRepository $mountRepository,
private EggRepository $eggRepository,
private NodeRepository $nodeRepository, private NodeRepository $nodeRepository,
private EnvironmentService $environmentService, private EnvironmentService $environmentService,
private ViewFactory $view private ViewFactory $view

View File

@ -3,13 +3,11 @@
namespace App\Providers; namespace App\Providers;
use Illuminate\Support\ServiceProvider; use Illuminate\Support\ServiceProvider;
use App\Repositories\Eloquent\EggRepository;
use App\Repositories\Eloquent\NodeRepository; use App\Repositories\Eloquent\NodeRepository;
use App\Repositories\Eloquent\SessionRepository; use App\Repositories\Eloquent\SessionRepository;
use App\Repositories\Eloquent\SubuserRepository; use App\Repositories\Eloquent\SubuserRepository;
use App\Repositories\Eloquent\DatabaseRepository; use App\Repositories\Eloquent\DatabaseRepository;
use App\Repositories\Eloquent\SettingsRepository; use App\Repositories\Eloquent\SettingsRepository;
use App\Contracts\Repository\EggRepositoryInterface;
use App\Repositories\Eloquent\EggVariableRepository; use App\Repositories\Eloquent\EggVariableRepository;
use App\Contracts\Repository\NodeRepositoryInterface; use App\Contracts\Repository\NodeRepositoryInterface;
use App\Repositories\Eloquent\DatabaseHostRepository; use App\Repositories\Eloquent\DatabaseHostRepository;
@ -30,7 +28,6 @@ class RepositoryServiceProvider extends ServiceProvider
// Eloquent Repositories // Eloquent Repositories
$this->app->bind(DatabaseRepositoryInterface::class, DatabaseRepository::class); $this->app->bind(DatabaseRepositoryInterface::class, DatabaseRepository::class);
$this->app->bind(DatabaseHostRepositoryInterface::class, DatabaseHostRepository::class); $this->app->bind(DatabaseHostRepositoryInterface::class, DatabaseHostRepository::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(NodeRepositoryInterface::class, NodeRepository::class); $this->app->bind(NodeRepositoryInterface::class, NodeRepository::class);
$this->app->bind(SessionRepositoryInterface::class, SessionRepository::class); $this->app->bind(SessionRepositoryInterface::class, SessionRepository::class);

View File

@ -1,75 +0,0 @@
<?php
namespace App\Repositories\Eloquent;
use App\Models\Egg;
use Webmozart\Assert\Assert;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Database\Eloquent\ModelNotFoundException;
use App\Contracts\Repository\EggRepositoryInterface;
use App\Exceptions\Repository\RecordNotFoundException;
class EggRepository extends EloquentRepository implements EggRepositoryInterface
{
/**
* Return the model backing this repository.
*/
public function model(): string
{
return Egg::class;
}
/**
* Return an egg with the variables relation attached.
*
* @throws \App\Exceptions\Repository\RecordNotFoundException
*/
public function getWithVariables(int $id): Egg
{
try {
return $this->getBuilder()->with('variables')->findOrFail($id, $this->getColumns());
} catch (ModelNotFoundException) {
throw new RecordNotFoundException();
}
}
/**
* Return all eggs and their relations to be used in the daemon API.
*/
public function getAllWithCopyAttributes(): Collection
{
return $this->getBuilder()->with('scriptFrom', 'configFrom')->get($this->getColumns());
}
/**
* Return an egg with the scriptFrom and configFrom relations loaded onto the model.
*
* @param int|string $value
*
* @throws \App\Exceptions\Repository\RecordNotFoundException
*/
public function getWithCopyAttributes($value, string $column = 'id'): Egg
{
Assert::true(is_digit($value) || is_string($value), 'First argument passed to getWithCopyAttributes must be an integer or string, received %s.');
try {
return $this->getBuilder()->with('scriptFrom', 'configFrom')->where($column, '=', $value)->firstOrFail($this->getColumns());
} catch (ModelNotFoundException) {
throw new RecordNotFoundException();
}
}
/**
* Return all the data needed to export a service.
*
* @throws \App\Exceptions\Repository\RecordNotFoundException
*/
public function getWithExportAttributes(int $id): Egg
{
try {
return $this->getBuilder()->with('scriptFrom', 'configFrom', 'variables')->findOrFail($id, $this->getColumns());
} catch (ModelNotFoundException) {
throw new RecordNotFoundException();
}
}
}

View File

@ -4,7 +4,6 @@ namespace App\Services\Eggs;
use Ramsey\Uuid\Uuid; use Ramsey\Uuid\Uuid;
use App\Models\Egg; use App\Models\Egg;
use App\Contracts\Repository\EggRepositoryInterface;
use Illuminate\Contracts\Config\Repository as ConfigRepository; use Illuminate\Contracts\Config\Repository as ConfigRepository;
use App\Exceptions\Service\Egg\NoParentConfigurationFoundException; use App\Exceptions\Service\Egg\NoParentConfigurationFoundException;
@ -13,7 +12,7 @@ class EggCreationService
/** /**
* EggCreationService constructor. * EggCreationService constructor.
*/ */
public function __construct(private ConfigRepository $config, private EggRepositoryInterface $repository) public function __construct(private ConfigRepository $config)
{ {
} }
@ -27,18 +26,19 @@ class EggCreationService
{ {
$data['config_from'] = array_get($data, 'config_from'); $data['config_from'] = array_get($data, 'config_from');
if (!is_null($data['config_from'])) { if (!is_null($data['config_from'])) {
$results = $this->repository->findCountWhere([ $results = Egg::query()
['id', '=', array_get($data, 'config_from')], ->where('nest_id', array_get($data, 'nest_id'))
]); ->where('id', array_get($data, 'config_from'))
->count();
if ($results !== 1) { if ($results !== 1) {
throw new NoParentConfigurationFoundException(trans('exceptions.egg.invalid_copy_id')); throw new NoParentConfigurationFoundException(trans('exceptions.egg.invalid_copy_id'));
} }
} }
return $this->repository->create(array_merge($data, [ return Egg::query()->create(array_merge($data, [
'uuid' => Uuid::uuid4()->toString(), 'uuid' => Uuid::uuid4()->toString(),
'author' => $this->config->get('panel.service.author'), 'author' => $this->config->get('pterodactyl.service.author'),
]), true, true); ]));
} }
} }

View File

@ -2,21 +2,13 @@
namespace App\Services\Eggs; namespace App\Services\Eggs;
use App\Contracts\Repository\EggRepositoryInterface;
use App\Exceptions\Service\Egg\HasChildrenException; use App\Exceptions\Service\Egg\HasChildrenException;
use App\Exceptions\Service\HasActiveServersException; use App\Exceptions\Service\HasActiveServersException;
use App\Models\Egg;
use App\Models\Server; use App\Models\Server;
class EggDeletionService class EggDeletionService
{ {
/**
* EggDeletionService constructor.
*/
public function __construct(
protected EggRepositoryInterface $repository
) {
}
/** /**
* Delete an Egg from the database if it has no active servers attached to it. * Delete an Egg from the database if it has no active servers attached to it.
* *
@ -29,11 +21,13 @@ class EggDeletionService
throw new HasActiveServersException(trans('exceptions.egg.delete_has_servers')); throw new HasActiveServersException(trans('exceptions.egg.delete_has_servers'));
} }
$children = $this->repository->findCountWhere([['config_from', '=', $egg]]); $children = Egg::query()->where('config_from', $egg)->count();
if ($children > 0) { if ($children > 0) {
throw new HasChildrenException(trans('exceptions.egg.has_children')); throw new HasChildrenException(trans('exceptions.egg.has_children'));
} }
return $this->repository->delete($egg); $egg = Egg::query()->findOrFail($egg);
return $egg->delete();
} }
} }

View File

@ -3,18 +3,10 @@
namespace App\Services\Eggs; namespace App\Services\Eggs;
use App\Models\Egg; use App\Models\Egg;
use App\Contracts\Repository\EggRepositoryInterface;
use App\Exceptions\Service\Egg\NoParentConfigurationFoundException; use App\Exceptions\Service\Egg\NoParentConfigurationFoundException;
class EggUpdateService class EggUpdateService
{ {
/**
* EggUpdateService constructor.
*/
public function __construct(protected EggRepositoryInterface $repository)
{
}
/** /**
* Update an egg. * Update an egg.
* *
@ -24,10 +16,12 @@ class EggUpdateService
*/ */
public function handle(Egg $egg, array $data): void public function handle(Egg $egg, array $data): void
{ {
if (!is_null(array_get($data, 'config_from'))) { $eggId = array_get($data, 'config_from');
$results = $this->repository->findCountWhere([ if ($eggId) {
['id', '=', array_get($data, 'config_from')], $results = Egg::query()
]); ->where('nest_id', $egg->nest_id)
->where('id', $eggId)
->count();
if ($results !== 1) { if ($results !== 1) {
throw new NoParentConfigurationFoundException(trans('exceptions.egg.invalid_copy_id')); throw new NoParentConfigurationFoundException(trans('exceptions.egg.invalid_copy_id'));
@ -38,6 +32,6 @@ class EggUpdateService
// in said UI, remove this so that you can actually update the denylist. // in said UI, remove this so that you can actually update the denylist.
unset($data['file_denylist']); unset($data['file_denylist']);
$this->repository->withoutFreshModel()->update($egg->id, $data); $egg->update($data);
} }
} }

View File

@ -3,26 +3,15 @@
namespace App\Services\Eggs\Scripts; namespace App\Services\Eggs\Scripts;
use App\Models\Egg; use App\Models\Egg;
use App\Contracts\Repository\EggRepositoryInterface;
class InstallScriptService class InstallScriptService
{ {
/**
* InstallScriptService constructor.
*/
public function __construct(protected EggRepositoryInterface $repository)
{
}
/** /**
* Modify the install script for a given Egg. * Modify the install script for a given Egg.
*
* @throws \App\Exceptions\Model\DataValidationException
* @throws \App\Exceptions\Repository\RecordNotFoundException
*/ */
public function handle(Egg $egg, array $data): void public function handle(Egg $egg, array $data): void
{ {
$this->repository->withoutFreshModel()->update($egg->id, [ $egg->update([
'script_install' => array_get($data, 'script_install'), 'script_install' => array_get($data, 'script_install'),
'script_is_privileged' => array_get($data, 'script_is_privileged', 1), 'script_is_privileged' => array_get($data, 'script_is_privileged', 1),
'script_entry' => array_get($data, 'script_entry'), 'script_entry' => array_get($data, 'script_entry'),

View File

@ -6,16 +6,9 @@ use Carbon\Carbon;
use App\Models\Egg; use App\Models\Egg;
use Illuminate\Support\Collection; use Illuminate\Support\Collection;
use App\Models\EggVariable; use App\Models\EggVariable;
use App\Contracts\Repository\EggRepositoryInterface;
class EggExporterService class EggExporterService
{ {
/**
* EggExporterService constructor.
*/
public function __construct(protected EggRepositoryInterface $repository)
{
}
/** /**
* Return a JSON representation of an egg and its variables. * Return a JSON representation of an egg and its variables.
@ -24,7 +17,7 @@ class EggExporterService
*/ */
public function handle(int $egg): string public function handle(int $egg): string
{ {
$egg = $this->repository->getWithExportAttributes($egg); $egg = Egg::with(['scriptFrom', 'configFrom', 'variables'])->findOrFail($egg);
$struct = [ $struct = [
'_comment' => 'DO NOT EDIT: FILE GENERATED AUTOMATICALLY BY PANEL', '_comment' => 'DO NOT EDIT: FILE GENERATED AUTOMATICALLY BY PANEL',