From 7277f728a9c06f132539462ed8cbc8f07ba85b68 Mon Sep 17 00:00:00 2001 From: Dane Everitt Date: Tue, 8 Aug 2017 21:21:10 -0500 Subject: [PATCH] Complete migration of node controllers/repositories to new service structure --- .../Repository/RepositoryInterface.php | 10 +++- .../Controllers/Admin/NodesController.php | 49 +++++++------------ .../Eloquent/EloquentRepository.php | 16 ++++-- resources/lang/en/admin/node.php | 1 + 4 files changed, 40 insertions(+), 36 deletions(-) diff --git a/app/Contracts/Repository/RepositoryInterface.php b/app/Contracts/Repository/RepositoryInterface.php index f48755d6c..a26646d08 100644 --- a/app/Contracts/Repository/RepositoryInterface.php +++ b/app/Contracts/Repository/RepositoryInterface.php @@ -84,10 +84,18 @@ interface RepositoryInterface * Delete a given record from the database. * * @param int $id - * @return bool|null + * @return int */ public function delete($id); + /** + * Delete records matching the given attributes. + * + * @param array $attributes + * @return int + */ + public function deleteWhere(array $attributes); + /** * Find a model that has the specific ID passed. * diff --git a/app/Http/Controllers/Admin/NodesController.php b/app/Http/Controllers/Admin/NodesController.php index 97b85abc8..4be09917a 100644 --- a/app/Http/Controllers/Admin/NodesController.php +++ b/app/Http/Controllers/Admin/NodesController.php @@ -24,17 +24,14 @@ namespace Pterodactyl\Http\Controllers\Admin; -use Alert; use Javascript; use Illuminate\Http\Request; use Pterodactyl\Models\Node; -use Pterodactyl\Models\Allocation; use Prologue\Alerts\AlertsMessageBag; use Pterodactyl\Http\Controllers\Controller; use Pterodactyl\Services\Nodes\UpdateService; use Pterodactyl\Services\Nodes\CreationService; use Pterodactyl\Services\Nodes\DeletionService; -use Illuminate\Contracts\Translation\Translator; use Illuminate\Cache\Repository as CacheRepository; use Pterodactyl\Services\Allocations\AssignmentService; use Pterodactyl\Http\Requests\Admin\Node\NodeFormRequest; @@ -86,11 +83,6 @@ class NodesController extends Controller */ protected $repository; - /** - * @var \Illuminate\Contracts\Translation\Translator - */ - protected $translator; - /** * @var \Pterodactyl\Services\Nodes\UpdateService */ @@ -107,7 +99,6 @@ class NodesController extends Controller * @param \Pterodactyl\Services\Nodes\DeletionService $deletionService * @param \Pterodactyl\Contracts\Repository\LocationRepositoryInterface $locationRepository * @param \Pterodactyl\Contracts\Repository\NodeRepositoryInterface $repository - * @param \Illuminate\Contracts\Translation\Translator $translator * @param \Pterodactyl\Services\Nodes\UpdateService $updateService */ public function __construct( @@ -119,7 +110,6 @@ class NodesController extends Controller DeletionService $deletionService, LocationRepositoryInterface $locationRepository, NodeRepositoryInterface $repository, - Translator $translator, UpdateService $updateService ) { $this->alert = $alert; @@ -130,7 +120,6 @@ class NodesController extends Controller $this->deletionService = $deletionService; $this->locationRepository = $locationRepository; $this->repository = $repository; - $this->translator = $translator; $this->updateService = $updateService; } @@ -156,7 +145,7 @@ class NodesController extends Controller { $locations = $this->locationRepository->all(); if (count($locations) < 1) { - $this->alert->warning($this->translator->trans('admin/node.notices.location_required'))->flash(); + $this->alert->warning(trans('admin/node.notices.location_required'))->flash(); return redirect()->route('admin.locations'); } @@ -175,7 +164,7 @@ class NodesController extends Controller public function store(NodeFormRequest $request) { $node = $this->creationService->handle($request->normalize()); - $this->alert->info($this->translator->trans('admin/node.notices.node_created'))->flash(); + $this->alert->info(trans('admin/node.notices.node_created'))->flash(); return redirect()->route('admin.nodes.view.allocation', $node->id); } @@ -262,7 +251,7 @@ class NodesController extends Controller public function updateSettings(NodeFormRequest $request, Node $node) { $this->updateService->handle($node, $request->normalize()); - $this->alert->success($this->translator->trans('admin/node.notices.node_updated'))->flash(); + $this->alert->success(trans('admin/node.notices.node_updated'))->flash(); return redirect()->route('admin.nodes.view.settings', $node->id)->withInput(); } @@ -276,12 +265,11 @@ class NodesController extends Controller */ public function allocationRemoveSingle($node, $allocation) { - $query = Allocation::where('node_id', $node)->whereNull('server_id')->where('id', $allocation)->delete(); - if ($query < 1) { - return response()->json([ - 'error' => 'Unable to find an allocation matching those details to delete.', - ], 400); - } + $this->allocationRepository->deleteWhere([ + ['id', '=', $allocation], + ['node_id', '=', $node], + ['server_id', '=', null], + ]); return response('', 204); } @@ -295,15 +283,14 @@ class NodesController extends Controller */ public function allocationRemoveBlock(Request $request, $node) { - $query = Allocation::where('node_id', $node) - ->whereNull('server_id') - ->where('ip', $request->input('ip')) - ->delete(); - if ($query < 1) { - Alert::danger('There was an error while attempting to delete allocations on that IP.')->flash(); - } else { - Alert::success('Deleted all unallocated ports for ' . $request->input('ip') . '.')->flash(); - } + $this->allocationRepository->deleteWhere([ + ['node_id', '=', $node], + ['server_id', '=', null], + ['ip', '=', $request->input('ip')], + ]); + + $this->alert->success(trans('admin/node.notices.unallocated_deleted', ['ip' => $request->input('ip')])) + ->flash(); return redirect()->route('admin.nodes.view.allocation', $node); } @@ -337,7 +324,7 @@ class NodesController extends Controller public function createAllocation(AllocationFormRequest $request, Node $node) { $this->assignmentService->handle($node, $request->normalize()); - $this->alert->success($this->translator->trans('admin/node.notices.allocations_added'))->flash(); + $this->alert->success(trans('admin/node.notices.allocations_added'))->flash(); return redirect()->route('admin.nodes.view.allocation', $node->id); } @@ -353,7 +340,7 @@ class NodesController extends Controller public function delete($node) { $this->deletionService->handle($node); - $this->alert->success($this->translator->trans('admin/node.notices.node_deleted'))->flash(); + $this->alert->success(trans('admin/node.notices.node_deleted'))->flash(); return redirect()->route('admin.nodes'); } diff --git a/app/Repositories/Eloquent/EloquentRepository.php b/app/Repositories/Eloquent/EloquentRepository.php index 98c546221..77108b59a 100644 --- a/app/Repositories/Eloquent/EloquentRepository.php +++ b/app/Repositories/Eloquent/EloquentRepository.php @@ -119,11 +119,19 @@ abstract class EloquentRepository extends Repository implements RepositoryInterf */ public function delete($id, $destroy = false) { - if ($destroy) { - return $this->getBuilder()->where($this->getModel()->getKeyName(), $id)->forceDelete(); - } + $instance = $this->getBuilder()->where($this->getModel()->getKeyName(), $id); - return $this->getBuilder()->where($this->getModel()->getKeyName(), $id)->delete(); + return ($destroy) ? $instance->forceDelete() : $instance->delete(); + } + + /** + * {@inheritdoc} + */ + public function deleteWhere(array $attributes, $force = false) + { + $instance = $this->getBuilder()->where($attributes); + + return ($force) ? $instance->forceDelete() : $instance->delete(); } /** diff --git a/resources/lang/en/admin/node.php b/resources/lang/en/admin/node.php index 2b3a4bf51..8e685a8f8 100644 --- a/resources/lang/en/admin/node.php +++ b/resources/lang/en/admin/node.php @@ -33,5 +33,6 @@ return [ 'location_required' => 'You must have at least one location configured before you can add a node to this panel.', 'node_created' => 'Successfully created new node. You can automatically configure the daemon on this machine by visiting the \'Configuration\' tab. Before you can add any servers you must first allocate at least one IP address and port.', 'node_updated' => 'Node information has been updated. If any daemon settings were changed you will need to reboot it for those changes to take effect.', + 'unallocated_deleted' => 'Deleted all unallocatred ports for :ip.', ], ];