Skip to content

Commit 7277f72

Browse files
committed
Complete migration of node controllers/repositories to new service structure
1 parent 669119c commit 7277f72

File tree

4 files changed

+40
-36
lines changed

4 files changed

+40
-36
lines changed

app/Contracts/Repository/RepositoryInterface.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,10 +84,18 @@ public function create(array $fields, $validate = true);
8484
* Delete a given record from the database.
8585
*
8686
* @param int $id
87-
* @return bool|null
87+
* @return int
8888
*/
8989
public function delete($id);
9090

91+
/**
92+
* Delete records matching the given attributes.
93+
*
94+
* @param array $attributes
95+
* @return int
96+
*/
97+
public function deleteWhere(array $attributes);
98+
9199
/**
92100
* Find a model that has the specific ID passed.
93101
*

app/Http/Controllers/Admin/NodesController.php

Lines changed: 18 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -24,17 +24,14 @@
2424

2525
namespace Pterodactyl\Http\Controllers\Admin;
2626

27-
use Alert;
2827
use Javascript;
2928
use Illuminate\Http\Request;
3029
use Pterodactyl\Models\Node;
31-
use Pterodactyl\Models\Allocation;
3230
use Prologue\Alerts\AlertsMessageBag;
3331
use Pterodactyl\Http\Controllers\Controller;
3432
use Pterodactyl\Services\Nodes\UpdateService;
3533
use Pterodactyl\Services\Nodes\CreationService;
3634
use Pterodactyl\Services\Nodes\DeletionService;
37-
use Illuminate\Contracts\Translation\Translator;
3835
use Illuminate\Cache\Repository as CacheRepository;
3936
use Pterodactyl\Services\Allocations\AssignmentService;
4037
use Pterodactyl\Http\Requests\Admin\Node\NodeFormRequest;
@@ -86,11 +83,6 @@ class NodesController extends Controller
8683
*/
8784
protected $repository;
8885

