154 lines
5.1 KiB
PHP
154 lines
5.1 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers\admin\package;
|
|
|
|
use App\Http\Controllers\Controller;
|
|
use App\Http\Requests\Package\DatastoreRequest;
|
|
use App\Jobs\VirtualMachine\Sync\SyncDatastore;
|
|
use App\Models\ModuleDatastore;
|
|
use App\Repositories\Module\Datastore\DatastoreRepositoryInterface;
|
|
use App\Repositories\Module\Host\HostRepositoryInterface;
|
|
use Exception;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Support\Facades\DB;
|
|
use Yajra\DataTables\Facades\DataTables;
|
|
|
|
class Datastore extends Controller
|
|
{
|
|
public function __construct(
|
|
public DatastoreRepositoryInterface $datastoreRepository,
|
|
public HostRepositoryInterface $hostRepository
|
|
) {
|
|
}
|
|
//
|
|
public function index(Request $request)
|
|
{
|
|
$datastore = $this->datastoreRepository->filter(
|
|
[
|
|
'page' => $request->get('page') ?? 1
|
|
],
|
|
[
|
|
'host.datacenter.vCenter'
|
|
]
|
|
);
|
|
$title = 'List all datastore';
|
|
return view('administrator.pages.package.datastore.index', compact('title', 'datastore'));
|
|
}
|
|
public function sync(SyncDatastore $syncDatastore)
|
|
{
|
|
|
|
dispatch($syncDatastore)->delay(now()->addSeconds(5));
|
|
return back()->with('success', 'The process is being established, please wait');
|
|
}
|
|
public function create()
|
|
{
|
|
$title = 'Add Datastore';
|
|
return view('administrator.pages.package.datastore.add', compact('title'));
|
|
}
|
|
public function store(DatastoreRequest $request)
|
|
{
|
|
$request->validated();
|
|
|
|
$host = $this->hostRepository->find($request->get('module_host_id'));
|
|
if (!$host) {
|
|
return back()->withErrors('Invalid Request');
|
|
}
|
|
$isCreated = $this->datastoreRepository->findBy('datastore', $request->get('datastore'));
|
|
|
|
if ($isCreated) {
|
|
return back()->withErrors('The Datastore ID already exists');
|
|
}
|
|
DB::beginTransaction();
|
|
try {
|
|
$this->datastoreRepository->create($request->all());
|
|
DB::commit();
|
|
} catch (Exception $error) {
|
|
DB::rollBack();
|
|
return back()->withErrors($error->getMessage());
|
|
}
|
|
return redirect()->route('package.datastore.index')->with('success', 'Add new datastore successfully');
|
|
}
|
|
public function edit(int $id = 0)
|
|
{
|
|
$datastore = $this->datastoreRepository->findWithRelationships($id, ['host.datacenter']);
|
|
|
|
if (!$datastore) {
|
|
return back()->withErrors('Invalid Request');
|
|
}
|
|
$title = 'Edit Datastore';
|
|
|
|
return view('administrator.pages.package.datastore.edit', compact('title', 'datastore'));
|
|
}
|
|
public function update(int $id, DatastoreRequest $request)
|
|
{
|
|
$datastore = $this->datastoreRepository->find($id);
|
|
if (!$datastore) {
|
|
return redirect()->route('package.datastore.index')->withErrors('Invalid request.');
|
|
}
|
|
$request->validated();
|
|
|
|
$host = $this->hostRepository->find($request->get('module_host_id'));
|
|
if (!$host) {
|
|
return back()->withErrors('Invalid Request');
|
|
}
|
|
if ($request->get('datastore') !== $datastore->datastore) {
|
|
$isCreated = $this->datastoreRepository->findBy('datastore', $request->get('datastore'));
|
|
|
|
if ($isCreated) {
|
|
return back()->withErrors('The Datastore ID already exists');
|
|
}
|
|
}
|
|
DB::beginTransaction();
|
|
try {
|
|
$this->datastoreRepository->update($id, $request->all());
|
|
DB::commit();
|
|
} catch (Exception $error) {
|
|
DB::rollBack();
|
|
return back()->withErrors($error->getMessage());
|
|
}
|
|
return redirect()->route('package.datastore.index')->with('success', 'Updated successfully');
|
|
}
|
|
public function destroy(int $id = 0)
|
|
{
|
|
$datastore = $this->datastoreRepository->find($id);
|
|
|
|
if (!$datastore) {
|
|
return back()->withErrors('Invalid request.');
|
|
}
|
|
|
|
DB::beginTransaction();
|
|
try {
|
|
$this->datastoreRepository->delete($id);
|
|
DB::commit();
|
|
} catch (Exception $error) {
|
|
DB::rollBack();
|
|
return back()->withErrors($error->getMessage());
|
|
}
|
|
return redirect()->route('package.datastore.index')->with('success', 'Deleted datacenter successfully!');
|
|
}
|
|
|
|
public function datatable(Request $request)
|
|
{
|
|
$query = $this->datastoreRepository->filter(
|
|
$request->all(),
|
|
['host.datacenter.vCenter'],
|
|
[],
|
|
['*'],
|
|
true
|
|
);
|
|
|
|
return DataTables::eloquent($query)
|
|
->addColumn(
|
|
'action',
|
|
fn($d) =>
|
|
'<a href="' . route('package.datastore.edit', $d->id) . '" class="btn btn-primary btn-sm">Edit</a>'
|
|
)
|
|
->filterColumn('host.datacenter.v_center.host', function ($query, $keyword) {
|
|
$query->whereHas('host.datacenter.vCenter', function ($q) use ($keyword) {
|
|
$q->where('host', 'like', "%{$keyword}%");
|
|
});
|
|
})
|
|
->make(true);
|
|
}
|
|
}
|