Cleanup location model and controller for Admin.
This commit is contained in:
parent
0720bfe62f
commit
ba175e6b55
3 changed files with 27 additions and 21 deletions
|
@ -43,35 +43,21 @@ class LocationsController extends Controller
|
|||
public function getIndex(Request $request)
|
||||
{
|
||||
return view('admin.locations.index', [
|
||||
'locations' => Models\Location::select(
|
||||
'locations.*',
|
||||
DB::raw('(SELECT COUNT(*) FROM nodes WHERE nodes.location_id = locations.id) as a_nodeCount'),
|
||||
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')
|
||||
)->paginate(20),
|
||||
'locations' => Models\Location::withCount('nodes', 'servers')->paginate(20),
|
||||
]);
|
||||
}
|
||||
|
||||
public function deleteLocation(Request $request, $id)
|
||||
{
|
||||
$model = Models\Location::select(
|
||||
'locations.id',
|
||||
DB::raw('(SELECT COUNT(*) FROM nodes WHERE nodes.location_id = locations.id) as a_nodeCount'),
|
||||
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')
|
||||
)->where('id', $id)->first();
|
||||
$location = Models\Location::withCount('nodes')->findOrFail($id);
|
||||
|
||||
if (! $model) {
|
||||
if ($location->nodes_count > 0) {
|
||||
return response()->json([
|
||||
'error' => 'No location with that ID exists on the system.',
|
||||
], 404);
|
||||
}
|
||||
|
||||
if ($model->a_nodeCount > 0 || $model->a_serverCount > 0) {
|
||||
return response()->json([
|
||||
'error' => 'You cannot remove a location that is currently assigned to a node or server.',
|
||||
'error' => 'You cannot remove a location that is currently assigned to a node.',
|
||||
], 422);
|
||||
}
|
||||
|
||||
$model->delete();
|
||||
$location->delete();
|
||||
|
||||
return response('', 204);
|
||||
}
|
||||
|
|
|
@ -41,4 +41,24 @@ class Location extends Model
|
|||
* @var array
|
||||
*/
|
||||
protected $guarded = ['id', 'created_at', 'updated_at'];
|
||||
|
||||
/**
|
||||
* Gets the nodes in a specificed location.
|
||||
*
|
||||
* @return \Illuminate\Database\Eloquent\Relations\HasMany
|
||||
*/
|
||||
public function nodes()
|
||||
{
|
||||
return $this->hasMany(Node::class);
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the servers within a given location.
|
||||
*
|
||||
* @return \Illuminate\Database\Eloquent\Relations\HasManyThrough
|
||||
*/
|
||||
public function servers()
|
||||
{
|
||||
return $this->hasManyThrough(Server::class, Node::class);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -46,8 +46,8 @@
|
|||
<tr>
|
||||
<td><code>{{ $location->short }}</code></td>
|
||||
<td>{{ $location->long }}</td>
|
||||
<td class="text-center">{{ $location->a_nodeCount }}</td>
|
||||
<td class="text-center">{{ $location->a_serverCount }}</td>
|
||||
<td class="text-center">{{ $location->nodes_count }}</td>
|
||||
<td class="text-center">{{ $location->servers_count }}</td>
|
||||
<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>
|
||||
<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>
|
||||
</tr>
|
||||
|
|
Loading…
Reference in a new issue