119 lines
4.0 KiB
PHP
119 lines
4.0 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers\admin\package;
|
|
|
|
use App\Http\Controllers\Controller;
|
|
use App\Http\Requests\Package\NetworkRequest;
|
|
use App\Jobs\VirtualMachine\Sync\SyncNetwork;
|
|
use App\Models\ModuleNetwork;
|
|
use App\Repositories\Module\Host\HostRepositoryInterface;
|
|
use App\Repositories\Module\Network\NetworkRepositoryInterface;
|
|
use Exception;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Support\Facades\DB;
|
|
|
|
class Network extends Controller
|
|
{
|
|
//
|
|
public function __construct(
|
|
public NetworkRepositoryInterface $networkRepository,
|
|
public HostRepositoryInterface $hostRepository
|
|
){}
|
|
public function index(Request $request)
|
|
{
|
|
$networks = $this->networkRepository->filter(
|
|
[
|
|
'page' => $request->get('page') ?? 1
|
|
],['host.datacenter.vCenter']
|
|
);
|
|
$title = 'List all network';
|
|
|
|
return view('admin.package.network.index',compact('networks','title'));
|
|
}
|
|
|
|
public function sync(SyncNetwork $syncNetwork)
|
|
{
|
|
dispatch($syncNetwork)->delay(now()->addSeconds(5));
|
|
return back()->with('success','The process is being established, please wait');
|
|
}
|
|
public function create()
|
|
{
|
|
$title = 'Add Network';
|
|
|
|
return view('admin.package.network.add', compact('title'));
|
|
}
|
|
public function store(NetworkRequest $request)
|
|
{
|
|
$request->validated();
|
|
$host = $this->hostRepository->find($request->get('module_host_id'));
|
|
if (! $host) {
|
|
return back()->withErrors('Invalid Request');
|
|
}
|
|
$isCreated = $this->networkRepository->findBy('network', $request->get('network'));
|
|
if ($isCreated) {
|
|
return back()->withErrors('The Network ID already exists');
|
|
}
|
|
DB::beginTransaction();
|
|
try {
|
|
$this->networkRepository->create($request->all());
|
|
DB::commit();
|
|
} catch (Exception $error) {
|
|
DB::rollBack();
|
|
return back()->withErrors($error->getMessage());
|
|
}
|
|
return redirect()->route('package.network.index')->with('success', 'Add new Network successfully');
|
|
}
|
|
public function edit(int $id = 0)
|
|
{
|
|
$network = $this->networkRepository->findWithRelationships($id, ['host.datacenter']);
|
|
if (! $network) {
|
|
return back()->withErrors('Invalid Request');
|
|
}
|
|
$title = "View Network Information";
|
|
|
|
return view('admin.package.network.edit', compact('network', 'title'));
|
|
}
|
|
public function update(int $id = 0, NetworkRequest $request)
|
|
{
|
|
|
|
$request->validated();
|
|
$host = $this->networkRepository->find($id);
|
|
if (! $host) {
|
|
return redirect()->route('package.network.index')->withErrors('Invalid Request');
|
|
}
|
|
if ($request->get('network') !== $host->network) {
|
|
$isCreated = $this->networkRepository->findBy('network', $request->get('network'));
|
|
if ($isCreated) {
|
|
return back()->withErrors('The Network ID already exists');
|
|
}
|
|
}
|
|
try {
|
|
DB::beginTransaction();
|
|
$this->networkRepository->update($id, $request->all());
|
|
DB::commit();
|
|
} catch (Exception $exception) {
|
|
DB::rollBack();
|
|
return back()->withErrors($exception->getMessage());
|
|
}
|
|
return redirect()->route('package.network.index')->with('success', 'Update successfully');
|
|
}
|
|
public function destroy(int $id = 0)
|
|
{
|
|
$host = $this->networkRepository->find($id);
|
|
|
|
if (! $host) {
|
|
return back()->withErrors('Invalid request.');
|
|
}
|
|
|
|
DB::beginTransaction();
|
|
try {
|
|
$this->networkRepository->delete($id);
|
|
DB::commit();
|
|
} catch (Exception $error) {
|
|
DB::rollBack();
|
|
return back()->withErrors($error->getMessage());
|
|
}
|
|
return redirect()->route('package.network.index')->with('success', 'Deleted successfully!');
|
|
}
|
|
}
|