Files
portal-api/app/Http/Controllers/admin/package/Host.php
T
2025-07-24 16:18:27 +07:00

118 lines
3.9 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;
class Host extends Controller
{
public function __construct(
public HostRepositoryInterface $hostRepository,
public DatacenterRepositoryInterface $datacenterRepository
) {}
public function index(Request $request)
{
$hosts = $this->hostRepository->filter([
'page' => $request->get('page') ?? 1
], ['datacenter.vCenter']);
$title = 'List all Host';
return view("admin.package.host.index", compact('hosts', 'title'));
}
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('admin.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('admin.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!');
}
}