Skip to content

Commit ba175e6

Browse files
committed
Cleanup location model and controller for Admin.
1 parent 0720bfe commit ba175e6

File tree

3 files changed

+27
-21
lines changed

3 files changed

+27
-21
lines changed

app/Http/Controllers/Admin/LocationsController.php

Lines changed: 5 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -43,35 +43,21 @@ public function __construct()
4343
public function getIndex(Request $request)
4444
{
4545
return view('admin.locations.index', [
46-
'locations' => Models\Location::select(
47-
'locations.*',
48-
DB::raw('(SELECT COUNT(*) FROM nodes WHERE nodes.location_id = locations.id) as a_nodeCount'),
49-
DB::raw('(SELECT COUNT(*) FROM servers WHERE servers.node_id IN (SELECT nodes.id FROM nodes WHERE nodes.location_id = locations.id)) as a_serverCount')
50-
)->paginate(20),
46+
'locations' => Models\Location::withCount('nodes', 'servers')->paginate(20),
5147
]);
5248
}
5349

5450
public function deleteLocation(Request $request, $id)
5551
{
56-
$model = Models\Location::select(
57-
'locations.id',
58-
DB::raw('(SELECT COUNT(*) FROM nodes WHERE nodes.location_id = locations.id) as a_nodeCount'),
59-
DB::raw('(SELECT COUNT(*) FROM servers WHERE servers.node_id IN (SELECT nodes.id FROM nodes WHERE nodes.location_id = locations.id)) as a_serverCount')
60-
)->where('id', $id)->first();
52+
$location = Models\Location::withCount('nodes')->findOrFail($id);
6153

62-
if (! $model) {
54+
if ($location->nodes_count > 0) {
6355
return response()->json([
64-
'error' => 'No location with that ID exists on the system.',
65-
], 404);
66-
}
67-
68-
if ($model->a_nodeCount > 0 || $model->a_serverCount > 0) {
69-
return response()->json([
70-
'error' => 'You cannot remove a location that is currently assigned to a node or server.',
56+
'error' => 'You cannot remove a location that is currently assigned to a node.',
7157
], 422);
7258
}
7359

74-
$model->delete();
60+
$location->delete();
7561

7662
return response('', 204);
7763
}

app/Models/Location.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,4 +41,24 @@ class Location extends Model
4141
* @var array
4242
*/
4343
protected $guarded = ['id', 'created_at', 'updated_at'];
44+
45+
/**
46+
* Gets the nodes in a specificed location.
47+
*
48+
* @return \Illuminate\Database\Eloquent\Relations\HasMany
49+
*/
50+
public function nodes()
51+
{
52+
return $this->hasMany(Node::class);
53+
}
54+
55+
/**
56+
* Gets the servers within a given location.
57+
*
58+
* @return \Illuminate\Database\Eloquent\Relations\HasManyThrough
59+
*/
60+
public function servers()
61+
{
62+
return $this->hasManyThrough(Server::class, Node::class);
63+
}
4464
}

resources/views/admin/locations/index.blade.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,8 @@
4646
<tr>
4747
<td><code>{{ $location->short }}</code></td>
4848
<td>{{ $location->long }}</td>
49-
<td class="text-center">{{ $location->a_nodeCount }}</td>
50-
<td class="text-center">{{ $location->a_serverCount }}</td>
49+
<td class="text-center">{{ $location->nodes_count }}</td>
50+
<td class="text-center">{{ $location->servers_count }}</td>
5151
<td class="text-center"><a href="#edit"><i class="fa fa-wrench" data-toggle="modal" data-target="#editModal" data-action="edit" data-id="{{ $location->id }}" data-short="{{ $location->short }}" data-long="{{ $location->long }}"></i></a></td>
5252
<td class="text-center"><a href="#delete" class="text-danger" data-action="delete" data-id="{{ $location->id }}"><i class="fa fa-trash-o"></i></a></td>
5353
</tr>

0 commit comments

Comments
 (0)