From 0db7debb4677d6d08a7b4dcfe6d186eb36729776 Mon Sep 17 00:00:00 2001 From: Matthew Penner Date: Wed, 20 May 2020 19:35:30 -0600 Subject: [PATCH] Add mount update and deletion services, add MountController@update and MountController@delete --- .../Controllers/Admin/MountController.php | 71 +++++++++++++++++-- app/Services/Mounts/MountDeletionService.php | 40 +++++++++++ app/Services/Mounts/MountUpdateService.php | 41 +++++++++++ resources/views/admin/mounts/index.blade.php | 4 +- resources/views/admin/mounts/view.blade.php | 8 +-- routes/admin.php | 1 + 6 files changed, 154 insertions(+), 11 deletions(-) create mode 100644 app/Services/Mounts/MountDeletionService.php create mode 100644 app/Services/Mounts/MountUpdateService.php diff --git a/app/Http/Controllers/Admin/MountController.php b/app/Http/Controllers/Admin/MountController.php index a9be8d7dd..aa1bcf918 100644 --- a/app/Http/Controllers/Admin/MountController.php +++ b/app/Http/Controllers/Admin/MountController.php @@ -2,10 +2,14 @@ namespace Pterodactyl\Http\Controllers\Admin; +use Pterodactyl\Models\Mount; use Prologue\Alerts\AlertsMessageBag; +use Pterodactyl\Exceptions\DisplayException; use Pterodactyl\Http\Controllers\Controller; -use Pterodactyl\Http\Requests\Admin\MountFormRequest; +use Pterodactyl\Services\Mounts\MountUpdateService; use Pterodactyl\Services\Mounts\MountCreationService; +use Pterodactyl\Services\Mounts\MountDeletionService; +use Pterodactyl\Http\Requests\Admin\MountFormRequest; use Pterodactyl\Repositories\Eloquent\MountRepository; class MountController extends Controller @@ -21,25 +25,41 @@ class MountController extends Controller protected $repository; /** - * @var \Pterodactyl\Services\Locations\LocationCreationService + * @var \Pterodactyl\Services\Mounts\MountCreationService */ protected $creationService; + /** + * @var \Pterodactyl\Services\Mounts\MountDeletionService + */ + protected $deletionService; + + /** + * @var \Pterodactyl\Services\Mounts\MountUpdateService + */ + protected $updateService; + /** * MountController constructor. * * @param \Prologue\Alerts\AlertsMessageBag $alert * @param \Pterodactyl\Repositories\Eloquent\MountRepository $repository * @param \Pterodactyl\Services\Mounts\MountCreationService $creationService + * @param \Pterodactyl\Services\Mounts\MountDeletionService $deletionService + * @param \Pterodactyl\Services\Mounts\MountUpdateService $updateService */ public function __construct( AlertsMessageBag $alert, MountRepository $repository, - MountCreationService $creationService + MountCreationService $creationService, + MountDeletionService $deletionService, + MountUpdateService $updateService ) { $this->alert = $alert; $this->repository = $repository; $this->creationService = $creationService; + $this->deletionService = $deletionService; + $this->updateService = $updateService; } /** @@ -82,7 +102,48 @@ class MountController extends Controller $mount = $this->creationService->handle($request->normalize()); $this->alert->success('Mount was created successfully.')->flash(); - //return redirect()->route('admin.mounts.view', $mount->id); - return redirect()->route('admin.mounts'); + return redirect()->route('admin.mounts.view', $mount->id); + } + + /** + * Handle request to update or delete location. + * + * @param \Pterodactyl\Http\Requests\Admin\MountFormRequest $request + * @param \Pterodactyl\Models\Mount $mount + * @return \Illuminate\Http\RedirectResponse + * + * @throws \Throwable + */ + public function update(MountFormRequest $request, Mount $mount) + { + if ($request->input('action') === 'delete') { + return $this->delete($mount); + } + + $this->updateService->handle($mount->id, $request->normalize()); + $this->alert->success('Mount was updated successfully.')->flash(); + + return redirect()->route('admin.mounts.view', $mount->id); + } + + /** + * Delete a location from the system. + * + * @param \Pterodactyl\Models\Mount $mount + * @return \Illuminate\Http\RedirectResponse + * + * @throws \Exception + */ + public function delete(Mount $mount) + { + try { + $this->deletionService->handle($mount->id); + + return redirect()->route('admin.mounts'); + } catch (DisplayException $ex) { + $this->alert->danger($ex->getMessage())->flash(); + } + + return redirect()->route('admin.mounts.view', $mount->id); } } diff --git a/app/Services/Mounts/MountDeletionService.php b/app/Services/Mounts/MountDeletionService.php new file mode 100644 index 000000000..0850e685b --- /dev/null +++ b/app/Services/Mounts/MountDeletionService.php @@ -0,0 +1,40 @@ +repository = $repository; + } + + /** + * Delete an existing location. + * + * @param int|\Pterodactyl\Models\Mount $mount + * @return int|null + */ + public function handle($mount) + { + $mount = ($mount instanceof Mount) ? $mount->id : $mount; + + Assert::integerish($mount, 'First argument passed to handle must be numeric or an instance of ' . Mount::class . ', received %s.'); + + return $this->repository->delete($mount); + } +} diff --git a/app/Services/Mounts/MountUpdateService.php b/app/Services/Mounts/MountUpdateService.php new file mode 100644 index 000000000..a66f1ea91 --- /dev/null +++ b/app/Services/Mounts/MountUpdateService.php @@ -0,0 +1,41 @@ +repository = $repository; + } + + /** + * Update an existing location. + * + * @param int|\Pterodactyl\Models\Mount $mount + * @param array $data + * @return \Pterodactyl\Models\Mount + * + * @throws \Pterodactyl\Exceptions\Model\DataValidationException + * @throws \Pterodactyl\Exceptions\Repository\RecordNotFoundException + */ + public function handle($mount, array $data) + { + $mount = ($mount instanceof Mount) ? $mount->id : $mount; + + return $this->repository->update($mount, $data); + } +} diff --git a/resources/views/admin/mounts/index.blade.php b/resources/views/admin/mounts/index.blade.php index 57835001a..da554cd04 100644 --- a/resources/views/admin/mounts/index.blade.php +++ b/resources/views/admin/mounts/index.blade.php @@ -47,8 +47,8 @@ {{ $mount->id }} {{ $mount->name }} - {{ $mount->source }} - {{ $mount->target }} + {{ $mount->source }} + {{ $mount->target }} {{ $mount->eggs_count }} {{ $mount->nodes_count }} {{ $mount->servers_count }} diff --git a/resources/views/admin/mounts/view.blade.php b/resources/views/admin/mounts/view.blade.php index ef93ec9ba..2f72946e3 100644 --- a/resources/views/admin/mounts/view.blade.php +++ b/resources/views/admin/mounts/view.blade.php @@ -57,12 +57,12 @@
- read_only) checked @endif> + read_only) checked @endif>
- read_only) checked @endif> + read_only) checked @endif>
@@ -73,12 +73,12 @@
- user_mountable) checked @endif> + user_mountable) checked @endif>
- user_mountable) checked @endif> + user_mountable) checked @endif>
diff --git a/routes/admin.php b/routes/admin.php index f552e6a33..17f4d0a1f 100644 --- a/routes/admin.php +++ b/routes/admin.php @@ -178,6 +178,7 @@ Route::group(['prefix' => 'mounts'], function () { Route::get('/view/{mount}', 'MountController@view')->name('admin.mounts.view'); Route::post('/', 'MountController@create'); + Route::patch('/view/{mount}', 'MountController@update'); }); /*