139 lines
4.5 KiB
PHP
139 lines
4.5 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers\admin\package;
|
|
|
|
use App\Http\Controllers\Controller;
|
|
use App\Http\Requests\Package\OsVersionRequest;
|
|
use App\Models\ModuleDatacenter;
|
|
use App\Models\ModuleDatastore;
|
|
use App\Models\ModuleHost;
|
|
use App\Models\Os;
|
|
use App\Models\OsGroup;
|
|
use App\Models\vCenter;
|
|
use App\Http\Services\vCenter as ServiceVCenter;
|
|
use App\Repositories\Os\OsRepositoryInterface;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Support\Facades\DB;
|
|
use Illuminate\Support\Facades\Log;
|
|
use Illuminate\Support\Facades\Validator;
|
|
use Yajra\DataTables\Facades\DataTables;
|
|
|
|
class OsVersion extends Controller
|
|
{
|
|
public function __construct(
|
|
public OsRepositoryInterface $osRepository
|
|
) {}
|
|
public function index(Request $request)
|
|
{
|
|
$versions = $this->osRepository->filter([
|
|
'page' => $request->get('page') ?? 1
|
|
], ['osGroup.host']);
|
|
$title = 'List all Version OS';
|
|
|
|
return view('administrator.pages.package.version.index', compact('versions', 'title'));
|
|
}
|
|
public function create()
|
|
{
|
|
$title = 'Add new Version Os';
|
|
|
|
return view('administrator.pages.package.version.add', compact('title'));
|
|
}
|
|
public function store(OsVersionRequest $request)
|
|
{
|
|
$validatedData = $request->validated();
|
|
|
|
unset($validatedData['module_datacenter_id']);
|
|
|
|
DB::beginTransaction();
|
|
try {
|
|
$this->osRepository->create($validatedData);
|
|
DB::commit();
|
|
} catch (\Exception $error) {
|
|
DB::rollBack();
|
|
return back()->withErrors($error->getMessage());
|
|
}
|
|
return redirect()->route('package.version.index')->with('success', 'Add Os Version successfully');
|
|
}
|
|
public function edit(int $id = 0)
|
|
{
|
|
$version = $this->osRepository->findWithRelationships($id, ['osGroup.host.datacenter.vCenter']);
|
|
if (! $version) {
|
|
return back()->withErrors('Can\'t find version os by ID #' . $id);
|
|
}
|
|
|
|
$title = "Edit version OS";
|
|
|
|
return view('administrator.pages.package.version.edit', compact('title', 'version'));
|
|
}
|
|
public function update(int $id = 0, OsVersionRequest $request)
|
|
{
|
|
$version = $this->osRepository->find($id);
|
|
if (! $version) {
|
|
return back()->withErrors('Can\'t find version os by ID #' . $id);
|
|
}
|
|
|
|
$validatedData = $request->validated();
|
|
|
|
// Remove unnecessary fields
|
|
unset($validatedData['vcenter_id'], $validatedData['module_datacenter_id']);
|
|
|
|
DB::beginTransaction();
|
|
try {
|
|
$this->osRepository->update($id, $validatedData);
|
|
DB::commit();
|
|
} catch (\Exception $error) {
|
|
DB::rollBack();
|
|
return back()->withErrors($error->getMessage());
|
|
}
|
|
return redirect()->route('package.version.index')->with('success', 'Update successfully');
|
|
}
|
|
public function destroy($id)
|
|
{
|
|
try {
|
|
$version = $this->osRepository->find($id);
|
|
if (!$version) {
|
|
return back()->withErrors('Can\'t find version os by ID #' . $id);
|
|
}
|
|
|
|
$version->delete();
|
|
|
|
return redirect()->route('package.version.index')
|
|
->with('success', 'OS Version deleted successfully');
|
|
} catch (\Exception $error) {
|
|
return back()->withErrors('Failed to delete OS Version: ' . $error->getMessage());
|
|
}
|
|
}
|
|
|
|
public function datatable(Request $request)
|
|
{
|
|
$query = $this->osRepository->filter(
|
|
[],
|
|
['osGroup'],
|
|
[],
|
|
['*'],
|
|
true
|
|
);
|
|
|
|
return DataTables::eloquent($query)
|
|
->editColumn('os_group', function ($d) {
|
|
return $d->osGroup ? $d->osGroup->name . ' - ' . $d->osGroup->host->name : '';
|
|
})
|
|
->editColumn('status', function ($d) {
|
|
return $d->getStatus();
|
|
})
|
|
->addColumn('action', function ($d) {
|
|
return view('administrator.pages.package.version.datatable.action', [
|
|
'id' => $d->id,
|
|
'version' => $d->version
|
|
])->render();
|
|
})
|
|
->filterColumn('host', function ($query, $keyword) {
|
|
$query->whereHas('host', function ($q) use ($keyword) {
|
|
$q->where('host', 'like', "%{$keyword}%");
|
|
});
|
|
})
|
|
->rawColumns(['action', 'status'])
|
|
->make(true);
|
|
}
|
|
}
|