update
This commit is contained in:
@@ -14,21 +14,74 @@ use Exception;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Illuminate\Support\Facades\Validator;
|
||||
use Yajra\DataTables\Facades\DataTables;
|
||||
|
||||
class Host extends Controller
|
||||
{
|
||||
public function __construct(
|
||||
public HostRepositoryInterface $hostRepository,
|
||||
public DatacenterRepositoryInterface $datacenterRepository
|
||||
) {}
|
||||
) {
|
||||
}
|
||||
|
||||
public function index(Request $request)
|
||||
{
|
||||
$hosts = $this->hostRepository->filter([
|
||||
'page' => $request->get('page') ?? 1
|
||||
], ['datacenter.vCenter']);
|
||||
$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)
|
||||
{
|
||||
@@ -45,7 +98,7 @@ class Host extends Controller
|
||||
{
|
||||
$request->validated();
|
||||
$datacenter = $this->datacenterRepository->find($request->get('module_datacenter_id'));
|
||||
if (! $datacenter) {
|
||||
if (!$datacenter) {
|
||||
return back()->withErrors('Invalid Request');
|
||||
}
|
||||
$isCreated = $this->hostRepository->findBy('host_id', $request->get('host_id'));
|
||||
@@ -65,19 +118,19 @@ class Host extends Controller
|
||||
public function edit(int $id = 0)
|
||||
{
|
||||
$host = $this->hostRepository->findWithRelationships($id, ['datacenter']);
|
||||
if (! $host) {
|
||||
if (!$host) {
|
||||
return back()->withErrors('Invalid Request');
|
||||
}
|
||||
$title = "View Host Information";
|
||||
|
||||
return view('admin.package.host.edit', compact('host', 'title'));
|
||||
return view('admin.package.host.edit', compact('host', 'title'));
|
||||
}
|
||||
public function update(int $id = 0, HostRequest $request)
|
||||
{
|
||||
|
||||
$request->validated();
|
||||
$host = $this->hostRepository->find($id);
|
||||
if (! $host) {
|
||||
if (!$host) {
|
||||
return redirect()->route('package.host.index')->withErrors('Invalid Request');
|
||||
}
|
||||
if ($request->get('host_id') !== $host->host_id) {
|
||||
@@ -100,7 +153,7 @@ class Host extends Controller
|
||||
{
|
||||
$host = $this->hostRepository->find($id);
|
||||
|
||||
if (! $host) {
|
||||
if (!$host) {
|
||||
return back()->withErrors('Invalid request.');
|
||||
}
|
||||
|
||||
|
||||
@@ -16,18 +16,23 @@ class HostCachedRepository extends BaseCachedRepository implements HostRepositor
|
||||
{
|
||||
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([
|
||||
'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->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 = [])
|
||||
|
||||
@@ -12,23 +12,32 @@ class HostRepository extends BaseRepository implements HostRepositoryInterface
|
||||
return ModuleHost::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->with($relationships)->select($select);
|
||||
|
||||
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) {
|
||||
$model = $model->withCount($withCount);
|
||||
$query = $query->withCount($withCount);
|
||||
}
|
||||
|
||||
if (isset($data['page'])) {
|
||||
return $model->paginate(15);
|
||||
if ($returnQuery) {
|
||||
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 = [])
|
||||
|
||||
@@ -4,7 +4,13 @@ namespace App\Repositories\Module\Host;
|
||||
use App\Repositories\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 findBy(string $key = '',string|int|null $value = '' );
|
||||
|
||||
|
||||
Reference in New Issue
Block a user