update network moudle

This commit is contained in:
pbao9
2026-01-31 10:25:21 +07:00
parent 745e7f71ec
commit 4b5e22c9d2
11 changed files with 277 additions and 179 deletions
+37 -10
View File
@@ -11,6 +11,7 @@ use App\Repositories\Module\Network\NetworkRepositoryInterface;
use Exception;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Yajra\DataTables\Facades\DataTables;
class Network extends Controller
{
@@ -18,35 +19,37 @@ 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']
],
['host.datacenter.vCenter']
);
$title = 'List all network';
return view('admin.package.network.index',compact('networks','title'));
return view('administrator.pages.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');
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'));
return view('administrator.pages.package.network.add', compact('title'));
}
public function store(NetworkRequest $request)
{
$request->validated();
$host = $this->hostRepository->find($request->get('module_host_id'));
if (! $host) {
if (!$host) {
return back()->withErrors('Invalid Request');
}
$isCreated = $this->networkRepository->findBy('network', $request->get('network'));
@@ -66,19 +69,19 @@ class Network extends Controller
public function edit(int $id = 0)
{
$network = $this->networkRepository->findWithRelationships($id, ['host.datacenter']);
if (! $network) {
if (!$network) {
return back()->withErrors('Invalid Request');
}
$title = "View Network Information";
return view('admin.package.network.edit', compact('network', 'title'));
return view('administrator.pages.package.network.edit', compact('network', 'title'));
}
public function update(int $id = 0, NetworkRequest $request)
{
$request->validated();
$host = $this->networkRepository->find($id);
if (! $host) {
if (!$host) {
return redirect()->route('package.network.index')->withErrors('Invalid Request');
}
if ($request->get('network') !== $host->network) {
@@ -101,7 +104,7 @@ class Network extends Controller
{
$host = $this->networkRepository->find($id);
if (! $host) {
if (!$host) {
return back()->withErrors('Invalid request.');
}
@@ -115,4 +118,28 @@ class Network extends Controller
}
return redirect()->route('package.network.index')->with('success', 'Deleted successfully!');
}
public function datatable(Request $request)
{
$query = $this->networkRepository->filter(
$request->all(),
['host.datacenter.vCenter'],
[],
['*'],
true
);
return DataTables::eloquent($query)
->addColumn(
'action',
fn($d) =>
'<a href="' . route('package.network.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);
}
}
@@ -8,27 +8,39 @@ class NetworkCachedRepository extends BaseCachedRepository implements NetworkRep
{
public function __construct(
public NetworkRepository $networkRepository
)
{
) {
parent::__construct();
}
public function getModel()
{
return $this->networkRepository->getModel();
}
public function filter(array $data = [], array $relationships = [], array $withCount = [], array $select = ['*'])
{
public function filter(
array $data = [],
array $relationships = [],
array $withCount = [],
array $select = ['*'],
bool $returnQuery = false
) {
if (
$returnQuery ||
!empty($data['paginate'])
) {
return $this->networkRepository
->filter($data, $relationships, $withCount, $select, $returnQuery);
}
$cacheKey = $this->getCacheKey(md5(json_encode([
'filterItem' => [
'data' => $data,
'relationships' => $relationships,
'select' => $select,
'withCount' => $withCount
]
'filter' => compact('data', 'relationships', 'withCount', 'select')
])));
return Cache::remember($cacheKey, $this->cacheTime, function () use ($data, $relationships, $select, $withCount) {
return $this->networkRepository->filter($data, $relationships, $withCount, $select);
});
return Cache::remember(
$cacheKey,
$this->cacheTime,
fn() =>
$this->networkRepository
->filter($data, $relationships, $withCount, $select, $returnQuery)
);
}
public function findBy(string $key = '', string|int|null $value = '')
{
@@ -42,7 +54,7 @@ class NetworkCachedRepository extends BaseCachedRepository implements NetworkRep
return $this->networkRepository->findBy($key, $value);
});
}
public function findWithRelationships(int $id = 0, array $relationships = [],array $withCount = [], array $withSum = [])
public function findWithRelationships(int $id = 0, array $relationships = [], array $withCount = [], array $withSum = [])
{
$cacheKey = $this->getCacheKey(md5(json_encode([
'findWithRelationships' => [
@@ -50,8 +62,8 @@ class NetworkCachedRepository extends BaseCachedRepository implements NetworkRep
'relationships' => $relationships
]
])));
return Cache::remember($cacheKey, $this->cacheTime, function () use ($id, $relationships,$withCount,$withSum) {
return $this->networkRepository->findWithRelationships($id, $relationships,$withCount,$withSum);
return Cache::remember($cacheKey, $this->cacheTime, function () use ($id, $relationships, $withCount, $withSum) {
return $this->networkRepository->findWithRelationships($id, $relationships, $withCount, $withSum);
});
}
}
@@ -3,28 +3,36 @@ namespace App\Repositories\Module\Network;
use App\Models\ModuleNetwork;
use App\Repositories\BaseRepository;
class NetworkRepository extends BaseRepository implements NetworkRepositoryInterface{
class NetworkRepository extends BaseRepository implements NetworkRepositoryInterface
{
public function getModel()
{
return ModuleNetwork::class;
}
public function filter(array $data = [], array $relationships = [], array $withCount = [], array $select = ['*'])
public function filter(array $data = [], array $relationships = [], array $withCount = [], array $select = ['*'], bool $returnQuery = false)
{
$model = $this->model->with($relationships)->select($select);
$query = $this->model->with($relationships)->select($select);
if (isset($data['status'])) {
$model = $model->where("status", $data['status']);
$query = $query->where("status", $data['status']);
}
if (isset($data['module_host_id']) && $data['module_host_id']) {
$model = $model->where("module_host_id", $data['module_host_id']);
$query->where("module_host_id", $data['module_host_id']);
}
if (count($withCount) > 0) {
$model = $model->withCount($withCount);
$query->withCount($withCount);
}
if(isset($data['page'])) {
return $model->paginate(15);
if ($returnQuery) {
return $query;
}
return $model->get();
if (!empty($data['paginate'])) {
return $query->paginate(
$data['per_page'] ?? 15
);
}
return $query->get();
}
public function findBy(string $key = '', string|int|null $value = '')
{
@@ -2,9 +2,16 @@
namespace App\Repositories\Module\Network;
use App\Repositories\BaseRepositoryInterface;
interface NetworkRepositoryInterface extends BaseRepositoryInterface{
public function filter(array $data = [],array $relationships = [],array $withCount = [],array $select = ['*']);
public function findBy(string $key = '',string|int|null $value = '' );
public function findWithRelationships(int $id = 0, array $relationships = [],array $withCount = [], array $withSum = []);
interface NetworkRepositoryInterface extends BaseRepositoryInterface
{
public function filter(
array $data = [],
array $relationships = [],
array $withCount = [],
array $select = ['*'],
bool $returnQuery = false
);
public function findBy(string $key = '', string|int|null $value = '');
public function findWithRelationships(int $id = 0, array $relationships = [], array $withCount = [], array $withSum = []);
}