This commit is contained in:
pbao9
2026-02-02 10:06:42 +07:00
parent a2a75b2015
commit 34575b2d96
29 changed files with 253 additions and 157 deletions
@@ -15,6 +15,7 @@ use App\Repositories\Os\OsRepositoryInterface;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Validator;
use Yajra\DataTables\Facades\DataTables;
class OsVersion extends Controller
{
@@ -39,10 +40,10 @@ class OsVersion extends Controller
public function store(OsVersionRequest $request)
{
$validatedData = $request->validated();
// Remove unnecessary fields
unset($validatedData['vcenter_id'], $validatedData['module_datacenter_id']);
DB::beginTransaction();
try {
$this->osRepository->create($validatedData);
@@ -55,14 +56,11 @@ class OsVersion extends Controller
}
public function edit(int $id = 0)
{
$version = $this->osRepository->findWithRelationships($id, ['osGroup.host.datacenter.vCenter']);
if (! $version) {
return back()->withErrors('Can\'t find version os by ID #' . $id);
}
$title = "Edit version OS";
return view('administrator.pages.package.version.edit', compact('title', 'version'));
@@ -73,12 +71,12 @@ class OsVersion extends Controller
if (! $version) {
return back()->withErrors('Can\'t find version os by ID #' . $id);
}
$validatedData = $request->validated();
// Remove unnecessary fields
unset($validatedData['vcenter_id'], $validatedData['module_datacenter_id']);
DB::beginTransaction();
try {
$this->osRepository->update($id, $validatedData);
@@ -106,4 +104,36 @@ class OsVersion extends Controller
return back()->with('success', 'Delete success');
}
public function datatable(Request $request)
{
$query = $this->osRepository->filter(
[],
['osGroup'],
[],
['*'],
true
);
return DataTables::eloquent($query)
->editColumn('os_group', function ($d) {
return $d->osGroup ? $d->osGroup->name . ' - ' . $d->osGroup->host->name : '';
})
->editColumn('status', function ($d) {
return $d->getStatus();
})
->addColumn('action', function ($d) {
return '<a href="' . route('package.version.edit', $d->id) . '"
class="btn btn-primary btn-sm">
Edit
</a>';
})
->filterColumn('host', function ($query, $keyword) {
$query->whereHas('host', function ($q) use ($keyword) {
$q->where('host', 'like', "%{$keyword}%");
});
})
->rawColumns(['action', 'status'])
->make(true);
}
}
+24 -11
View File
@@ -15,19 +15,32 @@ class OsCachedRepository extends BaseCachedRepository implements OsRepositoryInt
{
return $this->osRepository->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->osRepository
->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->osRepository->filter($data, $relationships, $withCount, $select);
});
return Cache::remember(
$cacheKey,
$this->cacheTime,
fn() =>
$this->osRepository
->filter($data, $relationships, $withCount, $select, $returnQuery)
);
}
public function findWithRelationships(int $id = 0, array $relationships = [],array $withCount = [], array $withSum = [])
{
+26 -24
View File
@@ -11,34 +11,36 @@ class OsRepository extends BaseRepository implements OsRepositoryInterface
{
return Os::class;
}
public function filter(array $data = [], array $relationships = [], array $withCount = [], array $select = ['*'])
{
$model = $this->model->with($relationships)->select($select);
// Apply filters from $data
if (isset($data['os_id'])) {
$model = $model->where('os_group_id', $data['os_id']);
public function filter(
array $data = [],
array $relationships = [],
array $withCount = [],
array $select = ['*'],
bool $returnQuery = false
) {
$query = $this->model
->select($select)
->with($relationships);
if (!empty($data['module_host_id'])) {
$query->where('module_host_id', $data['module_host_id']);
}
if (isset($data['os_group_id'])) {
$model = $model->where('os_group_id', $data['os_group_id']);
if (!empty($withCount)) {
$query->withCount($withCount);
}
if (isset($data['module_host_id'])) {
$model = $model->where('module_host_id', $data['module_host_id']);
if ($returnQuery) {
return $query;
}
if (isset($data['status'])) {
$model = $model->where('status', $data['status']);
if (!empty($data['paginate'])) {
return $query->paginate(
$data['per_page'] ?? 15
);
}
if (count($withCount) > 0) {
$model = $model->withCount($withCount);
}
if(isset($data['page'])) {
return $model->paginate(15);
}
return $model->get();
return $query->get();
}
public function findWithRelationships(int $id = 0, array $relationships = [], array $withCount = [], array $withSum = [])
{
@@ -5,7 +5,13 @@ namespace App\Repositories\Os;
use App\Repositories\BaseRepositoryInterface;
interface OsRepositoryInterface 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 = []);
}
@@ -6,7 +6,7 @@ use Closure;
use Illuminate\Contracts\View\View;
use Illuminate\View\Component;
class Breabcrumb extends Component
class AdminBreadcrumb extends Component
{
/**
* Create a new component instance.
@@ -21,6 +21,6 @@ class Breabcrumb extends Component
*/
public function render(): View|Closure|string
{
return view('components.breabcrumb');
return view('components.admin-breadcrumb');
}
}