|
24 | 24 |
|
25 | 25 | namespace Pterodactyl\Http\Controllers\Admin; |
26 | 26 |
|
27 | | -use Log; |
28 | | -use Alert; |
29 | | -use Illuminate\Http\Request; |
30 | | -use Pterodactyl\Models\Database; |
31 | 27 | use Pterodactyl\Models\Location; |
32 | 28 | use Pterodactyl\Models\DatabaseHost; |
33 | | -use Pterodactyl\Exceptions\DisplayException; |
| 29 | +use Prologue\Alerts\AlertsMessageBag; |
34 | 30 | use Pterodactyl\Http\Controllers\Controller; |
35 | | -use Pterodactyl\Repositories\DatabaseRepository; |
36 | | -use Pterodactyl\Exceptions\DisplayValidationException; |
| 31 | +use Pterodactyl\Services\DatabaseHostService; |
| 32 | +use Pterodactyl\Http\Requests\Admin\DatabaseHostFormRequest; |
37 | 33 |
|
38 | 34 | class DatabaseController extends Controller |
39 | 35 | { |
| 36 | + /** |
| 37 | + * @var \Prologue\Alerts\AlertsMessageBag |
| 38 | + */ |
| 39 | + protected $alert; |
| 40 | + |
| 41 | + /** |
| 42 | + * @var \Pterodactyl\Models\DatabaseHost |
| 43 | + */ |
| 44 | + protected $hostModel; |
| 45 | + |
| 46 | + /** |
| 47 | + * @var \Pterodactyl\Models\Location |
| 48 | + */ |
| 49 | + protected $locationModel; |
| 50 | + |
| 51 | + /** |
| 52 | + * @var \Pterodactyl\Services\DatabaseHostService |
| 53 | + */ |
| 54 | + protected $service; |
| 55 | + |
| 56 | + /** |
| 57 | + * DatabaseController constructor. |
| 58 | + * |
| 59 | + * @param \Prologue\Alerts\AlertsMessageBag $alert |
| 60 | + * @param \Pterodactyl\Models\DatabaseHost $hostModel |
| 61 | + * @param \Pterodactyl\Models\Location $locationModel |
| 62 | + * @param \Pterodactyl\Services\DatabaseHostService $service |
| 63 | + */ |
| 64 | + public function __construct( |
| 65 | + AlertsMessageBag $alert, |
| 66 | + DatabaseHost $hostModel, |
| 67 | + Location $locationModel, |
| 68 | + DatabaseHostService $service |
| 69 | + ) { |
| 70 | + $this->alert = $alert; |
| 71 | + $this->hostModel = $hostModel; |
| 72 | + $this->locationModel = $locationModel; |
| 73 | + $this->service = $service; |
| 74 | + } |
| 75 | + |
40 | 76 | /** |
41 | 77 | * Display database host index. |
42 | 78 | * |
43 | | - * @param \Illuminate\Http\Request $request |
44 | 79 | * @return \Illuminate\View\View |
45 | 80 | */ |
46 | | - public function index(Request $request) |
| 81 | + public function index() |
47 | 82 | { |
48 | 83 | return view('admin.databases.index', [ |
49 | | - 'locations' => Location::with('nodes')->get(), |
50 | | - 'hosts' => DatabaseHost::withCount('databases')->with('node')->get(), |
| 84 | + 'locations' => $this->locationModel->with('nodes')->get(), |
| 85 | + 'hosts' => $this->hostModel->withCount('databases')->with('node')->get(), |
51 | 86 | ]); |
52 | 87 | } |
53 | 88 |
|
54 | 89 | /** |
55 | 90 | * Display database host to user. |
56 | 91 | * |
57 | | - * @param \Illuminate\Http\Request $request |
58 | | - * @param int $id |
| 92 | + * @param \Pterodactyl\Models\DatabaseHost $host |
59 | 93 | * @return \Illuminate\View\View |
60 | 94 | */ |
61 | | - public function view(Request $request, $id) |
| 95 | + public function view(DatabaseHost $host) |
62 | 96 | { |
| 97 | + $host->load('databases.server'); |
| 98 | + |
63 | 99 | return view('admin.databases.view', [ |
64 | | - 'locations' => Location::with('nodes')->get(), |
65 | | - 'host' => DatabaseHost::with('databases.server')->findOrFail($id), |
| 100 | + 'locations' => $this->locationModel->with('nodes')->get(), |
| 101 | + 'host' => $host, |
66 | 102 | ]); |
67 | 103 | } |
68 | 104 |
|
69 | 105 | /** |
70 | | - * Handle post request to create database host. |
| 106 | + * Handle request to create a new database host. |
71 | 107 | * |
72 | | - * @param \Illuminate\Http\Request $request |
| 108 | + * @param \Pterodactyl\Http\Requests\Admin\DatabaseHostFormRequest $request |
73 | 109 | * @return \Illuminate\Http\RedirectResponse |
| 110 | + * |
| 111 | + * @throws \Throwable |
74 | 112 | */ |
75 | | - public function create(Request $request) |
| 113 | + public function create(DatabaseHostFormRequest $request) |
76 | 114 | { |
77 | | - $repo = new DatabaseRepository; |
78 | | - |
79 | 115 | try { |
80 | | - $host = $repo->add($request->intersect([ |
81 | | - 'name', 'username', 'password', |
82 | | - 'host', 'port', 'node_id', |
83 | | - ])); |
84 | | - Alert::success('Successfully created new database host on the system.')->flash(); |
| 116 | + $host = $this->service->create($request->normalize()); |
| 117 | + $this->alert->success('Successfully created a new database host on the system.')->flash(); |
85 | 118 |
|
86 | 119 | return redirect()->route('admin.databases.view', $host->id); |
87 | 120 | } catch (\PDOException $ex) { |
88 | | - Alert::danger($ex->getMessage())->flash(); |
89 | | - } catch (DisplayValidationException $ex) { |
90 | | - return redirect()->route('admin.databases')->withErrors(json_decode($ex->getMessage())); |
91 | | - } catch (\Exception $ex) { |
92 | | - Log::error($ex); |
93 | | - Alert::danger('An error was encountered while trying to process this request. This error has been logged.')->flash(); |
| 121 | + $this->alert->danger($ex->getMessage())->flash(); |
94 | 122 | } |
95 | 123 |
|
96 | 124 | return redirect()->route('admin.databases'); |
97 | 125 | } |
98 | 126 |
|
99 | 127 | /** |
100 | | - * Handle post request to update a database host. |
| 128 | + * Handle updating database host. |
101 | 129 | * |
102 | | - * @param \Illuminate\Http\Request $request |
103 | | - * @param int $id |
| 130 | + * @param \Pterodactyl\Http\Requests\Admin\DatabaseHostFormRequest $request |
| 131 | + * @param \Pterodactyl\Models\DatabaseHost $host |
104 | 132 | * @return \Illuminate\Http\RedirectResponse |
| 133 | + * |
| 134 | + * @throws \Pterodactyl\Exceptions\DisplayException |
105 | 135 | */ |
106 | | - public function update(Request $request, $id) |
| 136 | + public function update(DatabaseHostFormRequest $request, DatabaseHost $host) |
107 | 137 | { |
108 | | - $repo = new DatabaseRepository; |
| 138 | + if ($request->input('action') === 'delete') { |
| 139 | + return $this->delete($host); |
| 140 | + } |
109 | 141 |
|
110 | 142 | try { |
111 | | - if ($request->input('action') !== 'delete') { |
112 | | - $host = $repo->update($id, $request->intersect([ |
113 | | - 'name', 'username', 'password', |
114 | | - 'host', 'port', 'node_id', |
115 | | - ])); |
116 | | - Alert::success('Database host was updated successfully.')->flash(); |
117 | | - } else { |
118 | | - $repo->delete($id); |
119 | | - |
120 | | - return redirect()->route('admin.databases'); |
121 | | - } |
| 143 | + $host = $this->service->update($host->id, $request->normalize()); |
| 144 | + $this->alert->success('Database host was updated successfully.')->flash(); |
122 | 145 | } catch (\PDOException $ex) { |
123 | | - Alert::danger($ex->getMessage())->flash(); |
124 | | - } catch (DisplayException $ex) { |
125 | | - Alert::danger($ex->getMessage())->flash(); |
126 | | - } catch (DisplayValidationException $ex) { |
127 | | - return redirect()->route('admin.databases.view', $id)->withErrors(json_decode($ex->getMessage())); |
128 | | - } catch (\Exception $ex) { |
129 | | - Log::error($ex); |
130 | | - Alert::danger('An error was encountered while trying to process this request. This error has been logged.')->flash(); |
| 146 | + $this->alert->danger($ex->getMessage())->flash(); |
131 | 147 | } |
132 | 148 |
|
133 | | - return redirect()->route('admin.databases.view', $id); |
| 149 | + return redirect()->route('admin.databases.view', $host->id); |
| 150 | + } |
| 151 | + |
| 152 | + /** |
| 153 | + * Handle request to delete a database host. |
| 154 | + * |
| 155 | + * @param \Pterodactyl\Models\DatabaseHost $host |
| 156 | + * @return \Illuminate\Http\RedirectResponse |
| 157 | + * |
| 158 | + * @throws \Pterodactyl\Exceptions\DisplayException |
| 159 | + */ |
| 160 | + public function delete(DatabaseHost $host) |
| 161 | + { |
| 162 | + $this->service->delete($host->id); |
| 163 | + $this->alert->success('The requested database host has been deleted from the system.')->flash(); |
| 164 | + |
| 165 | + return redirect()->route('admin.databases'); |
134 | 166 | } |
135 | 167 | } |
0 commit comments