update logic call

This commit is contained in:
pbao9
2026-01-31 10:08:25 +07:00
parent 72b690f22e
commit 669ed87a05
144 changed files with 600 additions and 12093 deletions
@@ -11,13 +11,15 @@ use App\Repositories\Module\Host\HostRepositoryInterface;
use Exception;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Yajra\DataTables\Facades\DataTables;
class Datastore extends Controller
{
public function __construct(
public DatastoreRepositoryInterface $datastoreRepository,
public HostRepositoryInterface $hostRepository
) {}
) {
}
//
public function index(Request $request)
{
@@ -30,7 +32,7 @@ class Datastore extends Controller
]
);
$title = 'List all datastore';
return view('admin.package.datastore.index', compact('title', 'datastore'));
return view('administrator.pages.package.datastore.index', compact('title', 'datastore'));
}
public function sync(SyncDatastore $syncDatastore)
{
@@ -41,14 +43,14 @@ class Datastore extends Controller
public function create()
{
$title = 'Add Datastore';
return view('admin.package.datastore.add', compact('title'));
return view('administrator.pages.package.datastore.add', compact('title'));
}
public function store(DatastoreRequest $request)
{
$request->validated();
$host = $this->hostRepository->find($request->get('module_host_id'));
if (! $host) {
if (!$host) {
return back()->withErrors('Invalid Request');
}
$isCreated = $this->datastoreRepository->findBy('datastore', $request->get('datastore'));
@@ -70,23 +72,23 @@ class Datastore extends Controller
{
$datastore = $this->datastoreRepository->findWithRelationships($id, ['host.datacenter']);
if (! $datastore) {
if (!$datastore) {
return back()->withErrors('Invalid Request');
}
$title = 'Edit Datastore';
return view('admin.package.datastore.edit', compact('title', 'datastore'));
return view('administrator.pages.package.datastore.edit', compact('title', 'datastore'));
}
public function update(int $id, DatastoreRequest $request)
{
$datastore = $this->datastoreRepository->find($id);
if (! $datastore) {
if (!$datastore) {
return redirect()->route('package.datastore.index')->withErrors('Invalid request.');
}
$request->validated();
$host = $this->hostRepository->find($request->get('module_host_id'));
if (! $host) {
if (!$host) {
return back()->withErrors('Invalid Request');
}
if ($request->get('datastore') !== $datastore->datastore) {
@@ -110,7 +112,7 @@ class Datastore extends Controller
{
$datastore = $this->datastoreRepository->find($id);
if (! $datastore) {
if (!$datastore) {
return back()->withErrors('Invalid request.');
}
@@ -124,4 +126,28 @@ class Datastore extends Controller
}
return redirect()->route('package.datastore.index')->with('success', 'Deleted datacenter successfully!');
}
public function datatable(Request $request)
{
$query = $this->datastoreRepository->filter(
$request->all(),
['host.datacenter.vCenter'],
[],
['*'],
true
);
return DataTables::eloquent($query)
->addColumn(
'action',
fn($d) =>
'<a href="' . route('package.datastore.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);
}
}
+35 -9
View File
@@ -11,13 +11,15 @@ use App\Repositories\Module\Folder\FolderRepositoryInterface;
use Exception;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Yajra\DataTables\Facades\DataTables;
class Folder extends Controller
{
public function __construct(
public FolderRepositoryInterface $folderRepository,
public DatacenterRepositoryInterface $datacenterRepository
) {}
) {
}
public function index(Request $request)
{
$folders = $this->folderRepository->filter([
@@ -25,7 +27,7 @@ class Folder extends Controller
], ['datacenter.vCenter']);
$title = 'List all folder';
return view('admin.package.folder.index', compact('folders', 'title'));
return view('administrator.pages.package.folder.index', compact('folders', 'title'));
}
public function sync(SyncFolder $syncFolder)
{
@@ -35,14 +37,14 @@ class Folder extends Controller
public function create()
{
$title = 'Add Folder';
return view('admin.package.folder.add', compact('title'));
return view('administrator.pages.package.folder.add', compact('title'));
}
public function store(FolderRequest $request)
{
$request->validated();
$datacenter = $this->datacenterRepository->find($request->get('module_datacenter_id'));
if (! $datacenter) {
if (!$datacenter) {
return back()->withErrors('Invalid Request');
}
$isCreated = $this->folderRepository->findBy('folder', $request->get('folder'));
@@ -63,23 +65,23 @@ class Folder extends Controller
public function edit(int $id = 0)
{
$folder = $this->folderRepository->findWithRelationships($id, ['datacenter']);
if (! $folder) {
if (!$folder) {
return back()->withErrors('Invalid Request');
}
$title = 'Edit Folder';
return view('admin.package.folder.edit', compact('title', 'folder'));
return view('administrator.pages.package.folder.edit', compact('title', 'folder'));
}
public function update(int $id, FolderRequest $request)
{
$folder = $this->folderRepository->find($id);
if (! $folder) {
if (!$folder) {
return redirect()->route('package.folder.index')->withErrors('Invalid request.');
}
$request->validated();
$datacenter = $this->datacenterRepository->find($request->get('module_datacenter_id'));
if (! $datacenter) {
if (!$datacenter) {
return back()->withErrors('Invalid Request');
}
if ($request->get('folder') !== $folder->folder) {
@@ -104,7 +106,7 @@ class Folder extends Controller
{
$datastore = $this->folderRepository->find($id);
if (! $datastore) {
if (!$datastore) {
return back()->withErrors('Invalid request.');
}
@@ -118,4 +120,28 @@ class Folder extends Controller
}
return redirect()->route('package.folder.index')->with('success', 'Deleted successfully!');
}
public function datatable(Request $request)
{
$query = $this->folderRepository->filter(
$request->all(),
['datacenter.vCenter'],
[],
['*'],
true
);
return DataTables::eloquent($query)
->addColumn(
'action',
fn($d) =>
'<a href="' . route('package.folder.edit', $d->id) . '" class="btn btn-primary btn-sm">Edit</a>'
)
->filterColumn('datacenter.v_center.host', function ($query, $keyword) {
$query->whereHas('datacenter.vCenter', function ($q) use ($keyword) {
$q->where('host', 'like', "%{$keyword}%");
});
})
->make(true);
}
}
@@ -16,19 +16,32 @@ class DatastoreCachedRepository extends BaseCachedRepository implements Datastor
{
return $this->datastoreRepository->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->datastoreRepository
->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->datastoreRepository->filter($data, $relationships, $withCount, $select);
});
return Cache::remember(
$cacheKey,
$this->cacheTime,
fn() =>
$this->datastoreRepository
->filter($data, $relationships, $withCount, $select, $returnQuery)
);
}
public function findBy(string $key = '', string|int|null $value = '')
{
@@ -38,11 +51,13 @@ class DatastoreCachedRepository extends BaseCachedRepository implements Datastor
'value' => $value
]
])));
return Cache::remember($cacheKey, $this->cacheTime, function () use ($key, $value) {
return $this->datastoreRepository->findBy($key, $value);
});
return Cache::remember(
$cacheKey,
$this->cacheTime,
fn() => $this->datastoreRepository->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 +65,10 @@ class DatastoreCachedRepository extends BaseCachedRepository implements Datastor
'relationships' => $relationships
]
])));
return Cache::remember($cacheKey, $this->cacheTime, function () use ($id, $relationships,$withCount,$withSum) {
return $this->datastoreRepository->findWithRelationships($id, $relationships,$withCount,$withSum);
});
return Cache::remember(
$cacheKey,
$this->cacheTime,
fn() => $this->datastoreRepository->findWithRelationships($id, $relationships, $withCount, $withSum)
);
}
}
@@ -11,23 +11,30 @@ class DatastoreRepository extends BaseRepository implements DatastoreRepositoryI
{
return ModuleDatastore::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 findWithRelationships(int $id = 0, array $relationships = [], array $withCount = [], array $withSum = [])
{
@@ -3,9 +3,16 @@ namespace App\Repositories\Module\Datastore;
use App\Repositories\BaseRepositoryInterface;
interface DatastoreRepositoryInterface 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 DatastoreRepositoryInterface 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 = []);
}
@@ -16,19 +16,32 @@ class FolderCachedRepository extends BaseCachedRepository implements FolderRepos
{
return $this->folderRepository->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->folderRepository
->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->folderRepository->filter($data, $relationships, $withCount, $select);
});
return Cache::remember(
$cacheKey,
$this->cacheTime,
fn() =>
$this->folderRepository
->filter($data, $relationships, $withCount, $select, $returnQuery)
);
}
public function findBy(string $key = '', string|int|null $value = '')
{
@@ -42,7 +55,7 @@ class FolderCachedRepository extends BaseCachedRepository implements FolderRepos
return $this->folderRepository->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 +63,8 @@ class FolderCachedRepository extends BaseCachedRepository implements FolderRepos
'relationships' => $relationships
]
])));
return Cache::remember($cacheKey, $this->cacheTime, function () use ($id, $relationships,$withCount,$withSum) {
return $this->folderRepository->findWithRelationships($id, $relationships,$withCount,$withSum);
return Cache::remember($cacheKey, $this->cacheTime, function () use ($id, $relationships, $withCount, $withSum) {
return $this->folderRepository->findWithRelationships($id, $relationships, $withCount, $withSum);
});
}
}
@@ -11,21 +11,26 @@ class FolderRepository extends BaseRepository implements FolderRepositoryInterfa
{
return ModuleFolder::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['module_datacenter_id']) && $data['module_datacenter_id']) {
$model = $model->where("module_datacenter_id", $data['module_datacenter_id']);
$query->where("module_datacenter_id", $data['module_datacenter_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 = '')
{
@@ -3,9 +3,16 @@ namespace App\Repositories\Module\Folder;
use App\Repositories\BaseRepositoryInterface;
interface FolderRepositoryInterface 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 FolderRepositoryInterface 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 = []);
}