|
26 | 26 |
|
27 | 27 | use Log; |
28 | 28 | use Alert; |
29 | | -use Pterodactyl\Models; |
30 | 29 | use Illuminate\Http\Request; |
| 30 | +use Pterodactyl\Models\Database; |
| 31 | +use Pterodactyl\Models\Location; |
| 32 | +use Pterodactyl\Models\DatabaseHost; |
31 | 33 | use Pterodactyl\Exceptions\DisplayException; |
32 | 34 | use Pterodactyl\Http\Controllers\Controller; |
33 | 35 | use Pterodactyl\Repositories\DatabaseRepository; |
|
36 | 38 | class DatabaseController extends Controller |
37 | 39 | { |
38 | 40 | /** |
39 | | - * Controller Constructor. |
| 41 | + * Display database host index. |
| 42 | + * |
| 43 | + * @param Request $request |
| 44 | + * @return \Illuminate\View\View |
40 | 45 | */ |
41 | | - public function __construct() |
42 | | - { |
43 | | - // |
44 | | - } |
45 | | - |
46 | | - public function getIndex(Request $request) |
| 46 | + public function index(Request $request) |
47 | 47 | { |
48 | 48 | return view('admin.databases.index', [ |
49 | | - 'databases' => Models\Database::with('server')->paginate(50), |
50 | | - 'hosts' => Models\DatabaseServer::withCount('databases')->with('node')->paginate(20), |
| 49 | + 'locations' => Location::with('nodes')->get(), |
| 50 | + 'hosts' => DatabaseHost::withCount('databases')->with('node')->get(), |
51 | 51 | ]); |
52 | 52 | } |
53 | 53 |
|
54 | | - public function getNew(Request $request) |
| 54 | + /** |
| 55 | + * Display database host to user. |
| 56 | + * |
| 57 | + * @param Request $request |
| 58 | + * @param int $id |
| 59 | + * @return \Illuminate\View\View |
| 60 | + */ |
| 61 | + public function view(Request $request, $id) |
55 | 62 | { |
56 | | - return view('admin.databases.new', [ |
57 | | - 'nodes' => Models\Node::all()->load('location'), |
| 63 | + return view('admin.databases.view', [ |
| 64 | + 'locations' => Location::with('nodes')->get(), |
| 65 | + 'host' => DatabaseHost::with('databases.server')->findOrFail($id), |
58 | 66 | ]); |
59 | 67 | } |
60 | 68 |
|
61 | | - public function postNew(Request $request) |
| 69 | + /** |
| 70 | + * Handle post request to create database host. |
| 71 | + * |
| 72 | + * @param Request $request |
| 73 | + * @return \Illuminate\Response\RedirectResponse |
| 74 | + */ |
| 75 | + public function create(Request $request) |
62 | 76 | { |
| 77 | + $repo = new DatabaseRepository; |
| 78 | + |
63 | 79 | try { |
64 | | - $repo = new DatabaseRepository; |
65 | | - $repo->add($request->only([ |
66 | | - 'name', |
67 | | - 'host', |
68 | | - 'port', |
69 | | - 'username', |
70 | | - 'password', |
71 | | - 'linked_node', |
| 80 | + $host = $repo->add($request->intersect([ |
| 81 | + 'name', 'username', 'password', |
| 82 | + 'host', 'port', 'node_id', |
72 | 83 | ])); |
73 | | - Alert::success('Successfully added a new database server to the system.')->flash(); |
| 84 | + Alert::success('Successfully created new database host on the system.')->flash(); |
74 | 85 |
|
75 | | - return redirect()->route('admin.databases', ['tab' => 'tab_dbservers']); |
| 86 | + return redirect()->route('admin.databases.view', $host->id); |
| 87 | + } catch (\PDOException $ex) { |
| 88 | + Alert::danger($ex->getMessage())->flash(); |
76 | 89 | } catch (DisplayValidationException $ex) { |
77 | | - return redirect()->route('admin.databases.new')->withErrors(json_decode($ex->getMessage()))->withInput(); |
78 | | - } catch (\Exception $ex) { |
79 | | - if ($ex instanceof DisplayException || $ex instanceof \PDOException) { |
80 | | - Alert::danger($ex->getMessage())->flash(); |
81 | | - } else { |
82 | | - Log::error($ex); |
83 | | - Alert::danger('An error occurred while attempting to delete this database server from the system.')->flash(); |
84 | | - } |
85 | | - |
86 | | - return redirect()->route('admin.databases.new')->withInput(); |
87 | | - } |
88 | | - } |
89 | | - |
90 | | - public function deleteDatabase(Request $request, $id) |
91 | | - { |
92 | | - try { |
93 | | - $repo = new DatabaseRepository; |
94 | | - $repo->drop($id); |
| 90 | + return redirect()->route('admin.databases')->withErrors(json_decode($ex->getMessage())); |
95 | 91 | } catch (\Exception $ex) { |
96 | 92 | Log::error($ex); |
97 | | - |
98 | | - return response()->json([ |
99 | | - 'error' => ($ex instanceof DisplayException) ? $ex->getMessage() : 'An error occurred while attempting to delete this database from the system.', |
100 | | - ], 500); |
| 93 | + Alert::danger('An error was encountered while trying to process this request. This error has been logged.')->flash(); |
101 | 94 | } |
| 95 | + |
| 96 | + return redirect()->route('admin.databases'); |
102 | 97 | } |
103 | 98 |
|
104 | | - public function deleteServer(Request $request, $id) |
| 99 | + /** |
| 100 | + * Handle post request to update a database host. |
| 101 | + * |
| 102 | + * @param Request $request |
| 103 | + * @param int $id |
| 104 | + * @return \Illuminate\Response\RedirectResponse |
| 105 | + */ |
| 106 | + public function update(Request $request, $id) |
105 | 107 | { |
| 108 | + $repo = new DatabaseRepository; |
| 109 | + |
106 | 110 | try { |
107 | | - $repo = new DatabaseRepository; |
108 | | - $repo->delete($id); |
| 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 | + } |
| 122 | + } 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())); |
109 | 128 | } catch (\Exception $ex) { |
110 | 129 | Log::error($ex); |
111 | | - |
112 | | - return response()->json([ |
113 | | - 'error' => ($ex instanceof DisplayException) ? $ex->getMessage() : 'An error occurred while attempting to delete this database server from the system.', |
114 | | - ], 500); |
| 130 | + Alert::danger('An error was encountered while trying to process this request. This error has been logged.')->flash(); |
115 | 131 | } |
| 132 | + |
| 133 | + return redirect()->route('admin.databases.view', $id); |
116 | 134 | } |
117 | 135 | } |
0 commit comments