Allow deleting default allocation and setting new default at the same time.

This commit is contained in:
Dane Everitt 2017-04-17 21:07:37 -04:00
parent 776220636b
commit 3acc7b338b
No known key found for this signature in database
GPG key ID: EEA66103B3D71F53
3 changed files with 27 additions and 18 deletions

View file

@ -518,23 +518,7 @@ class ServerRepository
}
$newPorts = false;
// Remove Assignments
if (isset($data['remove_allocations'])) {
foreach ($data['remove_allocations'] as $allocation) {
// Can't remove the assigned IP/Port combo
if ((int) $allocation === $server->allocation_id) {
continue;
}
$newPorts = true;
Models\Allocation::where('id', $allocation)->where('server_id', $server->id)->update([
'server_id' => null,
]);
}
$server->load('allocations');
}
$firstNewAllocation = null;
// Add Assignments
if (isset($data['add_allocations'])) {
foreach ($data['add_allocations'] as $allocation) {
@ -544,6 +528,7 @@ class ServerRepository
}
$newPorts = true;
$firstNewAllocation = (is_null($firstNewAllocation)) ? $model->id : $firstNewAllocation;
$model->update([
'server_id' => $server->id,
]);
@ -552,6 +537,29 @@ class ServerRepository
$server->load('allocations');
}
// Remove Assignments
if (isset($data['remove_allocations'])) {
foreach ($data['remove_allocations'] as $allocation) {
// Can't remove the assigned IP/Port combo
if ((int) $allocation === $server->allocation_id) {
// No New Allocation
if (is_null($firstNewAllocation)) {
continue;
}
// New Allocation, set as the default.
$server->allocation_id = $firstNewAllocation;
}
$newPorts = true;
Models\Allocation::where('id', $allocation)->where('server_id', $server->id)->update([
'server_id' => null,
]);
}
$server->load('allocations');
}
if ($newPorts) {
$newBuild['ports|overwrite'] = $server->allocations->groupBy('ip')->map(function ($item) {
return $item->pluck('port');