This commit is contained in:
pbao9
2026-01-30 17:08:59 +07:00
parent 69923ea016
commit 72b690f22e
15 changed files with 370 additions and 211 deletions
+58 -5
View File
@@ -14,21 +14,74 @@ use Exception;
use Illuminate\Http\Request; use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB; use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Validator; use Illuminate\Support\Facades\Validator;
use Yajra\DataTables\Facades\DataTables;
class Host extends Controller class Host extends Controller
{ {
public function __construct( public function __construct(
public HostRepositoryInterface $hostRepository, public HostRepositoryInterface $hostRepository,
public DatacenterRepositoryInterface $datacenterRepository public DatacenterRepositoryInterface $datacenterRepository
) {} ) {
}
public function index(Request $request) public function index(Request $request)
{ {
$hosts = $this->hostRepository->filter([
'page' => $request->get('page') ?? 1
], ['datacenter.vCenter']);
$title = 'List all Host'; $title = 'List all Host';
return view("admin.package.host.index", compact('hosts', 'title')); return view("admin.package.host.index", compact('title'));
}
public function datatable(Request $request)
{
$query = $this->hostRepository->filter(
$request->all(),
['datacenter.vCenter'],
[],
['*'],
true
);
return DataTables::eloquent($query)
->editColumn('updated_at', function ($d) {
return $d->updated_at ? $d->updated_at->format('d/m/Y') : '';
})
->addColumn('action', function ($d) {
$editUrl = route('package.host.edit', ['id' => $d->id]);
$deleteBtn = '';
if (auth()->guard('admins')->user()->can("delete module vcenter")) {
$deleteUrl = route('package.host.destroy', ['id' => $d->id]);
$csrf = csrf_field();
$method = method_field('delete');
$deleteBtn = <<<HTML
<form method="post" action="$deleteUrl" class="d-inline">
$method
$csrf
<button type="submit" class="btn btn-danger btn-sm" data-toggle="tooltip" title="Delete">
<i data-lucide="trash-2" class="icon-sm"></i>
</button>
</form>
HTML;
}
return <<<HTML
<a href="$editUrl" class="btn btn-warning btn-sm me-1" data-toggle="tooltip" title="Edit">
<i data-lucide="edit" class="icon-sm"></i>
</a>
$deleteBtn
HTML;
})
->filterColumn('datacenter.name', function ($query, $keyword) {
$query->whereHas('datacenter', function ($q) use ($keyword) {
$q->where('name', 'like', "%{$keyword}%");
});
})
->filterColumn('datacenter.v_center.host', function ($query, $keyword) {
$query->whereHas('datacenter.vCenter', function ($q) use ($keyword) {
$q->where('host', 'like', "%{$keyword}%");
});
})
->rawColumns(['action'])
->make(true);
} }
public function sync(SyncHost $syncHost) public function sync(SyncHost $syncHost)
{ {
@@ -16,18 +16,23 @@ class HostCachedRepository extends BaseCachedRepository implements HostRepositor
{ {
return $this->hostRepository->getModel(); return $this->hostRepository->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 || isset($data['page']) || isset($data['paginate'])) {
return $this->hostRepository->filter($data, $relationships, $withCount, $select, $returnQuery);
}
$cacheKey = $this->getCacheKey(md5(json_encode([ $cacheKey = $this->getCacheKey(md5(json_encode([
'filterItem' => [ 'filter' => compact('data', 'relationships', 'withCount', 'select')
'data' => $data,
'relationships' => $relationships,
'select' => $select,
'withCount' => $withCount
]
]))); ])));
return Cache::remember($cacheKey, $this->cacheTime, function () use ($data, $relationships, $select, $withCount) {
return $this->hostRepository->filter($data, $relationships, $withCount, $select); return Cache::remember($cacheKey, $this->cacheTime, function () use ($data, $relationships, $select, $withCount, $returnQuery) {
return $this->hostRepository->filter($data, $relationships, $withCount, $select, $returnQuery);
}); });
} }
public function findWithRelationships(int $id = 0, array $relationships = [],array $withCount = [], array $withSum = []) public function findWithRelationships(int $id = 0, array $relationships = [],array $withCount = [], array $withSum = [])
@@ -12,23 +12,32 @@ class HostRepository extends BaseRepository implements HostRepositoryInterface
return ModuleHost::class; return ModuleHost::class;
} }
public function filter(array $data = [], array $relationships = [], array $withCount = [], array $select = ['*']) public function filter(
{ array $data = [],
$model = $this->model->with($relationships)->select($select); array $relationships = [],
array $withCount = [],
array $select = ['*'],
bool $returnQuery = false
) {
$query = $this->model->with($relationships)->select($select);
if (isset($data['module_datacenter_id']) && $data['module_datacenter_id']) { if (isset($data['module_datacenter_id']) && $data['module_datacenter_id']) {
$model = $model->where("module_datacenter_id", $data['module_datacenter_id']); $query = $query->where("module_datacenter_id", $data['module_datacenter_id']);
} }
if (count($withCount) > 0) { if (count($withCount) > 0) {
$model = $model->withCount($withCount); $query = $query->withCount($withCount);
} }
if (isset($data['page'])) { if ($returnQuery) {
return $model->paginate(15); return $query;
} }
return $model->get(); if (isset($data['page']) || isset($data['paginate'])) {
return $query->paginate($data['per_page'] ?? 15);
}
return $query->get();
} }
public function findWithRelationships(int $id = 0, array $relationships = [], array $withCount = [], array $withSum = []) public function findWithRelationships(int $id = 0, array $relationships = [], array $withCount = [], array $withSum = [])
@@ -4,7 +4,13 @@ namespace App\Repositories\Module\Host;
use App\Repositories\BaseRepositoryInterface; use App\Repositories\BaseRepositoryInterface;
interface HostRepositoryInterface extends BaseRepositoryInterface{ interface HostRepositoryInterface extends BaseRepositoryInterface{
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
);
public function findWithRelationships(int $id = 0, array $relationships = [],array $withCount = [], array $withSum = []); public function findWithRelationships(int $id = 0, array $relationships = [],array $withCount = [], array $withSum = []);
public function findBy(string $key = '',string|int|null $value = '' ); public function findBy(string $key = '',string|int|null $value = '' );
@@ -16,7 +16,7 @@ return new class extends Migration {
$table->id()->autoIncrement(); $table->id()->autoIncrement();
$table->string('name', 255)->nullable(); $table->string('name', 255)->nullable();
$table->string('datacenter_id', 255)->nullable(); $table->string('datacenter_id', 255)->nullable();
$table->bigInteger('vcenter_id')->default(0)->index(); $table->foreignId('vcenter_id')->constrained('module_vcenter')->cascadeOnDelete()->nullable()->index();
$table->integer('status')->default(0); $table->integer('status')->default(0);
$table->timestamps(); $table->timestamps();
}); });
@@ -18,7 +18,7 @@ return new class extends Migration
$table->string('name',100)->nullable(); $table->string('name',100)->nullable();
$table->string('host_id',255)->nullable(); $table->string('host_id',255)->nullable();
$table->string('template',255)->nullable(); $table->string('template',255)->nullable();
$table->bigInteger('module_datacenter_id')->default(0)->index(); $table->foreignId('module_datacenter_id')->constrained('module_datacenter')->cascadeOnDelete()->nullable()->index();
$table->string('console',255)->nullable(); $table->string('console',255)->nullable();
$table->string('iso_path',255)->nullable(); $table->string('iso_path',255)->nullable();
$table->timestamps(); $table->timestamps();
@@ -11,10 +11,10 @@ return new class extends Migration
*/ */
public function up(): void public function up(): void
{ {
Schema::create('product_vm_templates', function (Blueprint $table) { // Schema::create('product_vm_templates', function (Blueprint $table) {
$table->foreignId('product_id')->constrained('products')->cascadeOnDelete(); // $table->foreignId('product_id')->constrained('products')->cascadeOnDelete();
$table->foreignId('vm_template_id')->constrained('vm_templates')->cascadeOnDelete(); // $table->foreignId('vm_template_id')->constrained('vm_templates')->cascadeOnDelete();
}); // });
} }
/** /**
@@ -0,0 +1,37 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
/**
* Run the migrations.
*/
public function up(): void
{
Schema::table('module_host', function (Blueprint $table) {
$table->unsignedBigInteger('module_datacenter_id')->nullable()->change();
$table->foreign('module_datacenter_id')
->references('id')
->on('module_datacenter')
->cascadeOnDelete();
});
}
/**
* Reverse the migrations.
*/
public function down(): void
{
Schema::table('module_host', function (Blueprint $table) {
// Drop the foreign key first
$table->dropForeign(['module_datacenter_id']);
// Revert column type (assuming it was bigInteger with default 0 based on previous context)
$table->bigInteger('module_datacenter_id')->default(0)->change();
});
}
};
@@ -0,0 +1,33 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration {
/**
* Run the migrations.
*/
public function up(): void
{
Schema::table('module_datacenter', function (Blueprint $table) {
//
$table->unsignedBigInteger('vcenter_id')->nullable()->change();
$table->foreign('vcenter_id')
->references('id')
->on('module_vcenter')
->cascadeOnDelete();
});
}
/**
* Reverse the migrations.
*/
public function down(): void
{
Schema::table('module_datacenter', function (Blueprint $table) {
//
});
}
};
@@ -1,4 +1,18 @@
@extends("admin.main") @extends('administrator.layout.master')
@section('breadcrumb')
@php
$breadcrumbItems = [
['label' => 'Modules'],
['label' => 'Package Host'],
['label' => 'Host', 'active' => true],
];
@endphp
<x-breabcrumb :items="$breadcrumbItems" />
@endsection
@push('plugin-styles')
<link href="{{ asset('build/plugins/select2/select2.min.css') }}" rel="stylesheet" />
@endpush
@section("content") @section("content")
@if(auth()->guard('admins')->user()->can("add module vcenter")) @if(auth()->guard('admins')->user()->can("add module vcenter"))
<div class="card card-outline card-primary"> <div class="card card-outline card-primary">
@@ -32,7 +46,8 @@
<div class="row form-group align-items-center mb-0"> <div class="row form-group align-items-center mb-0">
<label for="console" class="col-md-3 text-right">Console Name</label> <label for="console" class="col-md-3 text-right">Console Name</label>
<div class="col-md-9 p-1 bg-customer"> <div class="col-md-9 p-1 bg-customer">
<input type="text" id="console" name="console" placeholder="consolenode129.tgs.com.vn" value="" class="form-control"> <input type="text" id="console" name="console" placeholder="consolenode129.tgs.com.vn" value=""
class="form-control">
</div> </div>
</div> </div>
</div> </div>
@@ -49,3 +64,13 @@
</div> </div>
@endif @endif
@endsection @endsection
@push('plugin-scripts')
<script src="{{ asset('build/plugins/jquery/jquery.min.js') }}"></script>
<script src="{{ asset('build/plugins/select2/select2.min.js') }}"></script>
<script src="{{ asset('build/plugins/datatables.net/dataTables.min.js') }}"></script>
<script src="{{ asset('build/plugins/datatables.net-bs5/dataTables.bootstrap5.min.js') }}"></script>
@endpush
@push('plugin-styles')
<link href="{{ asset('build/plugins/select2/select2.min.css') }}" rel="stylesheet" />
@endpush
@@ -1,4 +1,18 @@
@extends("admin.main") @extends('administrator.layout.master')
@section('breadcrumb')
@php
$breadcrumbItems = [
['label' => 'Modules'],
['label' => 'Package Host'],
['label' => 'Host', 'active' => true],
];
@endphp
<x-breabcrumb :items="$breadcrumbItems" />
@endsection
@push('plugin-styles')
<link href="{{ asset('build/plugins/select2/select2.min.css') }}" rel="stylesheet" />
@endpush
@section("content") @section("content")
@if(auth()->guard('admins')->user()->can("add module vcenter")) @if(auth()->guard('admins')->user()->can("add module vcenter"))
<div class="card card-outline card-primary"> <div class="card card-outline card-primary">
@@ -13,28 +27,32 @@
<div class="row form-group align-items-center mb-0"> <div class="row form-group align-items-center mb-0">
<label for="name" class="col-md-3 text-right">Name</label> <label for="name" class="col-md-3 text-right">Name</label>
<div class="col-md-9 p-1 bg-customer"> <div class="col-md-9 p-1 bg-customer">
<input type="text" id="name" name="name" placeholder="" value="{{ $host->name}}" class="form-control"> <input type="text" id="name" name="name" placeholder="" value="{{ $host->name}}"
class="form-control">
</div> </div>
</div> </div>
{{--Host ID--}} {{--Host ID--}}
<div class="row form-group align-items-center mb-0"> <div class="row form-group align-items-center mb-0">
<label for="host_id" class="col-md-3 text-right">Host ID</label> <label for="host_id" class="col-md-3 text-right">Host ID</label>
<div class="col-md-9 p-1 bg-customer"> <div class="col-md-9 p-1 bg-customer">
<input type="text" id="host_id" name="host_id" placeholder="" value="{{ $host->host_id}}" class="form-control"> <input type="text" id="host_id" name="host_id" placeholder="" value="{{ $host->host_id}}"
class="form-control">
</div> </div>
</div> </div>
{{--Template ID--}} {{--Template ID--}}
<div class="row form-group align-items-center mb-0"> <div class="row form-group align-items-center mb-0">
<label for="template" class="col-md-3 text-right">Template ID</label> <label for="template" class="col-md-3 text-right">Template ID</label>
<div class="col-md-9 p-1 bg-customer"> <div class="col-md-9 p-1 bg-customer">
<input type="text" id="template" name="template" placeholder="" value="{{ $host->template}}" class="form-control"> <input type="text" id="template" name="template" placeholder="" value="{{ $host->template}}"
class="form-control">
</div> </div>
</div> </div>
{{--Console ID--}} {{--Console ID--}}
<div class="row form-group align-items-center mb-0"> <div class="row form-group align-items-center mb-0">
<label for="console" class="col-md-3 text-right">Console Name</label> <label for="console" class="col-md-3 text-right">Console Name</label>
<div class="col-md-9 p-1 bg-customer"> <div class="col-md-9 p-1 bg-customer">
<input type="text" id="console" name="console" placeholder="consolenode129.tgs.com.vn" value="{{ $host->console}}" class="form-control"> <input type="text" id="console" name="console" placeholder="consolenode129.tgs.com.vn"
value="{{ $host->console}}" class="form-control">
</div> </div>
</div> </div>
</div> </div>
@@ -51,3 +69,14 @@
</div> </div>
@endif @endif
@endsection @endsection
@push('plugin-scripts')
<script src="{{ asset('build/plugins/jquery/jquery.min.js') }}"></script>
<script src="{{ asset('build/plugins/select2/select2.min.js') }}"></script>
<script src="{{ asset('build/plugins/datatables.net/dataTables.min.js') }}"></script>
<script src="{{ asset('build/plugins/datatables.net-bs5/dataTables.bootstrap5.min.js') }}"></script>
@endpush
@push('plugin-styles')
<link href="{{ asset('build/plugins/select2/select2.min.css') }}" rel="stylesheet" />
@endpush
@@ -1,79 +1,30 @@
@php use App\Http\Helpers\Helper; @endphp @extends('administrator.layout.master')
@extends('admin.main')
@section('breadcrumb')
@php
$breadcrumbItems = [
['label' => 'Modules'],
['label' => 'Package Host'],
['label' => 'Host', 'active' => true],
];
@endphp
<x-breabcrumb :items="$breadcrumbItems" />
@endsection
@section('content') @section('content')
@if(auth()->guard('admins')->user()->can("manager package module")) @if(auth()->guard('admins')->user()->can("manager package module"))
<div class="card card-primary card-outline"> <div class="card card-primary card-outline">
<div class="card-header"> <div class="card-header">
@include("admin.block.Sync.Host") @include("admin.block.Sync.Host")
</div> </div>
<div class="card-body"> <div class="card-body p-5">
<table class="table table-invoice table-bordered"> <x-datatable id="hostTable" ajax="{{ route('package.host.datatable') }}" :columns="[
<thead> ['key' => 'name', 'label' => 'Name'],
<tr> ['key' => 'datacenter.name', 'label' => 'Datacenter Name'],
<th class="text-center"> ['key' => 'datacenter.v_center.host', 'label' => 'vCenter Host'],
<label> ['key' => 'updated_at', 'label' => 'Last Updated'],
<input type="checkbox" name="check" class="check-all"> ['key' => 'action', 'label' => 'Action']
</label> ]" />
</th>
<th class="text-center">Host Name</th>
<th class="text-center">Datacenter Name</th>
<th class="text-center">vCenter Host</th>
<th class="text-center">Last Updated</th>
<th style="width: 40px"></th>
<th style="width: 40px"></th>
</tr>
</thead>
<tbody>
@if(count($hosts) > 0)
@foreach($hosts->all() as $item)
<tr>
<td class="text-center">
<label for=""></label><input type="checkbox" name="id[]" value="{{ $item->id }}"
id="">
</td>
<td class="text-center">
{{ $item->name }}
</td>
<td class="text-center">
@isset($item->datacenter->name)
{{$item->datacenter->name}}
@endisset
</td>
<td class="text-center">
@isset($item->datacenter->vCenter->host)
{{$item->datacenter->vCenter->host}}
@endisset
</td>
<td class="text-center">
@time($item->updated_at)
</td>
<td class="text-center">
<a class="btn btn-warning btn-sm"
href="{{ route('package.host.edit', ['id' => $item->id]) }}">
<i class="fas fa-search"></i>
</a>
</td>
<td>
@if(auth()->guard('admins')->user()->can("delete module vcenter"))
<form method="post" class="d-inline"
action="{{ route('package.host.destroy', ['id' => $item->id]) }}">
@method('delete')
@csrf
<button type="submit" data-toggle="tooltip" data-placement="top" title=""
class="btn btn-sm btn-danger" data-original-title="Xoá">
<i class="fas fa-trash"></i>
</button>
</form>
@endif
</td>
</tr>
@endforeach
@endif
</tbody>
</table>
</div>
<div class="card-footer">
{{ $hosts->withQueryString()->links() }}
</div> </div>
</div> </div>
@else @else
@@ -83,3 +34,14 @@
</div> </div>
@endif @endif
@endsection @endsection
@push('plugin-scripts')
<script src="{{ asset('build/plugins/jquery/jquery.min.js') }}"></script>
<script src="{{ asset('build/plugins/select2/select2.min.js') }}"></script>
<script src="{{ asset('build/plugins/datatables.net/dataTables.min.js') }}"></script>
<script src="{{ asset('build/plugins/datatables.net-bs5/dataTables.bootstrap5.min.js') }}"></script>
@endpush
@push('plugin-styles')
<link href="{{ asset('build/plugins/select2/select2.min.css') }}" rel="stylesheet" />
@endpush
@@ -74,6 +74,9 @@ $dtColumns = collect($columns)->map(function ($c) {
minimumResultsForSearch: Infinity, minimumResultsForSearch: Infinity,
width: '60px' width: '60px'
}); });
if (window.lucide) {
lucide.createIcons();
}
} }
}); });
+1
View File
@@ -248,6 +248,7 @@ Route::prefix('administrator')->group(function () {
Route::get('edit/{id}', 'edit')->name('edit'); Route::get('edit/{id}', 'edit')->name('edit');
Route::put('edit/{id}', 'update')->name('update'); Route::put('edit/{id}', 'update')->name('update');
Route::delete('remove/{id}', 'destroy')->name('destroy'); Route::delete('remove/{id}', 'destroy')->name('destroy');
Route::get('datatable', 'datatable')->name('datatable');
}); });
// Network // Network
Route::group([ Route::group([
-4
View File
@@ -19,7 +19,3 @@ use Yajra\DataTables\DataTables;
include __DIR__ . '/admin.php'; include __DIR__ . '/admin.php';
include __DIR__ . '/customer.php'; include __DIR__ . '/customer.php';
Route::get('/users/datatable-test', function () {
return DataTables::of(Customer::query())->make(true);
})->name('admin.users.datatable');