Repository interface improvements

This commit is contained in:
Dane Everitt 2017-07-15 11:52:34 -05:00
parent 1f4f6024cc
commit bc3366b10d
No known key found for this signature in database
GPG key ID: EEA66103B3D71F53
23 changed files with 829 additions and 179 deletions

View file

@ -170,115 +170,4 @@ class DatabaseRepository
$database->delete();
});
}
/**
* Deletes a database host from the system if it has no associated databases.
*
* @param int $id
* @return void
*
* @throws \Pterodactyl\Exceptions\DisplayException
*/
public function delete($id)
{
$host = DatabaseHost::withCount('databases')->findOrFail($id);
if ($host->databases_count > 0) {
throw new DisplayException('You cannot delete a database host that has active databases attached to it.');
}
$host->delete();
}
/**
* Adds a new Database Host to the system.
*
* @param array $data
* @return \Pterodactyl\Models\DatabaseHost
*
* @throws \Pterodactyl\Exceptions\DisplayValidationException
*/
public function add(array $data)
{
if (isset($data['host'])) {
$data['host'] = gethostbyname($data['host']);
}
$validator = Validator::make($data, [
'name' => 'required|string|max:255',
'host' => 'required|ip|unique:database_hosts,host',
'port' => 'required|numeric|between:1,65535',
'username' => 'required|string|max:32',
'password' => 'required|string',
'node_id' => 'sometimes|required|exists:nodes,id',
]);
if ($validator->fails()) {
throw new DisplayValidationException(json_encode($validator->errors()));
}
return DB::transaction(function () use ($data) {
$host = new DatabaseHost;
$host->password = Crypt::encrypt($data['password']);
$host->fill([
'name' => $data['name'],
'host' => $data['host'],
'port' => $data['port'],
'username' => $data['username'],
'max_databases' => null,
'node_id' => (isset($data['node_id'])) ? $data['node_id'] : null,
])->save();
// Allows us to check that we can connect to things.
$host->setDynamicConnection();
DB::connection('dynamic')->select('SELECT 1 FROM dual');
return $host;
});
}
/**
* Updates a Database Host on the system.
*
* @param int $id
* @param array $data
* @return \Pterodactyl\Models\DatabaseHost
*
* @throws \Pterodactyl\Exceptions\DisplayValidationException
*/
public function update($id, array $data)
{
$host = DatabaseHost::findOrFail($id);
if (isset($data['host'])) {
$data['host'] = gethostbyname($data['host']);
}
$validator = Validator::make($data, [
'name' => 'sometimes|required|string|max:255',
'host' => 'sometimes|required|ip|unique:database_hosts,host,' . $host->id,
'port' => 'sometimes|required|numeric|between:1,65535',
'username' => 'sometimes|required|string|max:32',
'password' => 'sometimes|required|string',
'node_id' => 'sometimes|required|exists:nodes,id',
]);
if ($validator->fails()) {
throw new DisplayValidationException(json_encode($validator->errors()));
}
return DB::transaction(function () use ($data, $host) {
if (isset($data['password'])) {
$host->password = Crypt::encrypt($data['password']);
}
$host->fill($data)->save();
// Check that we can still connect with these details.
$host->setDynamicConnection();
DB::connection('dynamic')->select('SELECT 1 FROM dual');
return $host;
});
}
}