33namespace Pterodactyl \Http \Controllers \Api \Client \Servers ;
44
55use Pterodactyl \Models \Server ;
6+ use Pterodactyl \Exceptions \DisplayException ;
7+ use Pterodactyl \Repositories \Eloquent \ServerRepository ;
8+ use Illuminate \Database \Eloquent \ModelNotFoundException ;
69use Pterodactyl \Repositories \Eloquent \AllocationRepository ;
710use Pterodactyl \Transformers \Api \Client \AllocationTransformer ;
811use Pterodactyl \Http \Controllers \Api \Client \ClientApiController ;
912use Pterodactyl \Http \Requests \Api \Client \Servers \Network \GetNetworkRequest ;
13+ use Pterodactyl \Http \Requests \Api \Client \Servers \Network \SetPrimaryAllocationRequest ;
1014
1115class NetworkController extends ClientApiController
1216{
@@ -15,16 +19,25 @@ class NetworkController extends ClientApiController
1519 */
1620 private $ repository ;
1721
22+ /**
23+ * @var \Pterodactyl\Repositories\Eloquent\ServerRepository
24+ */
25+ private $ serverRepository ;
26+
1827 /**
1928 * NetworkController constructor.
2029 *
2130 * @param \Pterodactyl\Repositories\Eloquent\AllocationRepository $repository
31+ * @param \Pterodactyl\Repositories\Eloquent\ServerRepository $serverRepository
2232 */
23- public function __construct (AllocationRepository $ repository )
24- {
33+ public function __construct (
34+ AllocationRepository $ repository ,
35+ ServerRepository $ serverRepository
36+ ) {
2537 parent ::__construct ();
2638
2739 $ this ->repository = $ repository ;
40+ $ this ->serverRepository = $ serverRepository ;
2841 }
2942
3043 /**
@@ -37,11 +50,40 @@ public function __construct(AllocationRepository $repository)
3750 */
3851 public function index (GetNetworkRequest $ request , Server $ server ): array
3952 {
40- $ allocations = $ this ->repository ->findWhere ([
41- ['server_id ' , '= ' , $ server ->id ],
42- ]);
53+ return $ this ->fractal ->collection ($ server ->allocations )
54+ ->transformWith ($ this ->getTransformer (AllocationTransformer::class))
55+ ->toArray ();
56+ }
57+
58+ /**
59+ * Set the primary allocation for a server.
60+ *
61+ * @param \Pterodactyl\Http\Requests\Api\Client\Servers\Network\SetPrimaryAllocationRequest $request
62+ * @param \Pterodactyl\Models\Server $server
63+ * @return array
64+ *
65+ * @throws \Pterodactyl\Exceptions\DisplayException
66+ * @throws \Pterodactyl\Exceptions\Repository\RecordNotFoundException
67+ * @throws \Pterodactyl\Exceptions\Model\DataValidationException
68+ */
69+ public function storePrimary (SetPrimaryAllocationRequest $ request , Server $ server ): array
70+ {
71+ try {
72+ /** @var \Pterodactyl\Models\Allocation $allocation */
73+ $ allocation = $ this ->repository ->findFirstWhere ([
74+ 'server_id ' => $ server ->id ,
75+ 'ip ' => $ request ->input ('ip ' ),
76+ 'port ' => $ request ->input ('port ' ),
77+ ]);
78+ } catch (ModelNotFoundException $ exception ) {
79+ throw new DisplayException (
80+ 'The IP and port you selected are not available for this server. '
81+ );
82+ }
83+
84+ $ this ->serverRepository ->update ($ server ->id , ['allocation_id ' => $ allocation ->id ]);
4385
44- return $ this ->fractal ->collection ( $ allocations )
86+ return $ this ->fractal ->item ( $ allocation )
4587 ->transformWith ($ this ->getTransformer (AllocationTransformer::class))
4688 ->toArray ();
4789 }
0 commit comments