- Implemented DomainCollection, DomainDetailResource, DomainNSCollection, DomainNSResource, and DomainResource for handling DNS domain and record data. - Created VpsServiceCollection and VpsServiceResource for VPS service management. - Added DnsManagerNS and DnsManagerUser models to represent DNS records and user associations. - Developed AdminZoneDnsSqlImporter for importing DNS data from SQL dumps, including customer and DNS domain records. - Added migration scripts to update the customers table with new fields and sync foreign key references for DNS tables. - Created a migration preview document outlining the mapping and schema changes for the admin zone DNS import.
189 lines
6.0 KiB
PHP
189 lines
6.0 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers\Api\Admin\DnsManager;
|
|
|
|
use App\Http\Controllers\Controller;
|
|
use App\Repositories\DnsManager\Server\DnsServerRepositoryInterface;
|
|
use App\Services\ServerService;
|
|
use Illuminate\Http\JsonResponse;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Support\Facades\DB;
|
|
|
|
class ServerController extends Controller
|
|
{
|
|
public function __construct(
|
|
private readonly DnsServerRepositoryInterface $serverRepository,
|
|
private readonly ServerService $serverService,
|
|
) {}
|
|
|
|
public function index(Request $request): JsonResponse
|
|
{
|
|
$perPage = min((int) $request->get('per_page', 15), 500);
|
|
$filter = $request->get('filter');
|
|
$status = $request->get('status');
|
|
|
|
$query = $this->serverRepository->model->newQuery();
|
|
|
|
if ($filter) {
|
|
$query->where(function ($subQuery) use ($filter) {
|
|
$subQuery->where('name', 'like', "%{$filter}%")
|
|
->orWhere('ip_address', 'like', "%{$filter}%")
|
|
->orWhere('username', 'like', "%{$filter}%");
|
|
});
|
|
}
|
|
|
|
if ($status !== null && $status !== '') {
|
|
$query->where('status', $status);
|
|
}
|
|
|
|
$paginator = $query->orderByDesc('id')->paginate($perPage);
|
|
|
|
return response()->json([
|
|
'error' => false,
|
|
'data' => [
|
|
'items' => $paginator->getCollection()->map(fn($server) => $this->formatServer($server)),
|
|
'total' => $paginator->total(),
|
|
'per_page' => $paginator->perPage(),
|
|
'current_page' => $paginator->currentPage(),
|
|
'last_page' => $paginator->lastPage(),
|
|
],
|
|
]);
|
|
}
|
|
|
|
public function show(int $id): JsonResponse
|
|
{
|
|
$server = $this->serverRepository->find($id);
|
|
|
|
if (!$server) {
|
|
return response()->json(['error' => true, 'message' => "Không tìm thấy server #$id"], 404);
|
|
}
|
|
|
|
return response()->json([
|
|
'error' => false,
|
|
'data' => $this->formatServer($server),
|
|
]);
|
|
}
|
|
|
|
public function store(Request $request): JsonResponse
|
|
{
|
|
$validated = $request->validate([
|
|
'name' => 'required|string|max:255',
|
|
'ip_address' => 'required|string|max:255',
|
|
'username' => 'required|string|max:255',
|
|
'password' => 'required|string|max:255',
|
|
'api_key' => 'nullable|string',
|
|
'status' => 'nullable|string|in:activated,Stopped',
|
|
]);
|
|
|
|
DB::beginTransaction();
|
|
|
|
try {
|
|
$server = $this->serverRepository->create([
|
|
'name' => $validated['name'],
|
|
'ip_address' => $validated['ip_address'],
|
|
'username' => $validated['username'],
|
|
'password' => encrypt($validated['password']),
|
|
'api_key' => $validated['api_key'] ?? null,
|
|
'status' => $validated['status'] ?? 'activated',
|
|
]);
|
|
|
|
DB::commit();
|
|
|
|
return response()->json([
|
|
'error' => false,
|
|
'data' => $this->formatServer($server),
|
|
], 201);
|
|
} catch (\Exception $e) {
|
|
DB::rollBack();
|
|
|
|
return response()->json(['error' => true, 'message' => $e->getMessage()], 500);
|
|
}
|
|
}
|
|
|
|
public function update(int $id, Request $request): JsonResponse
|
|
{
|
|
$server = $this->serverRepository->find($id);
|
|
|
|
if (!$server) {
|
|
return response()->json(['error' => true, 'message' => "Không tìm thấy server #$id"], 404);
|
|
}
|
|
|
|
$validated = $request->validate([
|
|
'name' => 'sometimes|required|string|max:255',
|
|
'ip_address' => 'sometimes|required|string|max:255',
|
|
'username' => 'sometimes|required|string|max:255',
|
|
'password' => 'nullable|string|max:255',
|
|
'api_key' => 'nullable|string',
|
|
'status' => 'nullable|string|in:activated,Stopped',
|
|
]);
|
|
|
|
DB::beginTransaction();
|
|
|
|
try {
|
|
$payload = collect($validated)
|
|
->except('password')
|
|
->toArray();
|
|
|
|
if ($request->filled('password')) {
|
|
$payload['password'] = encrypt((string) $request->password);
|
|
}
|
|
|
|
$this->serverRepository->update($id, $payload);
|
|
|
|
DB::commit();
|
|
|
|
return $this->show($id);
|
|
} catch (\Exception $e) {
|
|
DB::rollBack();
|
|
|
|
return response()->json(['error' => true, 'message' => $e->getMessage()], 500);
|
|
}
|
|
}
|
|
|
|
public function destroy(int $id): JsonResponse
|
|
{
|
|
$server = $this->serverRepository->find($id);
|
|
|
|
if (!$server) {
|
|
return response()->json(['error' => true, 'message' => "Không tìm thấy server #$id"], 404);
|
|
}
|
|
|
|
$this->serverRepository->delete($id);
|
|
|
|
return response()->json(['error' => false, 'message' => 'Đã xoá server thành công']);
|
|
}
|
|
|
|
public function checkConnection(int $id): JsonResponse
|
|
{
|
|
$server = $this->serverRepository->find($id);
|
|
|
|
if (!$server) {
|
|
return response()->json(['error' => true, 'message' => "Không tìm thấy server #$id"], 404);
|
|
}
|
|
|
|
$connected = $this->serverService->checkConnectionByServer($server);
|
|
|
|
return response()->json([
|
|
'error' => !$connected,
|
|
'data' => ['connected' => $connected],
|
|
'message' => $connected
|
|
? 'Kết nối server thành công.'
|
|
: 'Không thể kết nối tới server. Vui lòng kiểm tra lại IP hoặc tài khoản.',
|
|
]);
|
|
}
|
|
|
|
private function formatServer($server): array
|
|
{
|
|
return [
|
|
'id' => $server->id,
|
|
'name' => $server->name,
|
|
'ip_address' => $server->ip_address,
|
|
'username' => $server->username,
|
|
'api_key' => $server->api_key,
|
|
'status' => $server->status,
|
|
'created_at' => $server->created_at,
|
|
'updated_at' => $server->updated_at,
|
|
];
|
|
}
|
|
}
|