From 34f718a8b19f1ad9b921166738d7c5ba0f7b0882 Mon Sep 17 00:00:00 2001 From: Matthew Penner Date: Wed, 20 May 2020 20:50:56 -0600 Subject: [PATCH] Finish mount admin view page, add unique index to migration --- .../Controllers/Admin/MountController.php | 97 ++++++++++ .../2020_05_20_234655_add_mounts_table.php | 4 + resources/views/admin/mounts/view.blade.php | 172 +++++++++++++++++- routes/admin.php | 6 + 4 files changed, 276 insertions(+), 3 deletions(-) diff --git a/app/Http/Controllers/Admin/MountController.php b/app/Http/Controllers/Admin/MountController.php index aa1bcf918..1132f68f4 100644 --- a/app/Http/Controllers/Admin/MountController.php +++ b/app/Http/Controllers/Admin/MountController.php @@ -2,6 +2,7 @@ namespace Pterodactyl\Http\Controllers\Admin; +use Illuminate\Http\Request; use Pterodactyl\Models\Mount; use Prologue\Alerts\AlertsMessageBag; use Pterodactyl\Exceptions\DisplayException; @@ -11,6 +12,8 @@ use Pterodactyl\Services\Mounts\MountCreationService; use Pterodactyl\Services\Mounts\MountDeletionService; 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 { @@ -19,6 +22,16 @@ class MountController extends Controller */ protected $alert; + /** + * @var \Pterodactyl\Contracts\Repository\NestRepositoryInterface + */ + protected $nestRepository; + + /** + * @var \Pterodactyl\Contracts\Repository\LocationRepositoryInterface + */ + protected $locationRepository; + /** * @var \Pterodactyl\Repositories\Eloquent\MountRepository */ @@ -43,6 +56,8 @@ class MountController extends Controller * MountController constructor. * * @param \Prologue\Alerts\AlertsMessageBag $alert + * @param \Pterodactyl\Contracts\Repository\NestRepositoryInterface $nestRepository + * @param \Pterodactyl\Contracts\Repository\LocationRepositoryInterface $locationRepository * @param \Pterodactyl\Repositories\Eloquent\MountRepository $repository * @param \Pterodactyl\Services\Mounts\MountCreationService $creationService * @param \Pterodactyl\Services\Mounts\MountDeletionService $deletionService @@ -50,12 +65,16 @@ class MountController extends Controller */ public function __construct( AlertsMessageBag $alert, + NestRepositoryInterface $nestRepository, + LocationRepositoryInterface $locationRepository, MountRepository $repository, MountCreationService $creationService, MountDeletionService $deletionService, MountUpdateService $updateService ) { $this->alert = $alert; + $this->nestRepository = $nestRepository; + $this->locationRepository = $locationRepository; $this->repository = $repository; $this->creationService = $creationService; $this->deletionService = $deletionService; @@ -84,8 +103,16 @@ class MountController extends Controller */ public function view($id) { + $nests = $this->nestRepository->all(); + $nests->load('eggs'); + + $locations = $this->locationRepository->all(); + $locations->load('nodes'); + return view('admin.mounts.view', [ 'mount' => $this->repository->getWithRelations($id), + 'nests' => $nests, + 'locations' => $locations, ]); } @@ -146,4 +173,74 @@ class MountController extends Controller return redirect()->route('admin.mounts.view', $mount->id); } + + /** + * Adds eggs to the mount's many to many relation. + * + * @param \Illuminate\Http\Request $request + * @param \Pterodactyl\Models\Mount $mount + * @return \Illuminate\Http\RedirectResponse + */ + public function addEggs(Request $request, Mount $mount) + { + $validatedData = $request->validate([ + 'eggs' => 'required|exists:eggs,id', + ]); + + $eggs = $validatedData['eggs'] ?? []; + if (sizeof($eggs) > 0) { + $mount->eggs()->attach(array_map('intval', $eggs)); + $this->alert->success('Mount was updated successfully.')->flash(); + } + + return redirect()->route('admin.mounts.view', $mount->id); + } + + /** + * Adds nodes to the mount's many to many relation. + * + * @param \Illuminate\Http\Request $request + * @param \Pterodactyl\Models\Mount $mount + * @return \Illuminate\Http\RedirectResponse + */ + public function addNodes(Request $request, Mount $mount) + { + $validatedData = $request->validate([ + 'nodes' => 'required|exists:nodes,id', + ]); + + $nodes = $validatedData['nodes'] ?? []; + if (sizeof($nodes) > 0) { + $mount->nodes()->attach(array_map('intval', $nodes)); + $this->alert->success('Mount was updated successfully.')->flash(); + } + + return redirect()->route('admin.mounts.view', $mount->id); + } + + /** + * Deletes an egg from the mount's many to many relation. + * + * @param \Pterodactyl\Models\Mount $mount + * @param int $egg_id + * @return \Illuminate\Http\Response + */ + public function deleteEgg(Mount $mount, int $egg_id) + { + $mount->eggs()->detach($egg_id); + return response('', 204); + } + + /** + * Deletes an node from the mount's many to many relation. + * + * @param \Pterodactyl\Models\Mount $mount + * @param int $node_id + * @return \Illuminate\Http\Response + */ + public function deleteNode(Mount $mount, int $node_id) + { + $mount->nodes()->detach($node_id); + return response('', 204); + } } diff --git a/database/migrations/2020_05_20_234655_add_mounts_table.php b/database/migrations/2020_05_20_234655_add_mounts_table.php index 3bc25d036..e9d169692 100644 --- a/database/migrations/2020_05_20_234655_add_mounts_table.php +++ b/database/migrations/2020_05_20_234655_add_mounts_table.php @@ -27,11 +27,15 @@ class AddMountsTable extends Migration Schema::create('egg_mount', function (Blueprint $table) { $table->integer('egg_id'); $table->char('mount_id', 36); + + $table->unique(['egg_id', 'mount_id']); }); Schema::create('mount_node', function (Blueprint $table) { $table->integer('node_id'); $table->char('mount_id', 36); + + $table->unique(['node_id', 'mount_id']); }); } diff --git a/resources/views/admin/mounts/view.blade.php b/resources/views/admin/mounts/view.blade.php index 2f72946e3..6fe95202f 100644 --- a/resources/views/admin/mounts/view.blade.php +++ b/resources/views/admin/mounts/view.blade.php @@ -24,7 +24,7 @@
-

