feat: Add DNS management resources and importer
- 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.
This commit is contained in:
@@ -4,46 +4,48 @@ 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\Crypt;
|
||||
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');
|
||||
$filter = $request->get('filter');
|
||||
$status = $request->get('status');
|
||||
|
||||
$query = $this->serverRepository->model->newQuery();
|
||||
|
||||
if ($filter) {
|
||||
$query->where(function ($q) use ($filter) {
|
||||
$q->where('name', 'like', "%{$filter}%")
|
||||
->orWhere('ip_address', 'like', "%{$filter}%");
|
||||
$query->where(function ($subQuery) use ($filter) {
|
||||
$subQuery->where('name', 'like', "%{$filter}%")
|
||||
->orWhere('ip_address', 'like', "%{$filter}%")
|
||||
->orWhere('username', 'like', "%{$filter}%");
|
||||
});
|
||||
}
|
||||
|
||||
if ($status) {
|
||||
if ($status !== null && $status !== '') {
|
||||
$query->where('status', $status);
|
||||
}
|
||||
|
||||
$paginator = $query->paginate($perPage);
|
||||
$paginator = $query->orderByDesc('id')->paginate($perPage);
|
||||
|
||||
return response()->json([
|
||||
'error' => false,
|
||||
'data' => [
|
||||
'items' => $paginator->items(),
|
||||
'total' => $paginator->total(),
|
||||
'per_page' => $paginator->perPage(),
|
||||
'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(),
|
||||
'last_page' => $paginator->lastPage(),
|
||||
],
|
||||
]);
|
||||
}
|
||||
@@ -56,30 +58,44 @@ class ServerController extends Controller
|
||||
return response()->json(['error' => true, 'message' => "Không tìm thấy server #$id"], 404);
|
||||
}
|
||||
|
||||
return response()->json(['error' => false, 'data' => $server]);
|
||||
return response()->json([
|
||||
'error' => false,
|
||||
'data' => $this->formatServer($server),
|
||||
]);
|
||||
}
|
||||
|
||||
public function store(Request $request): JsonResponse
|
||||
{
|
||||
$request->validate([
|
||||
'name' => 'required|string|max:255',
|
||||
$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',
|
||||
'username' => 'required|string|max:255',
|
||||
'password' => 'required|string|max:255',
|
||||
'api_key' => 'nullable|string',
|
||||
'status' => 'nullable|string|in:activated,Stopped',
|
||||
]);
|
||||
|
||||
DB::beginTransaction();
|
||||
|
||||
try {
|
||||
$data = $request->all();
|
||||
$data['password'] = Crypt::encryptString($request->password);
|
||||
$server = $this->serverRepository->create($data);
|
||||
$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' => $server], 201);
|
||||
return response()->json([
|
||||
'error' => false,
|
||||
'data' => $this->formatServer($server),
|
||||
], 201);
|
||||
} catch (\Exception $e) {
|
||||
DB::rollBack();
|
||||
|
||||
return response()->json(['error' => true, 'message' => $e->getMessage()], 500);
|
||||
}
|
||||
}
|
||||
@@ -92,29 +108,34 @@ class ServerController extends Controller
|
||||
return response()->json(['error' => true, 'message' => "Không tìm thấy server #$id"], 404);
|
||||
}
|
||||
|
||||
$request->validate([
|
||||
'name' => 'required|string|max:255',
|
||||
'ip_address' => 'required|string|max:255',
|
||||
'username' => 'required|string|max:255',
|
||||
'password' => 'nullable|string|max:255',
|
||||
'api_key' => 'nullable|string',
|
||||
'status' => 'nullable|string|in:activated,Stopped',
|
||||
$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 {
|
||||
$data = $request->except(['password']);
|
||||
|
||||
if (!empty($request->password)) {
|
||||
$data['password'] = Crypt::encryptString($request->password);
|
||||
try {
|
||||
$payload = collect($validated)
|
||||
->except('password')
|
||||
->toArray();
|
||||
|
||||
if ($request->filled('password')) {
|
||||
$payload['password'] = encrypt((string) $request->password);
|
||||
}
|
||||
|
||||
$this->serverRepository->update($id, $data);
|
||||
$this->serverRepository->update($id, $payload);
|
||||
|
||||
DB::commit();
|
||||
|
||||
return response()->json(['error' => false, 'data' => $this->serverRepository->find($id)]);
|
||||
return $this->show($id);
|
||||
} catch (\Exception $e) {
|
||||
DB::rollBack();
|
||||
|
||||
return response()->json(['error' => true, 'message' => $e->getMessage()], 500);
|
||||
}
|
||||
}
|
||||
@@ -131,4 +152,37 @@ class ServerController extends Controller
|
||||
|
||||
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,
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user