From 1f04f8374eda20f6db63dc7f0fe8cd216067230f Mon Sep 17 00:00:00 2001 From: Deon George Date: Fri, 25 Jun 2021 16:42:12 +1000 Subject: [PATCH] Address input fixes, region_id/host_id must not be null, address constraints changes --- app/Http/Controllers/DomainController.php | 4 +- app/Http/Controllers/SystemController.php | 18 ++++---- app/Models/Address.php | 5 +- app/Models/Zone.php | 1 + ...25_035054_add_softdeletes_to_addresses.php | 46 +++++++++++++++++++ resources/views/domain/view.blade.php | 16 ++++++- 6 files changed, 77 insertions(+), 13 deletions(-) create mode 100644 database/migrations/2021_06_25_035054_add_softdeletes_to_addresses.php diff --git a/app/Http/Controllers/DomainController.php b/app/Http/Controllers/DomainController.php index 7dd3ed6..a51d46e 100644 --- a/app/Http/Controllers/DomainController.php +++ b/app/Http/Controllers/DomainController.php @@ -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)]; }); } diff --git a/app/Http/Controllers/SystemController.php b/app/Http/Controllers/SystemController.php index 95a8baf..ed8974b 100644 --- a/app/Http/Controllers/SystemController.php +++ b/app/Http/Controllers/SystemController.php @@ -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; diff --git a/app/Models/Address.php b/app/Models/Address.php index 1adfaa4..4be748f 100644 --- a/app/Models/Address.php +++ b/app/Models/Address.php @@ -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'); } diff --git a/app/Models/Zone.php b/app/Models/Zone.php index ab2595f..736189f 100644 --- a/app/Models/Zone.php +++ b/app/Models/Zone.php @@ -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'); diff --git a/database/migrations/2021_06_25_035054_add_softdeletes_to_addresses.php b/database/migrations/2021_06_25_035054_add_softdeletes_to_addresses.php new file mode 100644 index 0000000..0de967b --- /dev/null +++ b/database/migrations/2021_06_25_035054_add_softdeletes_to_addresses.php @@ -0,0 +1,46 @@ +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']); + }); + } +} diff --git a/resources/views/domain/view.blade.php b/resources/views/domain/view.blade.php index aa01a2a..791dd4a 100644 --- a/resources/views/domain/view.blade.php +++ b/resources/views/domain/view.blade.php @@ -68,7 +68,7 @@ - @foreach ($o->zones as $oz) + @foreach ($o->zones->sortBy('zone_id') as $oz) {{ sprintf('ZC-%s-%05d',$oz->domain->name,$oz->zone_id) }} @@ -80,7 +80,7 @@ - @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) {{ $ao->system->name($ao) }} {{ $ao->system->sysop }} @@ -89,6 +89,18 @@ {{ $ao->ftn }} - + + + @foreach ($oz->addresses()->active()->FTNorder()->where('hub_id',$ao->id)->with(['system','zone.domain'])->get() as $aoo) + + {{ $aoo->system->name($aoo) }} + {{ $aoo->system->sysop }} + {{ $ao->system->location }} + {{ $aoo->role }} + {{ $aoo->ftn }} + - + + @endforeach @endforeach @endforeach