Update source into github
This commit is contained in:
@@ -0,0 +1,161 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers\admin\package;
|
||||
|
||||
use App\Http\Controllers\Controller;
|
||||
use App\Http\Requests\Package\IPRequest;
|
||||
use App\Models\ModuleDatacenter;
|
||||
use App\Models\ModuleHost;
|
||||
use App\Models\ModuleIpAddress;
|
||||
use App\Models\ModuleNetwork;
|
||||
use App\Models\vCenter;
|
||||
use App\Repositories\Module\IpAddress\IpAddressRepositoryInterface;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Illuminate\Support\Facades\Validator;
|
||||
|
||||
class IPMapping extends Controller
|
||||
{
|
||||
public function __construct(
|
||||
public IpAddressRepositoryInterface $ipAddressRepository
|
||||
) {}
|
||||
public function index(Request $request)
|
||||
{
|
||||
$listIP = $this->ipAddressRepository->filter(
|
||||
[
|
||||
'page' => $request->get('page') ?? 1
|
||||
],
|
||||
['host.datacenter']
|
||||
);
|
||||
$title = 'List All IP Address';
|
||||
|
||||
return view('admin.package.ipaddress.index', compact('listIP', 'title'));
|
||||
}
|
||||
|
||||
public function create()
|
||||
{
|
||||
$title = 'Add new IP Address';
|
||||
|
||||
return view('admin.package.ipaddress.add', compact('title'));
|
||||
}
|
||||
|
||||
public function store(IPRequest $request)
|
||||
{
|
||||
$request->validated();
|
||||
|
||||
$model_data = [];
|
||||
|
||||
if ((int) $request->option_selected === 1) {
|
||||
if (empty($request->get('list-ip'))) {
|
||||
return back()->withErrors('IP Address can\'t empty!');
|
||||
}
|
||||
$list = explode("\r\n", $request->get('list-ip'));
|
||||
if (count($list) > 0) {
|
||||
foreach ($list as $item) {
|
||||
$model_data[] = [
|
||||
'module_network_id' => $request->module_network_id,
|
||||
'module_host_id' => $request->module_host_id,
|
||||
'gateway' => $request->gateway,
|
||||
'dns' => $request->dns,
|
||||
'netmask' => $request->netmask,
|
||||
'ip' => $item
|
||||
];
|
||||
}
|
||||
}
|
||||
} else {
|
||||
if (empty($request->get('from'))) {
|
||||
return back()->withErrors('IP Address From can\'t empty!');
|
||||
}
|
||||
if (empty($request->get('to'))) {
|
||||
return back()->withErrors('IP Address To can\'t empty!');
|
||||
}
|
||||
$ipaddress = explode(".", $request->from);
|
||||
$from = $ipaddress[3];
|
||||
$to = explode(".", $request->to)[3];
|
||||
|
||||
for ($from; $from <= $to; $from++) {
|
||||
$ipaddress[3] = $from;
|
||||
$model_data[] = [
|
||||
'module_network_id' => $request->module_network_id,
|
||||
'module_host_id' => $request->module_host_id,
|
||||
'gateway' => $request->gateway,
|
||||
'dns' => $request->dns,
|
||||
'netmask' => $request->netmask,
|
||||
'ip' => implode('.', $ipaddress)
|
||||
];
|
||||
}
|
||||
}
|
||||
DB::beginTransaction();
|
||||
try {
|
||||
$this->ipAddressRepository->insert($model_data);
|
||||
|
||||
DB::commit();
|
||||
} catch (\Exception $error) {
|
||||
DB::rollBack();
|
||||
return back()->withErrors($error->getMessage());
|
||||
}
|
||||
return redirect()->route('package.ipaddress.index')->with('success', 'Add new ipaddress successfully');
|
||||
}
|
||||
|
||||
public function edit(int $id = 0)
|
||||
{
|
||||
$ipaddress = $this->ipAddressRepository->findWithRelationships($id, ['host.datacenter.vCenter']);
|
||||
|
||||
|
||||
if (! $ipaddress) {
|
||||
return back()->withErrors('Can\'t find IP Address by ID #' . $id);
|
||||
}
|
||||
|
||||
|
||||
$title = "View IP Address";
|
||||
|
||||
return view('admin.package.ipaddress.edit', compact('title', 'ipaddress'));
|
||||
}
|
||||
|
||||
public function update(int $id = 0, IPRequest $request)
|
||||
{
|
||||
$ipaddress = $this->ipAddressRepository->find($id);
|
||||
|
||||
if (! $ipaddress) {
|
||||
return back()->withErrors('Can\'t find IP Address by ID #' . $id);
|
||||
}
|
||||
|
||||
$request->validated();
|
||||
|
||||
if ($request->get('ip') !== $ipaddress->ip) {
|
||||
$ip = $this->ipAddressRepository->findBy('ip', $request->get('ip'));
|
||||
if ($ip) {
|
||||
return back()->withErrors('IP address already exists');
|
||||
}
|
||||
}
|
||||
|
||||
DB::beginTransaction();
|
||||
try {
|
||||
$this->ipAddressRepository->update($id, $request->all());
|
||||
DB::commit();
|
||||
} catch (\Exception $error) {
|
||||
DB::rollBack();
|
||||
return back()->withErrors($error->getMessage());
|
||||
}
|
||||
return redirect()->route('package.ipaddress.index')->with('success', 'Update new information for IP Address success');
|
||||
}
|
||||
|
||||
public function destroy(int $id = 0)
|
||||
{
|
||||
$ipaddress = $this->ipAddressRepository->find($id);
|
||||
|
||||
if (! $ipaddress) {
|
||||
return back()->withErrors('Can\'t find IP Address by ID #' . $id);
|
||||
}
|
||||
|
||||
DB::beginTransaction();
|
||||
try {
|
||||
$this->ipAddressRepository->delete($id);
|
||||
DB::commit();
|
||||
} catch (\Exception $error) {
|
||||
DB::rollBack();
|
||||
return back()->withErrors($error->getMessage());
|
||||
}
|
||||
return back()->with('success', 'Deleted Successfully');
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user