From a388eabe1760781dee4f018e230e2a3c3287c096 Mon Sep 17 00:00:00 2001 From: pbao9 Date: Fri, 13 Mar 2026 07:50:35 +0700 Subject: [PATCH] refactor: Replace specific Hosting and VPS models and repositories with a generic Service repository and associated customer API endpoints. --- .../Api/Admin/Customer/CustomerController.php | 37 ++++++ app/Models/Hosting.php | 40 ------ app/Models/VPS.php | 55 -------- app/Providers/RepositoryProvider.php | 7 ++ .../Hosting/HostingCachedRepository.php | 45 ------- .../Hosting/HostingRepositoryInterface.php | 11 -- .../ServiceRepository.php} | 36 ++++-- .../Service/ServiceRepositoryInterface.php | 12 ++ app/Repositories/VPS/VPSCachedRepository.php | 57 --------- app/Repositories/VPS/VPSRepository.php | 117 ------------------ .../VPS/VPSRepositoryInterface.php | 11 -- routes/api.php | 1 + 12 files changed, 82 insertions(+), 347 deletions(-) delete mode 100644 app/Models/Hosting.php delete mode 100644 app/Models/VPS.php delete mode 100644 app/Repositories/Hosting/HostingCachedRepository.php delete mode 100644 app/Repositories/Hosting/HostingRepositoryInterface.php rename app/Repositories/{Hosting/HostingRepository.php => Service/ServiceRepository.php} (73%) create mode 100644 app/Repositories/Service/ServiceRepositoryInterface.php delete mode 100644 app/Repositories/VPS/VPSCachedRepository.php delete mode 100644 app/Repositories/VPS/VPSRepository.php delete mode 100644 app/Repositories/VPS/VPSRepositoryInterface.php diff --git a/app/Http/Controllers/Api/Admin/Customer/CustomerController.php b/app/Http/Controllers/Api/Admin/Customer/CustomerController.php index 773b023..afca314 100644 --- a/app/Http/Controllers/Api/Admin/Customer/CustomerController.php +++ b/app/Http/Controllers/Api/Admin/Customer/CustomerController.php @@ -17,6 +17,7 @@ use App\Repositories\Module\Domain\DomainRepositoryInterface; use App\Repositories\Module\Hosting\HostingRepositoryInterface as ModuleHostingRepositoryInterface; use App\Repositories\Module\Vmware\VmwareRepositoryInterface; use App\Repositories\Order\OrderRepositoryInterface; +use App\Repositories\Service\ServiceRepositoryInterface; use App\Repositories\Transaction\TransactionRepositoryInterface; use Illuminate\Http\JsonResponse; use Illuminate\Http\Request; @@ -35,6 +36,7 @@ class CustomerController extends Controller private readonly OrderRepositoryInterface $orderRepository, private readonly TransactionRepositoryInterface $transactionRepository, private readonly DomainRepositoryInterface $moduleDomainRepository, + private readonly ServiceRepositoryInterface $serviceRepository, ) {} public function index(Request $request): JsonResponse @@ -473,4 +475,39 @@ class CustomerController extends Controller 'data' => $domains ]); } + + public function getServices(string $uuid, Request $request): JsonResponse + { + $services = $this->serviceRepository->filter( + ['customer_uuid' => $uuid], + ['product', 'orderItem', 'invoice'] + ); + + return response()->json([ + 'error' => false, + 'data' => $services + ]); + } + + public function getOrders(string $uuid, Request $request): JsonResponse + { + $customer = $this->customerRepository->findBy('uuid', $uuid); + + if (!$customer) { + return response()->json([ + 'error' => true, + 'message' => "Can't find customer by UUID #" . $uuid + ], 404); + } + + $orders = $this->orderRepository->filter( + ['client' => $customer->id], + ['customer', 'payment', 'invoice'] + ); + + return response()->json([ + 'error' => false, + 'data' => $orders + ]); + } } diff --git a/app/Models/Hosting.php b/app/Models/Hosting.php deleted file mode 100644 index a0ddb33..0000000 --- a/app/Models/Hosting.php +++ /dev/null @@ -1,40 +0,0 @@ -belongsTo(\App\Models\Type::class); - } - - public function group() - { - return $this->belongsTo(\App\Models\Group::class); - } -} diff --git a/app/Models/VPS.php b/app/Models/VPS.php deleted file mode 100644 index fa00cdc..0000000 --- a/app/Models/VPS.php +++ /dev/null @@ -1,55 +0,0 @@ -belongsTo(\App\Models\Type::class); - } - - public function host() - { - return $this->belongsTo(\App\Models\ModuleHost::class, 'module_host_id', 'id'); - } - - public function group() - { - return $this->belongsTo(\App\Models\Group::class); - } - - /** - * Many-to-many relationship với OS. - */ - public function operatingSystems() - { - return $this->belongsToMany(\App\Models\Os::class, 'product_os', 'product_id', 'os_id') - ->withTimestamps() - ->withPivot('product_type'); - } -} diff --git a/app/Providers/RepositoryProvider.php b/app/Providers/RepositoryProvider.php index 5b605b6..9e97f99 100644 --- a/app/Providers/RepositoryProvider.php +++ b/app/Providers/RepositoryProvider.php @@ -68,6 +68,8 @@ use App\Repositories\TicketReply\TicketReplyCachedRepository; use App\Repositories\TicketReply\TicketReplyRepositoryInterface; use App\Repositories\Transaction\TransactionCachedRepository; use App\Repositories\Transaction\TransactionRepositoryInterface; +use App\Repositories\Service\ServiceRepository; +use App\Repositories\Service\ServiceRepositoryInterface; use Illuminate\Support\ServiceProvider; use App\Repositories\Module\Domain\DomainRepositoryInterface as ModuleDomainRepositoryInterface; use App\Repositories\Module\Domain\DomainCachedRepository as ModuleDomainCachedRepository; @@ -261,6 +263,11 @@ class RepositoryProvider extends ServiceProvider TransactionRepositoryInterface::class, TransactionCachedRepository::class ); + //Service + $this->app->bind( + ServiceRepositoryInterface::class, + ServiceRepository::class + ); //Module Datacenter $this->app->bind( DatacenterRepositoryInterface::class, diff --git a/app/Repositories/Hosting/HostingCachedRepository.php b/app/Repositories/Hosting/HostingCachedRepository.php deleted file mode 100644 index a35f944..0000000 --- a/app/Repositories/Hosting/HostingCachedRepository.php +++ /dev/null @@ -1,45 +0,0 @@ -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); - }); - } -} diff --git a/app/Repositories/Hosting/HostingRepositoryInterface.php b/app/Repositories/Hosting/HostingRepositoryInterface.php deleted file mode 100644 index 752311e..0000000 --- a/app/Repositories/Hosting/HostingRepositoryInterface.php +++ /dev/null @@ -1,11 +0,0 @@ -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)) { diff --git a/app/Repositories/Service/ServiceRepositoryInterface.php b/app/Repositories/Service/ServiceRepositoryInterface.php new file mode 100644 index 0000000..8095566 --- /dev/null +++ b/app/Repositories/Service/ServiceRepositoryInterface.php @@ -0,0 +1,12 @@ +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); - } -} diff --git a/app/Repositories/VPS/VPSRepository.php b/app/Repositories/VPS/VPSRepository.php deleted file mode 100644 index b0b3164..0000000 --- a/app/Repositories/VPS/VPSRepository.php +++ /dev/null @@ -1,117 +0,0 @@ -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; - } -} diff --git a/app/Repositories/VPS/VPSRepositoryInterface.php b/app/Repositories/VPS/VPSRepositoryInterface.php deleted file mode 100644 index 03dbfe7..0000000 --- a/app/Repositories/VPS/VPSRepositoryInterface.php +++ /dev/null @@ -1,11 +0,0 @@ -middleware('perm:customer.edit'); Route::post('{uuid}/add-credit', [CustomerController::class, 'addCredit'])->middleware('perm:customer.edit'); Route::get('{uuid}/services', [CustomerController::class, 'getServices'])->middleware('perm:customer.view'); + Route::get('{uuid}/orders', [CustomerController::class, 'getOrders'])->middleware('perm:customer.view'); Route::get('{uuid}/invoices', [CustomerController::class, 'getInvoices'])->middleware('perm:customer.view'); Route::get('{uuid}/transactions', [CustomerController::class, 'getTransactions'])->middleware('perm:customer.view'); Route::get('{uuid}/domains', [CustomerController::class, 'getDomains'])->middleware('perm:customer.view');