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

@ -53,7 +53,7 @@ class ServiceController extends BaseController
return [
'service' => $service,
'options' => Models\ServiceOptions::select('id', 'name', 'description', 'tag', 'docker_image')
->where('parent_service', $service->id)
->where('service_id', $service->id)
->with('variables')
->with('packs')
->get(),

View file

@ -42,64 +42,29 @@ class PackController extends Controller
//
}
protected function formatServices()
{
$options = Models\ServiceOptions::select(
'services.name AS p_service',
'service_options.id',
'service_options.name'
)->join('services', 'services.id', '=', 'service_options.parent_service')->get();
$array = [];
foreach ($options as &$option) {
if (! array_key_exists($option->p_service, $array)) {
$array[$option->p_service] = [];
}
$array[$option->p_service] = array_merge($array[$option->p_service], [[
'id' => $option->id,
'name' => $option->name,
]]);
}
return $array;
}
public function listAll(Request $request)
{
return view('admin.services.packs.index', [
'services' => Models\Service::all(),
]);
return view('admin.services.packs.index', ['services' => Models\Service::all()]);
}
public function listByOption(Request $request, $id)
{
$option = Models\ServiceOptions::findOrFail($id);
return view('admin.services.packs.byoption', [
'packs' => Models\ServicePack::where('option', $option->id)->get(),
'service' => Models\Service::findOrFail($option->parent_service),
'option' => $option,
'option' => Models\ServiceOptions::with('service', 'packs')->findOrFail($id)
]);
}
public function listByService(Request $request, $id)
{
return view('admin.services.packs.byservice', [
'service' => Models\Service::findOrFail($id),
'options' => Models\ServiceOptions::select(
'service_options.id',
'service_options.name',
DB::raw('(SELECT COUNT(id) FROM service_packs WHERE service_packs.option = service_options.id) AS p_count')
)->where('parent_service', $id)->get(),
'service' => Models\Service::with('options', 'options.packs')->findOrFail($id),
]);
}
public function new(Request $request, $opt = null)
{
return view('admin.services.packs.new', [
'services' => $this->formatServices(),
'packFor' => $opt,
'services' => Models\Service::with('options')->get(),
]);
}
@ -107,12 +72,18 @@ class PackController extends Controller
{
try {
$repo = new Pack;
$id = $repo->create($request->except([
'_token',
$pack = $repo->create($request->only([
'name',
'version',
'description',
'option',
'selectable',
'visible',
'file_upload',
]));
Alert::success('Successfully created new service!')->flash();
return redirect()->route('admin.services.packs.edit', $id)->withInput();
return redirect()->route('admin.services.packs.edit', $pack->id)->withInput();
} catch (DisplayValidationException $ex) {
return redirect()->route('admin.services.packs.new', $request->input('option'))->withErrors(json_decode($ex->getMessage()))->withInput();
} catch (DisplayException $ex) {
@ -127,15 +98,12 @@ class PackController extends Controller
public function edit(Request $request, $id)
{
$pack = Models\ServicePack::findOrFail($id);
$option = Models\ServiceOptions::select('id', 'parent_service', 'name')->where('id', $pack->option)->first();
$pack = Models\ServicePack::with('option.service')->findOrFail($id);
return view('admin.services.packs.edit', [
'pack' => $pack,
'services' => $this->formatServices(),
'services' => Models\Service::all()->load('options'),
'files' => Storage::files('packs/' . $pack->uuid),
'service' => Models\Service::findOrFail($option->parent_service),
'option' => $option,
]);
}
@ -159,8 +127,13 @@ class PackController extends Controller
} else {
try {
$repo = new Pack;
$repo->update($id, $request->except([
'_token',
$repo->update($id, $request->only([
'name',
'version',
'description',
'option',
'selectable',
'visible',
]));
Alert::success('Service pack has been successfully updated.')->flash();
} catch (DisplayValidationException $ex) {
@ -215,8 +188,7 @@ class PackController extends Controller
public function uploadForm(Request $request, $for = null)
{
return view('admin.services.packs.upload', [
'services' => $this->formatServices(),
'for' => $for,
'services' => Models\Service::all()->load('options'),
]);
}
@ -224,12 +196,10 @@ class PackController extends Controller
{
try {
$repo = new Pack;
$id = $repo->createWithTemplate($request->except([
'_token',
]));
$pack = $repo->createWithTemplate($request->only(['option', 'file_upload']));
Alert::success('Successfully created new service!')->flash();
return redirect()->route('admin.services.packs.edit', $id)->withInput();
return redirect()->route('admin.services.packs.edit', $pack->id)->withInput();
} catch (DisplayValidationException $ex) {
return redirect()->back()->withErrors(json_decode($ex->getMessage()))->withInput();
} catch (DisplayException $ex) {

View file

@ -244,7 +244,7 @@ class ServersController extends Controller
$service = Models\Service::select('executable', 'startup')->where('id', $request->input('service'))->first();
return response()->json(Models\ServiceOptions::select('id', 'name', 'docker_image')->where('parent_service', $request->input('service'))->orderBy('name', 'asc')->get());
return response()->json(Models\ServiceOptions::select('id', 'name', 'docker_image')->where('service_id', $request->input('service'))->orderBy('name', 'asc')->get());
}
/**
@ -264,7 +264,7 @@ class ServersController extends Controller
$option = Models\ServiceOptions::select(
DB::raw('COALESCE(service_options.executable, services.executable) as executable'),
DB::raw('COALESCE(service_options.startup, services.startup) as startup')
)->leftJoin('services', 'services.id', '=', 'service_options.parent_service')
)->leftJoin('services', 'services.id', '=', 'service_options.service_id')
->where('service_options.id', $request->input('option'))
->first();

View file

@ -45,10 +45,7 @@ class ServiceController extends Controller
public function getIndex(Request $request)
{
return view('admin.services.index', [
'services' => Models\Service::select(
'services.*',
DB::raw('(SELECT COUNT(*) FROM servers WHERE servers.service = services.id) as c_servers')
)->get(),
'services' => Models\Service::withCount('servers')->get(),
]);
}
@ -61,12 +58,16 @@ class ServiceController extends Controller
{
try {
$repo = new ServiceRepository\Service;
$id = $repo->create($request->except([
'_token',
$service = $repo->create($request->only([
'name',
'description',
'file',
'executable',
'startup',
]));
Alert::success('Successfully created new service!')->flash();
return redirect()->route('admin.services.service', $id);
return redirect()->route('admin.services.service', $service->id);
} catch (DisplayValidationException $ex) {
return redirect()->route('admin.services.new')->withErrors(json_decode($ex->getMessage()))->withInput();
} catch (DisplayException $ex) {
@ -82,11 +83,7 @@ class ServiceController extends Controller
public function getService(Request $request, $service)
{
return view('admin.services.view', [
'service' => Models\Service::findOrFail($service),
'options' => Models\ServiceOptions::select(
'service_options.*',
DB::raw('(SELECT COUNT(*) FROM servers WHERE servers.option = service_options.id) as c_servers')
)->where('parent_service', $service)->get(),
'service' => Models\Service::with('options', 'options.servers')->findOrFail($service),
]);
}
@ -94,8 +91,12 @@ class ServiceController extends Controller
{
try {
$repo = new ServiceRepository\Service;
$repo->update($service, $request->except([
'_token',
$repo->update($service, $request->only([
'name',
'description',
'file',
'executable',
'startup',
]));
Alert::success('Successfully updated this service.')->flash();
} catch (DisplayValidationException $ex) {
@ -130,16 +131,11 @@ class ServiceController extends Controller
public function getOption(Request $request, $service, $option)
{
$opt = Models\ServiceOptions::findOrFail($option);
$option = Models\ServiceOptions::with('service', 'variables')->findOrFail($option);
$option->setRelation('servers', $option->servers()->with('user')->paginate(25));
return view('admin.services.options.view', [
'service' => Models\Service::findOrFail($opt->parent_service),
'option' => $opt,
'variables' => Models\ServiceVariables::where('option_id', $option)->get(),
'servers' => Models\Server::select('servers.*', 'users.email as a_ownerEmail')
->join('users', 'users.id', '=', 'servers.owner_id')
->where('option', $option)
->paginate(10),
'option' => $option,
]);
}
@ -147,8 +143,13 @@ class ServiceController extends Controller
{
try {
$repo = new ServiceRepository\Option;
$repo->update($option, $request->except([
'_token',
$repo->update($option, $request->only([
'name',
'description',
'tag',
'executable',
'docker_image',
'startup',
]));
Alert::success('Option settings successfully updated.')->flash();
} catch (DisplayValidationException $ex) {
@ -164,13 +165,12 @@ class ServiceController extends Controller
public function deleteOption(Request $request, $service, $option)
{
try {
$service = Models\ServiceOptions::select('parent_service')->where('id', $option)->first();
$repo = new ServiceRepository\Option;
$repo->delete($option);
Alert::success('Successfully deleted that option.')->flash();
return redirect()->route('admin.services.service', $service->parent_service);
return redirect()->route('admin.services.service', $service);
} catch (DisplayException $ex) {
Alert::danger($ex->getMessage())->flash();
} catch (\Exception $ex) {
@ -218,8 +218,7 @@ class ServiceController extends Controller
public function getNewVariable(Request $request, $service, $option)
{
return view('admin.services.options.variable', [
'service' => Models\Service::findOrFail($service),
'option' => Models\ServiceOptions::where('parent_service', $service)->where('id', $option)->firstOrFail(),
'option' => Models\ServiceOptions::with('service')->findOrFail($option),
]);
}
@ -227,8 +226,15 @@ class ServiceController extends Controller
{
try {
$repo = new ServiceRepository\Variable;
$repo->create($option, $request->except([
'_token',
$repo->create($option, $request->only([
'name',
'description',
'env_variable',
'default_value',
'user_viewable',
'user_editable',
'required',
'regex',
]));
Alert::success('Successfully added new variable to this option.')->flash();
@ -305,8 +311,9 @@ class ServiceController extends Controller
{
try {
$repo = new ServiceRepository\Service;
$repo->updateFile($serviceId, $request->except([
'_token',
$repo->updateFile($serviceId, $request->only([
'file',
'contents',
]));
return response('', 204);

View file

@ -224,7 +224,7 @@ class ServerController extends Controller
$service = Models\Service::select(
DB::raw('IFNULL(service_options.executable, services.executable) as executable')
)->leftJoin('service_options', 'service_options.parent_service', '=', 'services.id')
)->leftJoin('service_options', 'service_options.service_id', '=', 'services.id')
->where('service_options.id', $server->option_id)
->where('services.id', $server->service_id)
->first();