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

128 lines
4.3 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;
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('admin.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('admin.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('admin.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!');
}
}