Remove service

This commit is contained in:
Lance Pioch 2024-03-19 04:30:26 -04:00
parent 139c70e52b
commit c01330dc06
4 changed files with 19 additions and 39 deletions

View File

@ -45,10 +45,8 @@ class DatabaseController extends Controller
/**
* Display database host to user.
*/
public function view(int $host): View
public function view(DatabaseHost $host): View
{
/** @var DatabaseHost $host */
$host = DatabaseHost::query()->findOrFail($host);
$databases = $host->databases()->with('server')->paginate(25);
return view('admin.databases.view', [

View File

@ -10,7 +10,6 @@ use Illuminate\View\Factory as ViewFactory;
use App\Http\Controllers\Controller;
use App\Services\Eggs\EggUpdateService;
use App\Services\Eggs\EggCreationService;
use App\Services\Eggs\EggDeletionService;
use App\Http\Requests\Admin\Egg\EggFormRequest;
class EggController extends Controller
@ -21,7 +20,6 @@ class EggController extends Controller
public function __construct(
protected AlertsMessageBag $alert,
protected EggCreationService $creationService,
protected EggDeletionService $deletionService,
protected EggUpdateService $updateService,
protected ViewFactory $view
) {
@ -105,7 +103,8 @@ class EggController extends Controller
*/
public function destroy(Egg $egg): RedirectResponse
{
$this->deletionService->handle($egg->id);
$egg->delete();
$this->alert->success(trans('admin/eggs.notices.deleted'))->flash();
return redirect()->route('admin.eggs.view', $egg->id);

View File

@ -2,6 +2,8 @@
namespace App\Models;
use App\Exceptions\Service\Egg\HasChildrenException;
use App\Exceptions\Service\HasActiveServersException;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
@ -140,6 +142,15 @@ class Egg extends Model
'update_url' => null,
];
protected static function booted(): void
{
static::deleting(function (self $egg) {
throw_if($egg->servers()->count(), new HasActiveServersException(trans('exceptions.egg.delete_has_servers')));
throw_if($egg->children()->count(), new HasChildrenException(trans('exceptions.egg.has_children')));
});
}
/**
* Returns the install script for the egg; if egg is copying from another
* it will return the copied script.
@ -277,6 +288,11 @@ class Egg extends Model
return $this->belongsTo(self::class, 'copy_script_from');
}
public function children(): HasMany
{
return $this->hasMany(self::class, 'config_from');
}
/**
* Get the parent egg from which to copy configuration settings.
*/

View File

@ -1,33 +0,0 @@
<?php
namespace App\Services\Eggs;
use App\Exceptions\Service\Egg\HasChildrenException;
use App\Exceptions\Service\HasActiveServersException;
use App\Models\Egg;
use App\Models\Server;
class EggDeletionService
{
/**
* Delete an Egg from the database if it has no active servers attached to it.
*
* @throws \App\Exceptions\Service\HasActiveServersException
* @throws \App\Exceptions\Service\Egg\HasChildrenException
*/
public function handle(int $egg): int
{
if (Server::query()->where('egg_id', $egg)->count()) {
throw new HasActiveServersException(trans('exceptions.egg.delete_has_servers'));
}
$children = Egg::query()->where('config_from', $egg)->count();
if ($children > 0) {
throw new HasChildrenException(trans('exceptions.egg.has_children'));
}
$egg = Egg::query()->findOrFail($egg);
return (int) $egg->delete();
}
}