Files

174 lines
6.2 KiB
PHP

<?php
namespace App\Http\Controllers\admin\package;
use App\Http\Controllers\Controller;
use App\Http\Requests\Package\HostRequest;
use App\Jobs\VirtualMachine\Sync\SyncHost;
use App\Models\ModuleDatacenter;
use App\Models\ModuleHost;
use App\Models\vCenter;
use App\Repositories\Module\Datacenter\DatacenterRepositoryInterface;
use App\Repositories\Module\Host\HostRepositoryInterface;
use Exception;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Validator;
use Yajra\DataTables\Facades\DataTables;
class Host extends Controller
{
public function __construct(
public HostRepositoryInterface $hostRepository,
public DatacenterRepositoryInterface $datacenterRepository
) {
}
public function index(Request $request)
{
$title = 'List all Host';
return view("administrator.pages.package.host.index", compact('title'));
}
public function datatable(Request $request)
{
$query = $this->hostRepository->filter(
$request->all(),
['datacenter.vCenter'],
[],
['*'],
true
);
return DataTables::eloquent($query)
->editColumn('updated_at', function ($d) {
return $d->updated_at ? $d->updated_at->format('d/m/Y') : '';
})
->editColumn('datacenter.name', function ($d) {
return $d->datacenter->name . ' - ' . $d->datacenter->datacenter_id;
})
->addColumn('action', function ($d) {
$editUrl = route('package.host.edit', ['id' => $d->id]);
$deleteBtn = '';
if (auth()->guard('admin')->user()->can("delete module vcenter")) {
$deleteUrl = route('package.host.destroy', ['id' => $d->id]);
$csrf = csrf_field();
$method = method_field('delete');
$deleteBtn = <<<HTML
<form method="post" action="$deleteUrl" class="d-inline">
$method
$csrf
<button type="submit" class="btn btn-danger btn-sm" data-toggle="tooltip" title="Delete">
<i data-lucide="trash-2" class="icon-sm"></i>
</button>
</form>
HTML;
}
return <<<HTML
<a href="$editUrl" class="btn btn-warning btn-sm me-1" data-toggle="tooltip" title="Edit">
<i data-lucide="edit" class="icon-sm"></i>
</a>
$deleteBtn
HTML;
})
->filterColumn('datacenter.name', function ($query, $keyword) {
$query->whereHas('datacenter', function ($q) use ($keyword) {
$q->where('name', 'like', "%{$keyword}%");
});
})
->filterColumn('datacenter.v_center.host', function ($query, $keyword) {
$query->whereHas('datacenter.vCenter', function ($q) use ($keyword) {
$q->where('host', 'like', "%{$keyword}%");
});
})
->rawColumns(['action'])
->make(true);
}
public function sync(SyncHost $syncHost)
{
dispatch($syncHost)->delay(now()->addSeconds(5));
return back()->with('success', 'The process is being established, please wait');
}
public function create()
{
$title = 'Add host';
return view('administrator.pages.package.host.add', compact('title'));
}
public function store(HostRequest $request)
{
$request->validated();
$datacenter = $this->datacenterRepository->find($request->get('module_datacenter_id'));
if (!$datacenter) {
return back()->withErrors('Invalid Request');
}
$isCreated = $this->hostRepository->findBy('host_id', $request->get('host_id'));
if ($isCreated) {
return back()->withErrors('The Host ID already exists');
}
DB::beginTransaction();
try {
$this->hostRepository->create($request->all());
DB::commit();
} catch (Exception $error) {
DB::rollBack();
return back()->withErrors($error->getMessage());
}
return redirect()->route('package.host.index')->with('success', 'Add new host successfully');
}
public function edit(int $id = 0)
{
$host = $this->hostRepository->findWithRelationships($id, ['datacenter']);
if (!$host) {
return back()->withErrors('Invalid Request');
}
$title = "View Host Information";
return view('administrator.pages.package.host.edit', compact('host', 'title'));
}
public function update(int $id = 0, HostRequest $request)
{
$request->validated();
$host = $this->hostRepository->find($id);
if (!$host) {
return redirect()->route('package.host.index')->withErrors('Invalid Request');
}
if ($request->get('host_id') !== $host->host_id) {
$isCreated = $this->hostRepository->findBy('host_id', $request->get('host_id'));
if ($isCreated) {
return back()->withErrors('The Host ID already exists');
}
}
try {
DB::beginTransaction();
$this->hostRepository->update($id, $request->all());
DB::commit();
} catch (Exception $exception) {
DB::rollBack();
return back()->withErrors($exception->getMessage());
}
return redirect()->route('package.host.index')->with('success', 'Update successfully');
}
public function destroy(int $id = 0)
{
$host = $this->hostRepository->find($id);
if (!$host) {
return back()->withErrors('Invalid request.');
}
DB::beginTransaction();
try {
$this->hostRepository->delete($id);
DB::commit();
} catch (Exception $error) {
DB::rollBack();
return back()->withErrors($error->getMessage());
}
return redirect()->route('package.host.index')->with('success', 'Deleted successfully!');
}
}