Files
portal-api/app/Http/Controllers/admin/package/IPMapping.php
T
2026-02-03 11:07:47 +07:00

182 lines
5.8 KiB
PHP

<?php
namespace App\Http\Controllers\admin\package;
use App\Http\Controllers\Controller;
use App\Http\Requests\Package\IPRequest;
use App\Repositories\Module\IpAddress\IpAddressRepositoryInterface;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Yajra\DataTables\Facades\DataTables;
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('administrator.pages.package.ipaddress.index', compact('listIP', 'title'));
}
public function create()
{
$title = 'Add new IP Address';
return view('administrator.pages.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('administrator.pages.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');
}
public function datatable(Request $request)
{
$query = $this->ipAddressRepository->filter(
$request->all(),
['host.datacenter.vCenter'],
[],
['*'],
true
);
return DataTables::eloquent($query)
->addColumn(
'action',
fn($d) =>
'<a href="' . route('package.ipaddress.edit', $d->id) . '" class="btn btn-primary btn-sm">Edit</a>'
)
->filterColumn('host.datacenter.v_center.host', function ($query, $keyword) {
$query->whereHas('host.datacenter.vCenter', function ($q) use ($keyword) {
$q->where('host', 'like', "%{$keyword}%");
});
})
->make(true);
}
}