Location Details

+

Mount Details

@@ -101,6 +101,10 @@

Eggs

+ +
+ +
@@ -108,12 +112,16 @@ ID Name + - @foreach($mount->eggs as $egg) + @foreach ($mount->eggs as $egg) {{ $egg->id }} {{ $egg->name }} + + + @endforeach @@ -123,6 +131,10 @@

Nodes

+ +
+ +
@@ -131,13 +143,17 @@ ID Name FQDN + - @foreach($mount->nodes as $node) + @foreach ($mount->nodes as $node) {{ $node->id }} {{ $node->name }} {{ $node->fqdn }} + + + @endforeach @@ -145,4 +161,154 @@
+ + + + +@endsection + +@section('footer-scripts') + @parent + + @endsection diff --git a/routes/admin.php b/routes/admin.php index 17f4d0a1f..d894703ff 100644 --- a/routes/admin.php +++ b/routes/admin.php @@ -178,7 +178,13 @@ Route::group(['prefix' => 'mounts'], function () { Route::get('/view/{mount}', 'MountController@view')->name('admin.mounts.view'); Route::post('/', 'MountController@create'); + Route::post('/{mount}/eggs', 'MountController@addEggs')->name('admin.mounts.eggs'); + Route::post('/{mount}/nodes', 'MountController@addNodes')->name('admin.mounts.nodes'); + Route::patch('/view/{mount}', 'MountController@update'); + + Route::delete('/{mount}/eggs/{egg_id}', 'MountController@deleteEgg'); + Route::delete('/{mount}/nodes/{node_id}', 'MountController@deleteNode'); }); /*