Cleanup location model and controller for Admin.

This commit is contained in:
Dane Everitt 2017-02-10 17:09:56 -05:00
parent 0720bfe62f
commit ba175e6b55
No known key found for this signature in database
GPG Key ID: EEA66103B3D71F53
3 changed files with 27 additions and 21 deletions

View File

@ -43,35 +43,21 @@ class LocationsController extends Controller
public function getIndex(Request $request) public function getIndex(Request $request)
{ {
return view('admin.locations.index', [ return view('admin.locations.index', [
'locations' => Models\Location::select( 'locations' => Models\Location::withCount('nodes', 'servers')->paginate(20),
'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),
]); ]);
} }
public function deleteLocation(Request $request, $id) public function deleteLocation(Request $request, $id)
{ {
$model = Models\Location::select( $location = Models\Location::withCount('nodes')->findOrFail($id);
'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();
if (! $model) { if ($location->nodes_count > 0) {
return response()->json([ return response()->json([
'error' => 'No location with that ID exists on the system.', 'error' => 'You cannot remove a location that is currently assigned to a node.',
], 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.',
], 422); ], 422);
} }
$model->delete(); $location->delete();
return response('', 204); return response('', 204);
} }

View File

@ -41,4 +41,24 @@ class Location extends Model
* @var array * @var array
*/ */
protected $guarded = ['id', 'created_at', 'updated_at']; 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);
}
} }

View File

@ -46,8 +46,8 @@
<tr> <tr>
<td><code>{{ $location->short }}</code></td> <td><code>{{ $location->short }}</code></td>
<td>{{ $location->long }}</td> <td>{{ $location->long }}</td>
<td class="text-center">{{ $location->a_nodeCount }}</td> <td class="text-center">{{ $location->nodes_count }}</td>
<td class="text-center">{{ $location->a_serverCount }}</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="#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> <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> </tr>