Add mount update and deletion services, add MountController@update and MountController@delete

This commit is contained in:
Matthew Penner 2020-05-20 19:35:30 -06:00
parent 77150b2551
commit 0db7debb46
6 changed files with 154 additions and 11 deletions

View file

@ -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);
}
}

View file

@ -0,0 +1,40 @@
<?php
namespace Pterodactyl\Services\Mounts;
use Webmozart\Assert\Assert;
use Pterodactyl\Models\Mount;
use Pterodactyl\Repositories\Eloquent\MountRepository;
class MountDeletionService
{
/**
* @var \Pterodactyl\Repositories\Eloquent\MountRepository
*/
protected $repository;
/**
* MountDeletionService constructor.
*
* @param \Pterodactyl\Repositories\Eloquent\MountRepository $repository
*/
public function __construct(MountRepository $repository)
{
$this->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);
}
}

View file

@ -0,0 +1,41 @@
<?php
namespace Pterodactyl\Services\Mounts;
use Pterodactyl\Models\Mount;
use Pterodactyl\Repositories\Eloquent\MountRepository;
class MountUpdateService
{
/**
* @var \Pterodactyl\Repositories\Eloquent\MountRepository
*/
protected $repository;
/**
* MountUpdateService constructor.
*
* @param \Pterodactyl\Repositories\Eloquent\MountRepository $repository
*/
public function __construct(MountRepository $repository)
{
$this->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);
}
}

View file

@ -47,8 +47,8 @@
<tr>
<td><code>{{ $mount->id }}</code></td>
<td><a href="{{ route('admin.mounts.view', $mount->id) }}">{{ $mount->name }}</a></td>
<td>{{ $mount->source }}</td>
<td>{{ $mount->target }}</td>
<td><code>{{ $mount->source }}</code></td>
<td><code>{{ $mount->target }}</code></td>
<td class="text-center">{{ $mount->eggs_count }}</td>
<td class="text-center">{{ $mount->nodes_count }}</td>
<td class="text-center">{{ $mount->servers_count }}</td>

View file

@ -57,12 +57,12 @@
<div>
<div class="radio radio-success radio-inline">
<input type="radio" id="pReadOnlyFalse" name="read_only" value="0" @if($mount->read_only) checked @endif>
<input type="radio" id="pReadOnlyFalse" name="read_only" value="0" @if(!$mount->read_only) checked @endif>
<label for="pReadOnlyFalse">False</label>
</div>
<div class="radio radio-warning radio-inline">
<input type="radio" id="pReadOnly" name="read_only" value="1" @if(!$mount->read_only) checked @endif>
<input type="radio" id="pReadOnly" name="read_only" value="1" @if($mount->read_only) checked @endif>
<label for="pReadOnly">True</label>
</div>
</div>
@ -73,12 +73,12 @@
<div>
<div class="radio radio-success radio-inline">
<input type="radio" id="pUserMountableFalse" name="user_mountable" value="0" @if($mount->user_mountable) checked @endif>
<input type="radio" id="pUserMountableFalse" name="user_mountable" value="0" @if(!$mount->user_mountable) checked @endif>
<label for="pUserMountableFalse">False</label>
</div>
<div class="radio radio-warning radio-inline">
<input type="radio" id="pUserMountable" name="user_mountable" value="1" @if(!$mount->user_mountable) checked @endif>
<input type="radio" id="pUserMountable" name="user_mountable" value="1" @if($mount->user_mountable) checked @endif>
<label for="pUserMountable">True</label>
</div>
</div>

View file

@ -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');
});
/*