Files
portal-api/app/Http/Controllers/Api/Admin/Hosting/HostingAccountController.php
T
baodev0 d6d63e5152 feat: Implement Plesk domain and hosting services
- Added PleskDomainService for managing domain operations (create, update, remove, get info).
- Introduced PleskHostingService for provisioning hosting accounts and notifying customers.
- Created PleskService for handling API requests to Plesk.
- Added migration to include 'type' column in dns_manager_servers table.
- Created migration for module_hosting_accounts table to track hosting subscriptions.
- Created migration for module_hosting_plesk_clients table to manage Plesk client credentials.
- Added email templates for notifying customers and admins about hosting account creation.
2026-08-06 17:24:01 +07:00

147 lines
4.7 KiB
PHP

<?php
namespace App\Http\Controllers\Api\Admin\Hosting;
use App\Http\Controllers\Controller;
use App\Http\Requests\ModuleHosting\CreateHostingAccountRequest;
use App\Http\Requests\ModuleHosting\UpdateHostingAccountRequest;
use App\Http\Resources\Api\Admin\HostingAccountResource;
use App\Repositories\Module\HostingAccount\HostingAccountRepositoryInterface;
use App\Services\Plesk\PleskHostingService;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
class HostingAccountController extends Controller
{
private const RELATIONSHIPS = ['customer', 'server', 'domain'];
public function __construct(
private readonly HostingAccountRepositoryInterface $accounts,
private readonly PleskHostingService $pleskHostingService,
) {}
public function index(Request $request): JsonResponse
{
$perPage = min((int) $request->get('per_page', 15), 500);
$filter = $request->get('filter');
$status = $request->get('status');
$customerId = $request->get('customer_id');
$query = $this->accounts->model
->with(self::RELATIONSHIPS)
->newQuery();
if ($filter) {
$query->where(function ($sub) use ($filter) {
$sub->where('hosting_plan', 'like', "%{$filter}%")
->orWhere('ftp_username', 'like', "%{$filter}%")
->orWhere('system_username', 'like', "%{$filter}%");
});
}
if ($status !== null && $status !== '') {
$query->where('status', $status);
}
if ($customerId) {
$query->where('customer_id', $customerId);
}
$paginator = $query->orderByDesc('id')->paginate($perPage);
return response()->json([
'error' => false,
'data' => [
'items' => HostingAccountResource::collection($paginator->getCollection()),
'total' => $paginator->total(),
'per_page' => $paginator->perPage(),
'current_page' => $paginator->currentPage(),
'last_page' => $paginator->lastPage(),
],
]);
}
public function show(int $id): JsonResponse
{
$account = $this->accounts->model->with(self::RELATIONSHIPS)->find($id);
if (!$account) {
return response()->json(['error' => true, 'message' => "Không tìm thấy hosting account #$id"], 404);
}
return response()->json(['error' => false, 'data' => new HostingAccountResource($account)]);
}
public function store(CreateHostingAccountRequest $request): JsonResponse
{
$result = $this->pleskHostingService->provision($request->validated());
if ($result['error']) {
return response()->json([
'error' => true,
'message' => $result['message'] ?? 'Không thể tạo hosting account trên Plesk',
], 422);
}
$account = $result['data']->load(self::RELATIONSHIPS);
return response()->json([
'error' => false,
'data' => new HostingAccountResource($account),
// Not persisted anywhere — this response is the only place it's ever shown.
'ftp_password' => $result['ftp_password'],
], 201);
}
public function update(int $id, UpdateHostingAccountRequest $request): JsonResponse
{
$account = $this->accounts->find($id);
if (!$account) {
return response()->json(['error' => true, 'message' => "Không tìm thấy hosting account #$id"], 404);
}
$this->accounts->update($id, $request->validated());
return $this->show($id);
}
public function destroy(int $id): JsonResponse
{
$account = $this->accounts->find($id);
if (!$account) {
return response()->json(['error' => true, 'message' => "Không tìm thấy hosting account #$id"], 404);
}
// Local record only — does not deprovision the subscription on Plesk,
// same as DnsManager\DomainController::destroy leaving the Plesk side alone.
$this->accounts->delete($id);
return response()->json(['error' => false, 'message' => 'Đã xoá hosting account']);
}
public function suspend(int $id): JsonResponse
{
return $this->notImplemented();
}
public function unsuspend(int $id): JsonResponse
{
return $this->notImplemented();
}
public function resetFtpPassword(int $id): JsonResponse
{
return $this->notImplemented();
}
private function notImplemented(): JsonResponse
{
return response()->json([
'error' => true,
'message' => 'Not implemented in this phase',
], 501);
}
}