Completed model updates for Services

This commit is contained in:
Dane Everitt 2017-02-05 17:58:17 -05:00
parent 09d23deed6
commit 323f1d943f
No known key found for this signature in database
GPG key ID: EEA66103B3D71F53
26 changed files with 299 additions and 167 deletions

View file

@ -163,7 +163,7 @@ class ServerRepository
// We know the service and option exists because of the validation.
// We need to verify that the option exists for the service, and then check for
// any required variable fields. (fields are labeled env_<env_variable>)
$option = Models\ServiceOptions::where('id', $data['option'])->where('parent_service', $data['service'])->first();
$option = Models\ServiceOptions::where('id', $data['option'])->where('service_id', $data['service'])->first();
if (! $option) {
throw new DisplayException('The requested service option does not exist for the specified service.');
}
@ -181,7 +181,7 @@ class ServerRepository
}
// Load up the Service Information
$service = Models\Service::find($option->parent_service);
$service = Models\Service::find($option->service_id);
// Check those Variables
$variables = Models\ServiceVariables::where('option_id', $data['option'])->get();

View file

@ -63,7 +63,7 @@ class Option
}
$option = new Models\ServiceOptions;
$option->parent_service = $service->id;
$option->service_id = $service->id;
$option->fill($data);
$option->save();

View file

@ -69,8 +69,8 @@ class Pack
try {
$uuid = new UuidService;
$pack = Models\ServicePack::create([
'option' => $data['option'],
'uuid' => $uuid->generate('servers', 'uuid'),
'option_id' => $data['option'],
'uuid' => $uuid->generate('service_packs', 'uuid'),
'name' => $data['name'],
'version' => $data['version'],
'description' => (empty($data['description'])) ? null : $data['description'],
@ -89,7 +89,7 @@ class Pack
throw $ex;
}
return $pack->id;
return $pack;
}
public function createWithTemplate(array $data)
@ -123,7 +123,7 @@ class Pack
}
$json = json_decode($zip->getFromName('import.json'));
$id = $this->create([
$pack = $this->create([
'name' => $json->name,
'version' => $json->version,
'description' => $json->description,
@ -132,7 +132,6 @@ class Pack
'visible' => $json->visible,
]);
$pack = Models\ServicePack::findOrFail($id);
if (! $zip->extractTo(storage_path('app/packs/' . $pack->uuid), 'archive.tar.gz')) {
$pack->delete();
throw new DisplayException('Unable to extract the archive file to the correct location.');
@ -140,7 +139,7 @@ class Pack
$zip->close();
return $pack->id;
return $pack;
} else {
$json = json_decode(file_get_contents($data['file_upload']->path()));
@ -170,18 +169,16 @@ class Pack
throw new DisplayValidationException($validator->errors());
}
DB::transaction(function () use ($id, $data) {
Models\ServicePack::findOrFail($id)->update([
'option' => $data['option'],
'name' => $data['name'],
'version' => $data['version'],
'description' => (empty($data['description'])) ? null : $data['description'],
'selectable' => isset($data['selectable']),
'visible' => isset($data['visible']),
]);
Models\ServicePack::findOrFail($id)->update([
'option_id' => $data['option'],
'name' => $data['name'],
'version' => $data['version'],
'description' => (empty($data['description'])) ? null : $data['description'],
'selectable' => isset($data['selectable']),
'visible' => isset($data['visible']),
]);
return true;
});
return;
}
public function delete($id)

View file

@ -55,23 +55,18 @@ class Service
$data['author'] = env('SERVICE_AUTHOR', (string) Uuid::generate(4));
$service = new Models\Service;
DB::beginTransaction();
try {
$service->fill($data);
$service->save();
Storage::put('services/' . $data['file'] . '/main.json', '{}');
Storage::copy('services/.templates/index.js', 'services/' . $data['file'] . '/index.js');
$service = Models\Service::create($data);
Storage::put('services/' . $service->file . '/main.json', '{}');
Storage::copy('services/.templates/index.js', 'services/' . $service->file . '/index.js');
DB::commit();
} catch (\Exception $ex) {
DB::rollBack();
throw $ex;
}
return $service->id;
return $service;
}
public function update($id, array $data)
@ -99,7 +94,7 @@ class Service
{
$service = Models\Service::findOrFail($id);
$servers = Models\Server::where('service', $service->id)->get();
$options = Models\ServiceOptions::select('id')->where('parent_service', $service->id);
$options = Models\ServiceOptions::select('id')->where('service_id', $service->id);
if (count($servers) !== 0) {
throw new DisplayException('You cannot delete a service that has servers associated with it.');

View file

@ -39,7 +39,7 @@ class Variable
public function create($id, array $data)
{
$option = Models\ServiceOptions::findOrFail($id);
$option = Models\ServiceOptions::select('id')->findOrFail($id);
$validator = Validator::make($data, [
'name' => 'required|string|min:1|max:255',
@ -67,21 +67,22 @@ class Variable
$data['user_viewable'] = (isset($data['user_viewable']) && in_array((int) $data['user_viewable'], [0, 1])) ? $data['user_viewable'] : 0;
$data['user_editable'] = (isset($data['user_editable']) && in_array((int) $data['user_editable'], [0, 1])) ? $data['user_editable'] : 0;
$data['required'] = (isset($data['required']) && in_array((int) $data['required'], [0, 1])) ? $data['required'] : 0;
$data['option_id'] = $option->id;
$variable = new Models\ServiceVariables;
$variable->option_id = $option->id;
$variable->fill($data);
$variable = Models\ServiceVariables::create($data);
return $variable->save();
return $variable;
}
public function delete($id)
{
$variable = Models\ServiceVariables::findOrFail($id);
$variable = Models\ServiceVariables::with('serverVariables')->findOrFail($id);
DB::beginTransaction();
try {
Models\ServerVariables::where('variable_id', $variable->id)->delete();
foreach($variable->serverVariables as $svar) {
$svar->delete();
}
$variable->delete();
DB::commit();
@ -125,7 +126,18 @@ class Variable
$data['user_editable'] = (isset($data['user_editable']) && in_array((int) $data['user_editable'], [0, 1])) ? $data['user_editable'] : $variable->user_editable;
$data['required'] = (isset($data['required']) && in_array((int) $data['required'], [0, 1])) ? $data['required'] : $variable->required;
$variable->fill($data);
// Not using $data because the function that passes into this function
// can't do $requst->only() due to the page setup.
$variable->fill([
'name' => $data['name'],
'description' => $data['description'],
'env_variable' => $data['env_variable'],
'default_value' => $data['default_value'],
'user_viewable' => $data['user_viewable'],
'user_editable' => $data['user_editable'],
'required' => $data['required'],
'regex' => $data['regex'],
]);
return $variable->save();
}