diff --git a/app/Http/Controllers/SystemController.php b/app/Http/Controllers/SystemController.php index fcdea18..307f637 100644 --- a/app/Http/Controllers/SystemController.php +++ b/app/Http/Controllers/SystemController.php @@ -173,24 +173,42 @@ class SystemController extends Controller 'required', new TwoByteInteger, function ($attribute,$value,$fail) use ($request) { - // Check that the region doesnt already exist + if ($request->point_id === 0) { + // Check that the host doesnt already exist + $o = Address::where(function($query) use ($request,$value) { + return $query + ->where('zone_id',$request->post('zone_id')) + ->where('host_id',$request->post('host_id')) + ->where('node_id',$value) + ->where('point_id',0); + }); + + if ($o->count()) { + $fail(sprintf('Host already exists: %s',$o->get()->pluck('ftn')->join(','))); + } + } + }, + ], + 'point_id' => [ + 'required', + function($attribute,$value,$fail) use ($request) { + if (! is_numeric($value) || $value > DomainController::NUMBER_MAX) + $fail(sprintf('Point numbers must be between 0 and %d',DomainController::NUMBER_MAX)); + + // Check that the host doesnt already exist $o = Address::where(function($query) use ($request,$value) { return $query ->where('zone_id',$request->post('zone_id')) ->where('host_id',$request->post('host_id')) - ->where('node_id',$value) - ->where('point_id',0); + ->where('node_id',$request->post('node_id')) + ->where('point_id',$value); }); if ($o->count()) { - $fail(sprintf('Host already exists: %s',$o->get()->pluck('ftn')->join(','))); + $fail(sprintf('Point already exists: %s',$o->get()->pluck('ftn')->join(','))); } - }, + } ], - 'point_id' => ['required',function($attribute,$value,$fail) { - if (! is_numeric($value) || $value > DomainController::NUMBER_MAX) - $fail(sprintf('Point numbers must be between 0 and %d',DomainController::NUMBER_MAX)); - }], 'hub' => 'required|boolean', 'hub_id' => 'nullable|exists:addresses,id', ]); @@ -202,7 +220,7 @@ class SystemController extends Controller $oo->node_id = $request->post('node_id'); $oo->point_id = $request->post('point_id'); $oo->hub_id = $request->post('hub_id') > 0 ? $request->post('hub_id') : NULL; - $oo->role = ((! $oo->point_id) && $request->post('hub')) ? Address::NODE_HC : Address::NODE_ACTIVE; + $oo->role = ((! $oo->point_id) && $request->post('hub')) ? Address::NODE_HC : ($request->post('point_id') ? Address::NODE_POINT : Address::NODE_ACTIVE); $oo->active = TRUE; $o->addresses()->save($oo);