@@ -46,8 +46,16 @@ public function __construct()
4646
4747 public function getIndex (Request $ request )
4848 {
49+ $ servers = Models \Server::withTrashed ()->with (
50+ 'node ' , 'user ' , 'allocation '
51+ );
52+
53+ if (! is_null ($ request ->input ('query ' ))) {
54+ $ servers ->search ($ request ->input ('query ' ));
55+ }
56+
4957 return view ('admin.servers.index ' , [
50- 'servers ' => Models \Server:: withTrashed ()-> with ( ' node ' , ' user ' ) ->paginate (25 ),
58+ 'servers ' => $ servers ->paginate (25 ),
5159 ]);
5260 }
5361
@@ -109,13 +117,25 @@ public function postNewServer(Request $request)
109117 */
110118 public function postNewServerGetNodes (Request $ request )
111119 {
112- if (! $ request ->input ('location ' )) {
113- return response ()->json ([
114- 'error ' => 'Missing location in request. ' ,
115- ], 500 );
116- }
117-
118- return response ()->json (Models \Node::select ('id ' , 'name ' , 'public ' )->where ('location_id ' , $ request ->input ('location ' ))->get ());
120+ $ nodes = Models \Node::with ('allocations ' )->where ('location_id ' , $ request ->input ('location ' ))->get ();
121+ return $ nodes ->map (function ($ item ) {
122+ $ filtered = $ item ->allocations ->map (function ($ map ) {
123+ return collect ($ map )->only (['ip ' , 'port ' ]);
124+ });
125+
126+ $ item ->ports = $ filtered ->unique ('ip ' )->map (function ($ map ) use ($ item ) {
127+ return [
128+ 'ip ' => $ map ['ip ' ],
129+ 'ports ' => $ item ->allocations ->where ('ip ' , $ map ['ip ' ])->pluck ('port ' )->all (),
130+ ];
131+ })->values ();
132+
133+ return [
134+ 'id ' => $ item ->id ,
135+ 'text ' => $ item ->name ,
136+ 'allocations ' => $ item ->ports ,
137+ ];
138+ })->values ();
119139 }
120140
121141 /**
@@ -126,24 +146,7 @@ public function postNewServerGetNodes(Request $request)
126146 */
127147 public function postNewServerGetIps (Request $ request )
128148 {
129- if (! $ request ->input ('node ' )) {
130- return response ()->json ([
131- 'error ' => 'Missing node in request. ' ,
132- ], 500 );
133- }
134-
135- $ ips = Models \Allocation::where ('node_id ' , $ request ->input ('node ' ))->whereNull ('server_id ' )->get ();
136- $ listing = [];
137-
138- foreach ($ ips as &$ ip ) {
139- if (array_key_exists ($ ip ->ip , $ listing )) {
140- $ listing [$ ip ->ip ] = array_merge ($ listing [$ ip ->ip ], [$ ip ->port ]);
141- } else {
142- $ listing [$ ip ->ip ] = [$ ip ->port ];
143- }
144- }
145-
146- return response ()->json ($ listing );
149+ return Models \Allocation::select ('id ' , 'ip ' )->where ('node_id ' , $ request ->input ('node ' ))->whereNull ('server_id ' )->get ()->unique ('ip ' )->values ()->all ();
147150 }
148151
149152 /**
0 commit comments