|
24 | 24 |
|
25 | 25 | namespace Pterodactyl\Http\Controllers\Admin; |
26 | 26 |
|
27 | | -use Log; |
28 | | -use Alert; |
29 | | -use Illuminate\Http\Request; |
30 | 27 | use Pterodactyl\Models\Location; |
31 | | -use Pterodactyl\Exceptions\DisplayException; |
| 28 | +use Prologue\Alerts\AlertsMessageBag; |
| 29 | +use Pterodactyl\Services\LocationService; |
32 | 30 | use Pterodactyl\Http\Controllers\Controller; |
33 | | -use Pterodactyl\Repositories\LocationRepository; |
34 | | -use Pterodactyl\Exceptions\DisplayValidationException; |
| 31 | +use Pterodactyl\Exceptions\DisplayException; |
| 32 | +use Pterodactyl\Http\Requests\Admin\LocationRequest; |
35 | 33 |
|
36 | 34 | class LocationController extends Controller |
37 | 35 | { |
| 36 | + /** |
| 37 | + * @var \Prologue\Alerts\AlertsMessageBag |
| 38 | + */ |
| 39 | + protected $alert; |
| 40 | + |
| 41 | + /** |
| 42 | + * @var \Pterodactyl\Models\Location |
| 43 | + */ |
| 44 | + protected $location; |
| 45 | + |
| 46 | + /** |
| 47 | + * @var \Pterodactyl\Services\LocationService |
| 48 | + */ |
| 49 | + protected $service; |
| 50 | + |
| 51 | + /** |
| 52 | + * LocationController constructor. |
| 53 | + * |
| 54 | + * @param \Prologue\Alerts\AlertsMessageBag $alert |
| 55 | + * @param \Pterodactyl\Models\Location $location |
| 56 | + * @param \Pterodactyl\Services\LocationService $service |
| 57 | + */ |
| 58 | + public function __construct(AlertsMessageBag $alert, Location $location, LocationService $service) |
| 59 | + { |
| 60 | + $this->alert = $alert; |
| 61 | + $this->location = $location; |
| 62 | + $this->service = $service; |
| 63 | + } |
| 64 | + |
38 | 65 | /** |
39 | 66 | * Return the location overview page. |
40 | 67 | * |
41 | | - * @param \Illuminate\Http\Request $request |
42 | 68 | * @return \Illuminate\View\View |
43 | 69 | */ |
44 | | - public function index(Request $request) |
| 70 | + public function index() |
45 | 71 | { |
46 | 72 | return view('admin.locations.index', [ |
47 | | - 'locations' => Location::withCount('nodes', 'servers')->get(), |
| 73 | + 'locations' => $this->location->withCount('nodes', 'servers')->get(), |
48 | 74 | ]); |
49 | 75 | } |
50 | 76 |
|
51 | 77 | /** |
52 | 78 | * Return the location view page. |
53 | 79 | * |
54 | | - * @param \Illuminate\Http\Request $request |
55 | | - * @param int $id |
| 80 | + * @param \Pterodactyl\Models\Location $location |
56 | 81 | * @return \Illuminate\View\View |
57 | 82 | */ |
58 | | - public function view(Request $request, $id) |
| 83 | + public function view(Location $location) |
59 | 84 | { |
60 | | - return view('admin.locations.view', ['location' => Location::with('nodes.servers')->findOrFail($id)]); |
| 85 | + $location->load('nodes.servers'); |
| 86 | + |
| 87 | + return view('admin.locations.view', ['location' => $location]); |
61 | 88 | } |
62 | 89 |
|
63 | 90 | /** |
64 | 91 | * Handle request to create new location. |
65 | 92 | * |
66 | | - * @param \Illuminate\Http\Request $request |
| 93 | + * @param \Pterodactyl\Http\Requests\Admin\LocationRequest $request |
67 | 94 | * @return \Illuminate\Http\RedirectResponse |
| 95 | + * |
| 96 | + * @throws \Throwable |
| 97 | + * @throws \Watson\Validating\ValidationException |
68 | 98 | */ |
69 | | - public function create(Request $request) |
| 99 | + public function create(LocationRequest $request) |
70 | 100 | { |
71 | | - $repo = new LocationRepository; |
72 | | - |
73 | | - try { |
74 | | - $location = $repo->create($request->intersect(['short', 'long'])); |
75 | | - Alert::success('Location was created successfully.')->flash(); |
76 | | - |
77 | | - return redirect()->route('admin.locations.view', $location->id); |
78 | | - } catch (DisplayValidationException $ex) { |
79 | | - return redirect()->route('admin.locations')->withErrors(json_decode($ex->getMessage())); |
80 | | - } catch (\Exception $ex) { |
81 | | - Log::error($ex); |
82 | | - Alert::error('An unhandled exception occurred while processing this request. This error has been logged.')->flash(); |
83 | | - } |
| 101 | + $location = $this->service->create($request->normalize()); |
| 102 | + $this->alert->success('Location was created successfully.')->flash(); |
84 | 103 |
|
85 | | - return redirect()->route('admin.locations'); |
| 104 | + return redirect()->route('admin.locations.view', $location->id); |
86 | 105 | } |
87 | 106 |
|
88 | 107 | /** |
89 | 108 | * Handle request to update or delete location. |
90 | 109 | * |
91 | | - * @param \Illuminate\Http\Request $request |
92 | | - * @param int $id |
| 110 | + * @param \Pterodactyl\Http\Requests\Admin\LocationRequest $request |
| 111 | + * @param \Pterodactyl\Models\Location $location |
93 | 112 | * @return \Illuminate\Http\RedirectResponse |
| 113 | + * |
| 114 | + * @throws \Throwable |
| 115 | + * @throws \Watson\Validating\ValidationException |
94 | 116 | */ |
95 | | - public function update(Request $request, $id) |
| 117 | + public function update(LocationRequest $request, Location $location) |
96 | 118 | { |
97 | | - $repo = new LocationRepository; |
| 119 | + if ($request->input('action') === 'delete') { |
| 120 | + return $this->delete($location); |
| 121 | + } |
98 | 122 |
|
| 123 | + $this->service->update($location, $request->normalize()); |
| 124 | + $this->alert->success('Location was updated successfully.')->flash(); |
| 125 | + |
| 126 | + return redirect()->route('admin.locations.view', $location->id); |
| 127 | + } |
| 128 | + |
| 129 | + /** |
| 130 | + * Delete a location from the system. |
| 131 | + * |
| 132 | + * @param \Pterodactyl\Models\Location $location |
| 133 | + * @return \Illuminate\Http\RedirectResponse |
| 134 | + * |
| 135 | + * @throws \Exception |
| 136 | + * @throws \Pterodactyl\Exceptions\DisplayException |
| 137 | + */ |
| 138 | + public function delete(Location $location) |
| 139 | + { |
99 | 140 | try { |
100 | | - if ($request->input('action') !== 'delete') { |
101 | | - $location = $repo->update($id, $request->intersect(['short', 'long'])); |
102 | | - Alert::success('Location was updated successfully.')->flash(); |
103 | | - } else { |
104 | | - $repo->delete($id); |
105 | | - |
106 | | - return redirect()->route('admin.locations'); |
107 | | - } |
108 | | - } catch (DisplayValidationException $ex) { |
109 | | - return redirect()->route('admin.locations.view', $id)->withErrors(json_decode($ex->getMessage())); |
| 141 | + $this->service->delete($location); |
| 142 | + |
| 143 | + return redirect()->route('admin.locations'); |
110 | 144 | } catch (DisplayException $ex) { |
111 | | - Alert::danger($ex->getMessage())->flash(); |
112 | | - } catch (\Exception $ex) { |
113 | | - Log::error($ex); |
114 | | - Alert::error('An unhandled exception occurred while processing this request. This error has been logged.')->flash(); |
| 145 | + $this->alert->danger($ex->getMessage())->flash(); |
115 | 146 | } |
116 | 147 |
|
117 | | - return redirect()->route('admin.locations.view', $id); |
| 148 | + return redirect()->route('admin.locations.view', $location->id); |
118 | 149 | } |
119 | 150 | } |
0 commit comments