Address input fixes, region_id/host_id must not be null, address constraints changes

This commit is contained in:
Deon George 2021-06-25 16:42:12 +10:00
parent 848f41b382
commit 1f04f8374e
6 changed files with 77 additions and 13 deletions

View File

@ -13,6 +13,8 @@ class DomainController extends Controller
public const NODE_RC = 1<<2; // Region
public const NODE_NC = 1<<3; // Host
public const NODE_HC = 1<<4; // Hub
public const NODE_PVT = 1<<5; // Pvt
public const NODE_DOWN = 1<<6; // Down
// http://ftsc.org/docs/frl-1002.001
public const NUMBER_MAX = 0x7fff;
@ -87,7 +89,7 @@ class DomainController extends Controller
->get();
return $oo->map(function($item) {
return ['id'=>$item->host_id,'value'=>sprintf('%s %s',$item->ftn,$item->system->name)];
return ['id'=>$item->id,'value'=>sprintf('%s %s',$item->ftn,$item->system->name)];
});
}

View File

@ -42,7 +42,7 @@ class SystemController extends Controller
// Check that the region doesnt already exist
$o = Address::where(function($query) use ($value) {
return $query->where('region_id',$value)
->whereNULL('host_id')
->where('host_id',0)
->where('node_id',0)
->where('point_id',0)
->where('role',DomainController::NODE_RC);
@ -74,7 +74,7 @@ class SystemController extends Controller
case 'host':
$request->validate([
'region_id' => ['nullable',new TwoByteInteger],
'region_id' => ['required',new TwoByteInteger],
'host_id_new' => [
'required',
new TwoByteInteger,
@ -82,7 +82,7 @@ class SystemController extends Controller
// Check that the region doesnt already exist
$o = Address::where(function($query) use ($value) {
return $query->where('region_id',$value)
->whereNULL('host_id')
->where('host_id',0)
->where('node_id',0)
->where('point_id',0)
->where('role',DomainController::NODE_RC);
@ -121,7 +121,7 @@ class SystemController extends Controller
$oo = new Address;
$oo->zone_id = $request->post('zone_id');
$oo->region_id = ($x=$request->post('region_id')) == 'no' ? NULL : $x;
$oo->region_id = ($x=$request->post('region_id')) == 'no' ? 0 : $x;
$oo->host_id = $request->post('host_id_new');
$oo->node_id = $request->post('node_id_new');
$oo->point_id = 0;
@ -133,8 +133,8 @@ class SystemController extends Controller
case 'node':
$request->validate([
'region_id' => ['nullable',new TwoByteInteger],
'host_id' => ['nullable',new TwoByteInteger],
'region_id' => ['required',new TwoByteInteger],
'host_id' => ['required',new TwoByteInteger],
'node_id' => [
'required',
new TwoByteInteger,
@ -142,7 +142,7 @@ class SystemController extends Controller
// Check that the region doesnt already exist
$o = Address::where(function($query) use ($request,$value) {
return $query
->where('host_id',$request->post('host_id_new'))
->where('host_id',$request->post('host_id'))
->where('node_id',$value)
->where('point_id',0)
->where('role',DomainController::NODE_RC);
@ -158,14 +158,16 @@ class SystemController extends Controller
$fail(sprintf('Point numbers must be between 0 and %d',DomainController::NUMBER_MAX));
}],
'hub' => 'required|boolean',
'hub_id' => 'nullable|exists:addresses,id',
]);
$oo = new Address;
$oo->zone_id = $request->post('zone_id');
$oo->region_id = ($x=$request->post('region_id')) == 'no' ? NULL : $x;
$oo->region_id = ($x=$request->post('region_id')) == 'no' ? 0 : $x;
$oo->host_id = $request->post('host_id');
$oo->node_id = $request->post('node_id');
$oo->point_id = $request->post('point_id');
$oo->hub_id = $request->post('hub_id');
$oo->role = (! $oo->point_id) && $request->post('hub') ? DomainController::NODE_HC : NULL;
$oo->active = TRUE;

View File

@ -4,13 +4,14 @@ namespace App\Models;
use Exception;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
use App\Http\Controllers\DomainController;
use App\Traits\ScopeActive;
class Address extends Model
{
use ScopeActive;
use ScopeActive,SoftDeletes;
/* SCOPES */
@ -18,7 +19,7 @@ class Address extends Model
{
return $query
->orderBy('region_id')
->orderByRaw('host_id NULLS FIRST')
->orderBy('host_id')
->orderBy('node_id')
->orderBy('point_id');
}

View File

@ -15,6 +15,7 @@ class Zone extends Model
public function scopeDomainZoneOrder($query)
{
return $query
->select('zones.*')
->join('domains',['domains.id'=>'zones.domain_id'])
->orderBy('domains.name')
->orderBy('zone_id');

View File

@ -0,0 +1,46 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class AddSoftdeletesToAddresses extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::table('addresses', function (Blueprint $table) {
$table->dropUnique(['zone_id','region_id','host_id','node_id']);
$table->dropUnique(['zone_id','host_id','node_id','point_id']);
$table->integer('hub_id')->nullable();
$table->foreign('hub_id')->references('id')->on('addresses');
$table->softDeletes();
});
DB::statement("ALTER TABLE addresses ALTER COLUMN region_id set NOT NULL");
DB::statement("ALTER TABLE addresses ALTER COLUMN host_id set NOT NULL");
DB::statement("CREATE UNIQUE INDEX active_addresses ON addresses (zone_id,region_id,host_id,node_id,point_id) WHERE active = true");
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::table('addresses', function (Blueprint $table) {
$table->dropSoftDeletes();
$table->dropForeign(['hub_id']);
$table->dropColumn('hub_id');
$table->dropIndex('active_addresses');
$table->unique(['zone_id','region_id','host_id','node_id']);
$table->unique(['zone_id','host_id','node_id','point_id']);
});
}
}

View File

@ -68,7 +68,7 @@
</thead>
<tbody>
@foreach ($o->zones as $oz)
@foreach ($o->zones->sortBy('zone_id') as $oz)
<!-- First System Zone -->
<tr>
<td>{{ sprintf('ZC-%s-%05d',$oz->domain->name,$oz->zone_id) }}</td>
@ -80,7 +80,7 @@
</tr>
<!-- Other Nodes -->
@foreach ($oz->addresses()->FTNorder()->with(['system','zone.domain'])->get() as $ao)
@foreach ($oz->addresses()->active()->FTNorder()->whereNull('hub_id')->with(['system','zone.domain'])->get() as $ao)
<tr>
<td>{{ $ao->system->name($ao) }}</td>
<td>{{ $ao->system->sysop }}</td>
@ -89,6 +89,18 @@
<td>{{ $ao->ftn }}</td>
<td>-</td>
</tr>
<!-- If this node is a hub -->
@foreach ($oz->addresses()->active()->FTNorder()->where('hub_id',$ao->id)->with(['system','zone.domain'])->get() as $aoo)
<tr>
<td>{{ $aoo->system->name($aoo) }}</td>
<td>{{ $aoo->system->sysop }}</td>
<td>{{ $ao->system->location }}</td>
<td>{{ $aoo->role }}</td>
<td>{{ $aoo->ftn }}</td>
<td>-</td>
</tr>
@endforeach
@endforeach
@endforeach
</tbody>