diff --git a/app/Enums/Status.php b/app/Enums/Status.php
index 723e527..7a8379a 100644
--- a/app/Enums/Status.php
+++ b/app/Enums/Status.php
@@ -18,8 +18,8 @@ enum Status: int
public function badge(): string
{
return match ($this) {
- self::DEACTIVE => 'Không hoạt động',
- self::ACTIVE => 'Hoạt động',
+ self::DEACTIVE => 'Không hoạt động',
+ self::ACTIVE => 'Hoạt động',
};
}
diff --git a/app/Http/Controllers/admin/ModulevCenterController.php b/app/Http/Controllers/admin/ModulevCenterController.php
index 8af1f4f..360bfad 100644
--- a/app/Http/Controllers/admin/ModulevCenterController.php
+++ b/app/Http/Controllers/admin/ModulevCenterController.php
@@ -18,12 +18,14 @@ use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Storage;
use Illuminate\Support\Facades\Validator;
use Maatwebsite\Excel\Facades\Excel;
+use Yajra\DataTables\Facades\DataTables;
class ModulevCenterController extends Controller
{
public function __construct(
public vCenterRepositoryInterface $vCenterRepository
- ) {}
+ ) {
+ }
// show all hosting module
public function index(Request $request)
{
@@ -37,7 +39,7 @@ class ModulevCenterController extends Controller
}
public function import(Request $request)
{
- if (! $request->file) {
+ if (!$request->file) {
return back()->withErrors("Please choose your file");
}
$data = Excel::toArray([], $request->file);
@@ -55,7 +57,7 @@ class ModulevCenterController extends Controller
}
}
- if (! isset($allData) || (isset($allData) && count($allData) < 1)) {
+ if (!isset($allData) || (isset($allData) && count($allData) < 1)) {
return back()->withErrors('No data imported');
}
@@ -86,7 +88,7 @@ class ModulevCenterController extends Controller
DB::beginTransaction();
try {
$data = $request->all();
- $data['password'] = SignRSA::EncryptFunc($request->password);
+ $data['password'] = SignRSA::EncryptFunc($request->password);
$this->vCenterRepository->create($data);
DB::commit();
} catch (\Exception $error) {
@@ -98,14 +100,16 @@ class ModulevCenterController extends Controller
public function edit(int $id = 0)
{
$server = $this->vCenterRepository->find($id);
- if (! $server) return back()->withErrors('Can\'t find server by id ' . $id);
+ if (!$server)
+ return back()->withErrors('Can\'t find server by id ' . $id);
$title = 'View vCenter information';
return view('administrator.pages.module.vcenter.edit', compact('title', 'server'));
}
public function update(UpdatevCenterRequest $request, int $id = 0)
{
$server = $this->vCenterRepository->find($id);
- if (! $server) return back()->withErrors('Invalid Request');
+ if (!$server)
+ return back()->withErrors('Invalid Request');
$request->validated();
if ($request->get('host') != $server->name) {
$isExitsHost = $this->vCenterRepository->findByName($request->get('host'));
@@ -116,8 +120,8 @@ class ModulevCenterController extends Controller
DB::beginTransaction();
try {
$data = $request->all();
- if (! empty($request->get('password'))) {
- $data['password'] = SignRSA::EncryptFunc($request->password);
+ if (!empty($request->get('password'))) {
+ $data['password'] = SignRSA::EncryptFunc($request->password);
}
$this->vCenterRepository->update($id, $data);
DB::commit();
@@ -127,11 +131,11 @@ class ModulevCenterController extends Controller
}
return redirect()->route('module.vcenter.index')->with('success', 'Updated Successfully');
}
-
+
public function destroy(int $id = 0)
{
$server = $this->vCenterRepository->find($id);
- if (! $server) {
+ if (!$server) {
return back()->withErrors('Invalid Request');
}
DB::beginTransaction();
@@ -145,4 +149,36 @@ class ModulevCenterController extends Controller
}
return redirect()->route('module.vcenter.index')->with('success', 'Deleted successfully.');
}
+
+ public function datatable(Request $request)
+ {
+ $query = $this->vCenterRepository->filter(
+ $request->all(),
+ [],
+ [],
+ ['*'],
+ true
+ );
+
+ return DataTables::eloquent($query)
+ ->addColumn(
+ 'action',
+ function ($d) {
+ return view('administrator.pages.module.vcenter.datatable.action', ['row' => $d])->render();
+ }
+ )
+ ->addColumn(
+ 'status',
+ function ($d) {
+ return view('administrator.pages.module.vcenter.datatable.status', ['row' => $d])->render();
+ }
+ )
+ ->filterColumn('v_center.host', function ($query, $keyword) {
+ $query->whereHas('vCenter', function ($q) use ($keyword) {
+ $q->where('host', 'like', "%{$keyword}%");
+ });
+ })
+ ->rawColumns(['action', 'status'])
+ ->make(true);
+ }
}
diff --git a/app/Repositories/vCenter/vCenterCachedRepository.php b/app/Repositories/vCenter/vCenterCachedRepository.php
index 434721e..4eee725 100644
--- a/app/Repositories/vCenter/vCenterCachedRepository.php
+++ b/app/Repositories/vCenter/vCenterCachedRepository.php
@@ -16,19 +16,32 @@ class vCenterCachedRepository extends BaseCachedRepository implements vCenterRep
{
return $this->vCenterRepository->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->vCenterRepository
+ ->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->vCenterRepository->filter($data, $relationships, $withCount, $select);
- });
+
+ return Cache::remember(
+ $cacheKey,
+ $this->cacheTime,
+ fn() =>
+ $this->vCenterRepository
+ ->filter($data, $relationships, $withCount, $select, $returnQuery)
+ );
}
public function findByName(string $name = '')
{
@@ -37,7 +50,7 @@ class vCenterCachedRepository extends BaseCachedRepository implements vCenterRep
'name' => $name
]
])));
- return Cache::remember($cacheKey, $this->cacheTime, function () use ($name){
+ return Cache::remember($cacheKey, $this->cacheTime, function () use ($name) {
return $this->vCenterRepository->findByName($name);
});
}
diff --git a/app/Repositories/vCenter/vCenterRepository.php b/app/Repositories/vCenter/vCenterRepository.php
index c5e1f02..1ae4939 100644
--- a/app/Repositories/vCenter/vCenterRepository.php
+++ b/app/Repositories/vCenter/vCenterRepository.php
@@ -11,26 +11,42 @@ class vCenterRepository extends BaseRepository implements vCenterRepositoryInter
{
return vCenter::class;
}
- public function filter(array $data = [], array $relationships = [], array $withCount = [], array $select = ['*'])
- {
- $model = $this->model->with($relationships)->select($select);
+ public function filter(
+ array $data = [],
+ array $relationships = [],
+ array $withCount = [],
+ array $select = ['*'],
+ bool $returnQuery = false
+ ) {
+ $query = $this->model
+ ->select($select)
+ ->with($relationships);
- if (isset($data['status'])) {
- $model = $model->where("status", $data['status']);
+ // if (!empty($data['vcenter_id'])) {
+ // $query->where('vcenter_id', $data['vcenter_id']);
+ // }
+
+ if (!empty($withCount)) {
+ $query->withCount($withCount);
}
- if (count($withCount) > 0) {
- $model = $model->withCount($withCount);
+
+ if ($returnQuery) {
+ return $query;
}
- if(isset($data['page'])) {
- return $model->paginate(15);
+
+ if (!empty($data['paginate'])) {
+ return $query->paginate(
+ $data['per_page'] ?? 15
+ );
}
- return $model->get();
+
+ return $query->get();
}
public function findByName(string $name = '')
{
if (empty($name)) {
return false;
}
- return $this->model->where('host','LIKE',"%$name%")->first();
+ return $this->model->where('host', 'LIKE', "%$name%")->first();
}
}
diff --git a/app/Repositories/vCenter/vCenterRepositoryInterface.php b/app/Repositories/vCenter/vCenterRepositoryInterface.php
index a4750fa..180360a 100644
--- a/app/Repositories/vCenter/vCenterRepositoryInterface.php
+++ b/app/Repositories/vCenter/vCenterRepositoryInterface.php
@@ -3,7 +3,14 @@ namespace App\Repositories\vCenter;
use App\Repositories\BaseRepositoryInterface;
-interface vCenterRepositoryInterface extends BaseRepositoryInterface {
- public function filter(array $data = [],array $relationships = [],array $withCount = [],array $select = ['*']);
+interface vCenterRepositoryInterface extends BaseRepositoryInterface
+{
+ public function filter(
+ array $data = [],
+ array $relationships = [],
+ array $withCount = [],
+ array $select = ['*'],
+ bool $returnQuery = false
+ );
public function findByName(string $name = '');
}
diff --git a/resources/views/administrator/pages/module/vcenter/datatable/action.blade.php b/resources/views/administrator/pages/module/vcenter/datatable/action.blade.php
new file mode 100644
index 0000000..ea3085e
--- /dev/null
+++ b/resources/views/administrator/pages/module/vcenter/datatable/action.blade.php
@@ -0,0 +1,2 @@
+
| - - | -IP Host | -Username | -Status | -Last Updated | -- | - |
|---|---|---|---|---|---|---|
| - - | -- {{ $item->host }} - | -- {{ $item->user }} - | -- {!! $item->getStatus() !!} - | -- @time($item->updated_at) - | -- - - - | -- @if(auth()->guard('admins')->user()->can("delete module vcenter")) - - @endif - | -