Files
portal-api/app/Http/Controllers/Api/Admin/Hosting/ServicePlanController.php
T
baodev0 2deb4ad74e feat: Enhance service management and transaction handling
- Implemented client access token verification middleware to secure API endpoints.
- Updated ServiceController to fetch active hosting services for authenticated users.
- Refactored OrderController to ensure order retrieval is customer-specific.
- Improved error logging in VmController for unexpected vCenter responses.
- Disabled VNPay auto-deposit in TransactionController, switching to manual deposit flow.
- Enhanced ServiceClientDetailResource to include Plesk credentials for hosting accounts.
- Adjusted VpsServiceResource to dynamically set page type based on product type.
- Added logging for PowerShellService to capture errors in RDP port changes.
- Provisioning job now resolves service plans and logs relevant information for hosting accounts.
- Updated database migrations to add necessary fields for service plans and DNS servers.
- Refactored Service and ServicePlan models to establish relationships with hosting accounts and DNS servers.
2026-08-17 11:55:48 +07:00

124 lines
4.3 KiB
PHP

<?php
namespace App\Http\Controllers\Api\Admin\Hosting;
use App\Http\Controllers\Controller;
use App\Models\ServicePlan;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
class ServicePlanController extends Controller
{
public function index(): JsonResponse
{
$plans = ServicePlan::with('plesk', 'dnsServer')->orderBy('title')->get();
return response()->json(['error' => false, 'data' => $plans]);
}
public function show(int $id): JsonResponse
{
$plan = ServicePlan::with('plesk', 'dnsServer')->find($id);
if (!$plan) {
return response()->json(['error' => true, 'message' => 'Không tìm thấy gói hosting.'], 404);
}
return response()->json(['error' => false, 'data' => $plan]);
}
public function store(Request $request): JsonResponse
{
$validated = $request->validate([
'title' => 'required|string|max:255',
'vcpu' => 'required|integer|min:0',
'ram' => 'required|integer|min:0',
'disk' => 'required|integer|min:0',
'domain' => 'required|integer|min:0',
'dns_manager_server_id' => 'nullable|integer|exists:dns_manager_servers,id',
'plesk_plan_name' => 'nullable|string|max:255',
'note' => 'nullable|string',
]);
$plan = ServicePlan::create([
'title' => $validated['title'],
'vcpu' => $validated['vcpu'],
'ram' => $validated['ram'],
'disk' => $validated['disk'],
'domain' => $validated['domain'],
'dns_manager_server_id' => $validated['dns_manager_server_id'] ?? null,
]);
$this->savePlesk($plan, $validated);
return response()->json([
'error' => false,
'message' => 'Tạo gói hosting thành công.',
'data' => $plan->load('plesk', 'dnsServer'),
], 201);
}
public function update(Request $request, int $id): JsonResponse
{
$plan = ServicePlan::find($id);
if (!$plan) {
return response()->json(['error' => true, 'message' => 'Không tìm thấy gói hosting.'], 404);
}
$validated = $request->validate([
'title' => 'required|string|max:255',
'vcpu' => 'required|integer|min:0',
'ram' => 'required|integer|min:0',
'disk' => 'required|integer|min:0',
'domain' => 'required|integer|min:0',
'dns_manager_server_id' => 'nullable|integer|exists:dns_manager_servers,id',
'plesk_plan_name' => 'nullable|string|max:255',
'note' => 'nullable|string',
]);
$plan->update([
'title' => $validated['title'],
'vcpu' => $validated['vcpu'],
'ram' => $validated['ram'],
'disk' => $validated['disk'],
'domain' => $validated['domain'],
'dns_manager_server_id' => $validated['dns_manager_server_id'] ?? null,
]);
$this->savePlesk($plan, $validated);
return response()->json([
'error' => false,
'message' => 'Cập nhật gói hosting thành công.',
'data' => $plan->fresh(['plesk', 'dnsServer']),
]);
}
public function destroy(int $id): JsonResponse
{
$plan = ServicePlan::find($id);
if (!$plan) {
return response()->json(['error' => true, 'message' => 'Không tìm thấy gói hosting.'], 404);
}
$plan->delete();
return response()->json(['error' => false, 'message' => 'Xoá gói hosting thành công.']);
}
private function savePlesk(ServicePlan $plan, array $data): void
{
if (empty($data['plesk_plan_name'])) {
$plan->plesk()->delete();
return;
}
$plan->plesk()->updateOrCreate(
['service_plan_id' => $plan->id],
['plesk_plan_name' => $data['plesk_plan_name'], 'note' => $data['note'] ?? null]
);
}
}