89-
/**
90-
* @var \Illuminate\Contracts\Translation\Translator
91-
*/
92-
protected $translator;
93-
9486
/**
9587
* @var \Pterodactyl\Services\Nodes\UpdateService
9688
*/
@@ -107,7 +99,6 @@ class NodesController extends Controller
10799
* @param \Pterodactyl\Services\Nodes\DeletionService $deletionService
108100
* @param \Pterodactyl\Contracts\Repository\LocationRepositoryInterface $locationRepository
109101
* @param \Pterodactyl\Contracts\Repository\NodeRepositoryInterface $repository
110-
* @param \Illuminate\Contracts\Translation\Translator $translator
111102
* @param \Pterodactyl\Services\Nodes\UpdateService $updateService
112103
*/
113104
public function __construct(
@@ -119,7 +110,6 @@ public function __construct(
119110
DeletionService $deletionService,
120111
LocationRepositoryInterface $locationRepository,
121112
NodeRepositoryInterface $repository,
122-
Translator $translator,
123113
UpdateService $updateService
124114
) {
125115
$this->alert = $alert;
@@ -130,7 +120,6 @@ public function __construct(
130120
$this->deletionService = $deletionService;
131121
$this->locationRepository = $locationRepository;
132122
$this->repository = $repository;
133-
$this->translator = $translator;
134123
$this->updateService = $updateService;
135124
}
136125

@@ -156,7 +145,7 @@ public function create()
156145
{
157146
$locations = $this->locationRepository->all();
158147
if (count($locations) < 1) {
159-
$this->alert->warning($this->translator->trans('admin/node.notices.location_required'))->flash();
148+
$this->alert->warning(trans('admin/node.notices.location_required'))->flash();
160149

161150
return redirect()->route('admin.locations');
162151
}
@@ -175,7 +164,7 @@ public function create()
175164
public function store(NodeFormRequest $request)
176165
{
177166
$node = $this->creationService->handle($request->normalize());
178-
$this->alert->info($this->translator->trans('admin/node.notices.node_created'))->flash();
167+
$this->alert->info(trans('admin/node.notices.node_created'))->flash();
179168

180169
return redirect()->route('admin.nodes.view.allocation', $node->id);
181170
}
@@ -262,7 +251,7 @@ public function viewServers($node)
262251
public function updateSettings(NodeFormRequest $request, Node $node)
263252
{
264253
$this->updateService->handle($node, $request->normalize());
265-
$this->alert->success($this->translator->trans('admin/node.notices.node_updated'))->flash();
254+
$this->alert->success(trans('admin/node.notices.node_updated'))->flash();
266255

267256
return redirect()->route('admin.nodes.view.settings', $node->id)->withInput();
268257
}
@@ -276,12 +265,11 @@ public function updateSettings(NodeFormRequest $request, Node $node)
276265
*/
277266
public function allocationRemoveSingle($node, $allocation)
278267
{
279-
$query = Allocation::where('node_id', $node)->whereNull('server_id')->where('id', $allocation)->delete();
280-
if ($query < 1) {
281-
return response()->json([
282-
'error' => 'Unable to find an allocation matching those details to delete.',
283-
], 400);
284-
}
268+
$this->allocationRepository->deleteWhere([
269+
['id', '=', $allocation],
270+
['node_id', '=', $node],
271+
['server_id', '=', null],
272+
]);
285273

286274
return response('', 204);
287275
}
@@ -295,15 +283,14 @@ public function allocationRemoveSingle($node, $allocation)
295283
*/
296284
public function allocationRemoveBlock(Request $request, $node)
297285
{
298-
$query = Allocation::where('node_id', $node)
299-
->whereNull('server_id')
300-
->where('ip', $request->input('ip'))
301-
->delete();
302-
if ($query < 1) {
303-
Alert::danger('There was an error while attempting to delete allocations on that IP.')->flash();
304-
} else {
305-
Alert::success('Deleted all unallocated ports for <code>' . $request->input('ip') . '</code>.')->flash();
306-
}
286+
$this->allocationRepository->deleteWhere([
287+
['node_id', '=', $node],
288+
['server_id', '=', null],
289+
['ip', '=', $request->input('ip')],
290+
]);
291+
292+
$this->alert->success(trans('admin/node.notices.unallocated_deleted', ['ip' => $request->input('ip')]))
293+
->flash();
307294

308295
return redirect()->route('admin.nodes.view.allocation', $node);
309296
}
@@ -337,7 +324,7 @@ public function allocationSetAlias(AllocationAliasFormRequest $request)
337324
public function createAllocation(AllocationFormRequest $request, Node $node)
338325
{
339326
$this->assignmentService->handle($node, $request->normalize());
340-
$this->alert->success($this->translator->trans('admin/node.notices.allocations_added'))->flash();
327+
$this->alert->success(trans('admin/node.notices.allocations_added'))->flash();
341328

342329
return redirect()->route('admin.nodes.view.allocation', $node->id);
343330
}
@@ -353,7 +340,7 @@ public function createAllocation(AllocationFormRequest $request, Node $node)
353340
public function delete($node)
354341
{
355342
$this->deletionService->handle($node);
356-
$this->alert->success($this->translator->trans('admin/node.notices.node_deleted'))->flash();
343+
$this->alert->success(trans('admin/node.notices.node_deleted'))->flash();
357344

358345
return redirect()->route('admin.nodes');
359346
}

app/Repositories/Eloquent/EloquentRepository.php

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -119,11 +119,19 @@ public function findCountWhere(array $fields)
119119
*/
120120
public function delete($id, $destroy = false)
121121
{
122-
if ($destroy) {
123-
return $this->getBuilder()->where($this->getModel()->getKeyName(), $id)->forceDelete();
124-
}
122+
$instance = $this->getBuilder()->where($this->getModel()->getKeyName(), $id);
123+
124+
return ($destroy) ? $instance->forceDelete() : $instance->delete();
125+
}
126+
127+
/**
128+
* {@inheritdoc}
129+
*/
130+
public function deleteWhere(array $attributes, $force = false)
131+
{
132+
$instance = $this->getBuilder()->where($attributes);
125133

126-
return $this->getBuilder()->where($this->getModel()->getKeyName(), $id)->delete();
134+
return ($force) ? $instance->forceDelete() : $instance->delete();
127135
}
128136

129137
/**

resources/lang/en/admin/node.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,5 +33,6 @@
3333
'location_required' => 'You must have at least one location configured before you can add a node to this panel.',
3434
'node_created' => 'Successfully created new node. You can automatically configure the daemon on this machine by visiting the \'Configuration\' tab. <strong>Before you can add any servers you must first allocate at least one IP address and port.</strong>',
3535
'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.',
36+
'unallocated_deleted' => 'Deleted all unallocatred ports for <code>:ip</code>.',
3637
],
3738
];

0 commit comments

Comments
 (0)