refactor: Replace specific Hosting and VPS models and repositories with a generic Service repository and associated customer API endpoints.

This commit is contained in:
pbao9
2026-03-13 07:50:35 +07:00
parent dbd3cdafcd
commit a388eabe17
12 changed files with 82 additions and 347 deletions
@@ -1,45 +0,0 @@
<?php
namespace App\Repositories\Hosting;
use App\Repositories\BaseCachedRepository;
use Illuminate\Support\Facades\Cache;
class HostingCachedRepository extends BaseCachedRepository implements HostingRepositoryInterface
{
public function __construct(
public HostingRepository $hostingRepository
) {
parent::__construct();
}
public function getModel()
{
return $this->hostingRepository->getModel();
}
public function filter(array $data = [], array $relationships = [], array $withCount = [], array $select = ['*'])
{
$cacheKey = $this->getCacheKey(md5(json_encode([
'filterItem' => [
'data' => $data,
'relationships' => $relationships,
'select' => $select,
'withCount' => $withCount
]
])));
return Cache::remember($cacheKey, $this->cacheTime, function () use ($data, $relationships, $select, $withCount) {
return $this->hostingRepository->filter($data, $relationships, $withCount, $select);
});
}
public function findWithRelationships(int $id = 0, array $relationships = [],array $withCount = [], array $withSum = [])
{
$cacheKey = $this->getCacheKey(md5(json_encode([
'findWithRelationships' => [
'id' => $id,
'relationships' => $relationships
]
])));
return Cache::remember($cacheKey, $this->cacheTime, function () use ($id, $relationships,$withCount,$withSum) {
return $this->hostingRepository->findWithRelationships($id, $relationships,$withCount,$withSum);
});
}
}
@@ -1,11 +0,0 @@
<?php
namespace App\Repositories\Hosting;
use App\Repositories\BaseRepositoryInterface;
interface HostingRepositoryInterface extends BaseRepositoryInterface{
public function filter(array $data = [],array $relationships = [],array $withCount = [],array $select = ['*']);
public function findWithRelationships(int $id = 0, array $relationships = [],array $withCount = [], array $withSum = []);
}
@@ -1,37 +1,53 @@
<?php
namespace App\Repositories\Hosting;
namespace App\Repositories\Service;
use App\Models\Hosting;
use App\Models\Customer;
use App\Repositories\BaseRepository;
use App\Models\Service;
class HostingRepository extends BaseRepository implements HostingRepositoryInterface
class ServiceRepository extends BaseRepository implements ServiceRepositoryInterface
{
public function getModel()
{
return Hosting::class;
return Service::class;
}
public function filter(array $data = [], array $relationships = [], array $withCount = [], array $select = ['*'])
{
$model = $this->model->with($relationships)->select($select);
if (isset($data['customer_uuid'])) {
$customer = Customer::where('uuid', $data['customer_uuid'])->first();
if ($customer) {
$model = $model->where('customer_id', $customer->id);
}
} elseif (isset($data['customer_id'])) {
$model = $model->where('customer_id', $data['customer_id']);
}
if (isset($data['status'])) {
$model = $model->where("status", $data['status']);
$model = $model->where('status', $data['status']);
}
if (isset($data['group_id']) && $data['group_id']) {
$model = $model->where("group_id", $data['group_id']);
if (isset($data['product_id'])) {
$model = $model->where('product_id', $data['product_id']);
}
if (count($withCount) > 0) {
$model = $model->withCount($withCount);
}
if(isset($data['page'])) {
if (isset($data['page'])) {
return $model->paginate(15);
}
return $model->get();
}
public function findWithRelationships(int $id = 0, array $relationships = [], array $withCount = [], array $withSum = [])
{
$model = $this->model;
if (count($relationships) > 0) {
foreach ($relationships as $relationship) {
if (method_exists($model, $relationship)) {
@@ -40,7 +56,6 @@ class HostingRepository extends BaseRepository implements HostingRepositoryInter
}
}
// Xử lý withCount
if (count($withCount) > 0) {
foreach ($withCount as $key => $items) {
if (is_array($items)) {
@@ -57,7 +72,6 @@ class HostingRepository extends BaseRepository implements HostingRepositoryInter
}
}
// Xử lý withSum
if (count($withSum) > 0) {
foreach ($withSum as $key => $items) {
if (is_array($items)) {
@@ -0,0 +1,12 @@
<?php
namespace App\Repositories\Service;
use App\Repositories\BaseRepositoryInterface;
interface ServiceRepositoryInterface extends BaseRepositoryInterface
{
public function filter(array $data = [], array $relationships = [], array $withCount = [], array $select = ['*']);
public function findWithRelationships(int $id = 0, array $relationships = [], array $withCount = [], array $withSum = []);
}
@@ -1,57 +0,0 @@
<?php
namespace App\Repositories\VPS;
use App\Repositories\BaseCachedRepository;
use Illuminate\Support\Facades\Cache;
class VPSCachedRepository extends BaseCachedRepository implements VPSRepositoryInterface
{
public function __construct(
public VPSRepository $vPSRepository
) {
parent::__construct();
}
public function getModel()
{
return $this->vPSRepository->getModel();
}
public function filter(array $data = [], array $relationships = [], array $withCount = [], array $select = ['*'])
{
$cacheKey = $this->getCacheKey(md5(json_encode([
'filterItem' => [
'data' => $data,
'relationships' => $relationships,
'select' => $select,
'withCount' => $withCount
]
])));
return Cache::remember($cacheKey, $this->cacheTime, function () use ($data, $relationships, $select, $withCount) {
return $this->vPSRepository->filter($data, $relationships, $withCount, $select);
});
}
public function findWithRelationships(int $id = 0, array $relationships = [],array $withCount = [], array $withSum = [])
{
$cacheKey = $this->getCacheKey(md5(json_encode([
'findWithRelationships' => [
'id' => $id,
'relationships' => $relationships
]
])));
return Cache::remember($cacheKey, $this->cacheTime, function () use ($id, $relationships,$withCount,$withSum) {
return $this->vPSRepository->findWithRelationships($id, $relationships,$withCount,$withSum);
});
}
public function createWithOs(array $data, array $osIds = [])
{
$this->clearCache();
return $this->vPSRepository->createWithOs($data, $osIds);
}
public function updateWithOs(int $id, array $data, array $osIds = [])
{
$this->clearCache();
return $this->vPSRepository->updateWithOs($id, $data, $osIds);
}
}
-117
View File
@@ -1,117 +0,0 @@
<?php
namespace App\Repositories\VPS;
use App\Models\VPS;
use App\Repositories\BaseRepository;
class VPSRepository extends BaseRepository implements VPSRepositoryInterface
{
public function getModel()
{
return VPS::class;
}
public function filter(array $data = [], array $relationships = [], array $withCount = [], array $select = ['*'])
{
$model = $this->model->with($relationships)->select($select);
if (isset($data['status'])) {
$model = $model->where("status", $data['status']);
}
if (isset($data['group_id']) && $data['group_id']) {
$model = $model->where("group_id", $data['group_id']);
}
if (count($withCount) > 0) {
$model = $model->withCount($withCount);
}
if(isset($data['page'])) {
return $model->paginate(15);
}
return $model->get();
}
public function findWithRelationships(int $id = 0, array $relationships = [], array $withCount = [], array $withSum = [])
{
$model = $this->model;
if (count($relationships) > 0) {
foreach ($relationships as $relationship) {
if (method_exists($model, $relationship)) {
$model = $model->with($relationship);
}
}
}
// Xử lý withCount
if (count($withCount) > 0) {
foreach ($withCount as $key => $items) {
if (is_array($items)) {
foreach ($items as $alias => $item) {
$model = $model->withCount([
$key . ' as ' . $alias => function ($query) use ($item) {
$query->where($item['key'], $item['condition'], $item['value']);
}
]);
}
} else {
$model = $model->withCount($key);
}
}
}
// Xử lý withSum
if (count($withSum) > 0) {
foreach ($withSum as $key => $items) {
if (is_array($items)) {
foreach ($items as $alias => $item) {
$model = $model->withSum([
$key . ' as ' . $alias => function ($query) use ($item) {
$query->where($item['key'], $item['condition'], $item['value']);
}
], $item['sum']);
}
} else {
$model = $model->withSum($key);
}
}
}
return $model->find($id);
}
/**
* Create VPS with OS relationship
*/
public function createWithOs(array $data, array $osIds = [])
{
$vps = $this->create($data);
if (!empty($osIds) && $vps) {
$syncData = [];
foreach ($osIds as $osId) {
$syncData[$osId] = ['product_type' => 4]; // 4 = VPS type
}
$vps->operatingSystems()->sync($syncData);
}
return $vps;
}
/**
* Update VPS with OS relationship
*/
public function updateWithOs(int $id, array $data, array $osIds = [])
{
$vps = $this->update($id, $data);
if ($vps) {
$model = $this->find($id);
if ($model) {
$syncData = [];
foreach ($osIds as $osId) {
$syncData[$osId] = ['product_type' => 4]; // 4 = VPS type
}
$model->operatingSystems()->sync($syncData);
}
}
return $vps;
}
}
@@ -1,11 +0,0 @@
<?php
namespace App\Repositories\VPS;
use App\Repositories\BaseRepositoryInterface;
interface VPSRepositoryInterface extends BaseRepositoryInterface {
public function filter(array $data = [],array $relationships = [],array $withCount = [],array $select = ['*']);
public function findWithRelationships(int $id = 0, array $relationships = [],array $withCount = [], array $withSum = []);
public function createWithOs(array $data, array $osIds = []);
public function updateWithOs(int $id, array $data, array $osIds = []);
}