feat: Implement DNS management with dedicated models, repositories, requests, controllers, and services, while refactoring authentication by removing Passport and Sanctum.

This commit is contained in:
pbao9
2026-03-09 16:43:06 +07:00
parent 80a5e0ca3d
commit 4e4807dbea
47 changed files with 1905 additions and 1243 deletions
@@ -0,0 +1,377 @@
<?php
namespace App\Http\Controllers\Backend;
use App\Http\Controllers\Controller;
use App\Http\Requests\Domain\DomainRemoveRequest;
use App\Http\Requests\Domain\DomainRequest;
use App\Repositories\DnsManager\Domain\DnsDomainRepositoryInterface;
use App\Services\ServerService;
use Exception;
use Illuminate\Support\Carbon;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;
class DomainController extends Controller
{
public function __construct(
public DnsDomainRepositoryInterface $domainRepository,
public ServerService $serverService
) {}
public function index()
{
$title = 'Domains';
return view('Backend.domains.index', compact('title'));
}
public function getAll()
{
$domains = $this->domainRepository->filter([], ['users', 'server'])->map(function ($domain) {
return [
'id' => $domain->id,
'name' => $domain->domain_name,
'server' => $domain->server ? $domain->server->ip_address : '',
'user' => $domain->users->pluck('name')->implode(', '),
'checkbox' => '<input class="form-check-input m-0 align-middle" value="' . $domain->id . '" type="checkbox" aria-label="Select tag">',
'created_at' => Carbon::parse($domain->created_at)->format('Y-m-d H:i:s'),
'action' => $this->domainActionButtons($domain),
];
});
return response()->json(['data' => $domains]);
}
public function create()
{
$title = 'Add Domain';
return view('Backend.domains.create', compact('title'));
}
public function store(DomainRequest $request)
{
$validated = $request->validated();
$isCreated = $this->domainRepository->findDomain($validated['domain_name']);
if (!empty($isCreated)) {
return response()->json([
'success' => false,
'message' => "Domain '{$validated['domain_name']}' đã tồn tại trong hệ thống!",
]);
}
DB::beginTransaction();
try {
$userIds = $validated['user_id'] ?? [];
$domainData = collect($validated)->except('user_id')->toArray();
$domain = $this->domainRepository->create($domainData);
$domain->users()->attach($userIds);
$domain->save();
try {
$server = $this->serverService->findServerByID($domain->dns_manager_server_id);
if (!$server) {
throw new Exception("Không tìm thấy server với ID: {$domain->dns_manager_server_id}");
}
$existingDomainInfo = $this->serverService->getDomainInfo($domain->domain_name);
if ($existingDomainInfo) {
$this->domainRepository->update($domain->id, [
'is_created' => 1,
'plesk_id' => $existingDomainInfo['id'],
]);
} else {
$response = $this->serverService->createDomain($domain->toArray());
if (!$response || !isset($response['id'])) {
throw new Exception('Không thể tạo domain trên server. Vui lòng kiểm tra lại thông tin server.');
}
$this->domainRepository->update($domain->id, [
'is_created' => 1,
'plesk_id' => $response['id'],
]);
}
} catch (Exception $domainError) {
Log::error('Lỗi khi tạo domain: ' . $domainError->getMessage());
}
DB::commit();
$domain = $domain->refresh();
$message = $domain->is_created == 1 && $domain->plesk_id
? "Domain '{$domain->domain_name}' đã được xử lý thành công trên server!"
: "Domain '{$domain->domain_name}' đã được lưu vào database nhưng chưa thể xử lý trên server.";
return response()->json(['success' => true, 'message' => $message, 'url' => route('admin.domain.index')]);
} catch (Exception $error) {
DB::rollBack();
return response()->json(['success' => false, 'message' => 'Có lỗi xảy ra: ' . $error->getMessage()]);
}
}
public function edit(int $id)
{
$domain = $this->domainRepository->find($id);
if (empty($domain)) {
return back()->withErrors("Không tìm thấy domain với ID: {$id}");
}
$title = 'Edit domain';
return view('Backend.domains.edit', compact('title', 'domain'));
}
public function update(DomainRequest $request, int $id)
{
$domain = $this->domainRepository->find($id);
if (empty($domain)) {
return response()->json(['success' => false, 'message' => "Không tìm thấy domain với ID: {$id}"]);
}
$validated = $request->validated();
DB::beginTransaction();
try {
$userIds = $validated['user_id'] ?? [];
$domainData = collect($validated)->except('user_id')->toArray();
$this->domainRepository->update($id, $domainData);
$domain->users()->sync($userIds);
try {
$updatedDomain = $this->domainRepository->find($id);
$server = $this->serverService->findServerByID($updatedDomain->dns_manager_server_id);
if (!$server) {
throw new Exception("Không tìm thấy server với ID: {$updatedDomain->dns_manager_server_id}");
}
$existingDomainInfo = $this->serverService->getDomainInfo($updatedDomain->domain_name);
if ($existingDomainInfo) {
$this->serverService->updateDomain($existingDomainInfo['id'], $updatedDomain->toArray());
$this->domainRepository->update($updatedDomain->id, [
'is_created' => 1,
'plesk_id' => $existingDomainInfo['id'],
]);
} else {
$response = $this->serverService->createDomain($updatedDomain->toArray());
if (!$response || !isset($response['id'])) {
throw new Exception('Không thể tạo domain trên server.');
}
$this->domainRepository->update($updatedDomain->id, [
'is_created' => 1,
'plesk_id' => $response['id'],
]);
}
} catch (Exception $domainError) {
Log::error('Lỗi khi cập nhật domain: ' . $domainError->getMessage());
}
DB::commit();
$updatedDomain = $this->domainRepository->find($id);
$message = $updatedDomain->is_created == 1 && $updatedDomain->plesk_id
? "Domain '{$updatedDomain->domain_name}' đã được cập nhật thành công trên server!"
: "Domain '{$updatedDomain->domain_name}' đã được cập nhật trong database nhưng chưa thể xử lý trên server.";
return response()->json(['success' => true, 'message' => $message, 'url' => route('admin.domain.index')]);
} catch (Exception $error) {
DB::rollBack();
return response()->json(['success' => false, 'message' => 'Có lỗi xảy ra: ' . $error->getMessage()]);
}
}
public function record(int $id = 0)
{
$domain = $this->domainRepository->findWithRelations($id, ['record']);
if (empty($domain)) {
return back()->withErrors("Không tìm thấy domain với ID: {$id}");
}
$title = strtoupper($domain->domain_name) . ' Records';
return view('Backend.domains.view', compact('domain', 'title', 'id'));
}
public function destroy(int $id)
{
$domain = $this->domainRepository->find($id);
if (!$domain) {
return response()->json(['success' => false, 'message' => "Không tìm thấy domain với ID: {$id}"]);
}
try {
if ($domain->is_created !== 1 || !$domain->plesk_id) {
$this->domainRepository->delete($domain->id);
return response()->json([
'success' => true,
'message' => "Domain '{$domain->domain_name}' đã được xóa khỏi database.",
'url' => route('admin.domain.index'),
]);
}
$server = $this->serverService->findServerByID($domain->dns_manager_server_id);
if (!$server) {
return response()->json(['success' => false, 'message' => "Không tìm thấy server với ID: {$domain->dns_manager_server_id}"]);
}
$removed = $this->serverService->removeDomain($domain->plesk_id);
if (!$removed) {
return response()->json(['success' => false, 'message' => "Không thể xóa domain '{$domain->domain_name}' trên server."]);
}
$this->domainRepository->delete($domain->id);
$this->clearDomainCaches($domain);
return response()->json([
'success' => true,
'message' => "Domain '{$domain->domain_name}' đã được xóa thành công khỏi server và database!",
'url' => route('admin.domain.index'),
]);
} catch (Exception $error) {
return response()->json(['success' => false, 'message' => 'Có lỗi xảy ra khi xóa domain: ' . $error->getMessage()]);
}
}
public function destroyCustom(DomainRemoveRequest $request)
{
$request->validated();
$domains = $this->domainRepository->findWhereIn('id', $request->get('ids'));
if (!$domains || $domains->isEmpty()) {
return response()->json(['success' => false, 'message' => 'Không tìm thấy domain nào.']);
}
$successCount = 0;
$errorCount = 0;
$errors = [];
foreach ($domains as $domain) {
try {
if ($domain->is_created !== 1 || !$domain->plesk_id) {
$this->domainRepository->delete($domain->id);
$successCount++;
continue;
}
$server = $this->serverService->findServerByID($domain->dns_manager_server_id);
if (!$server) {
$errors[] = "Domain '{$domain->domain_name}': Không tìm thấy server";
$errorCount++;
continue;
}
$removed = $this->serverService->removeDomain($domain->plesk_id);
if (!$removed) {
$errors[] = "Domain '{$domain->domain_name}': Không thể xóa trên server";
$errorCount++;
continue;
}
$this->domainRepository->delete($domain->id);
$this->clearDomainCaches($domain);
$successCount++;
} catch (Exception $error) {
$errors[] = "Domain '{$domain->domain_name}': " . $error->getMessage();
$errorCount++;
}
}
if ($successCount > 0 && $errorCount === 0) {
$message = "Đã xóa thành công {$successCount} domain!";
} elseif ($successCount > 0) {
$message = "Đã xóa thành công {$successCount} domain, {$errorCount} domain gặp lỗi.";
} else {
$message = 'Không thể xóa domain nào. Vui lòng kiểm tra lại.';
}
return response()->json([
'success' => $errorCount === 0,
'message' => $message,
'details' => ['success_count' => $successCount, 'error_count' => $errorCount, 'errors' => $errors],
'url' => route('admin.domain.index'),
]);
}
private function domainActionButtons($domain): string
{
$editUrl = route('admin.domain.edit', ['id' => $domain->id]);
$recordUrl = route('admin.domain.record', ['id' => $domain->id]);
$deleteUrl = route('admin.domain.destroy', ['id' => $domain->id]);
return <<<HTML
<a class="btn btn-sm btn-icon btn-primary" href="{$editUrl}">
<svg class="icon" xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round">
<path stroke="none" d="M0 0h24v24H0z" fill="none"/>
<path d="M7 7h-1a2 2 0 0 0 -2 2v9a2 2 0 0 0 2 2h9a2 2 0 0 0 2 -2v-1"/>
<path d="M20.385 6.585a2.1 2.1 0 0 0 -2.97 -2.97l-8.415 8.385v3h3l8.385 -8.415z"/>
<path d="M16 5l3 3"/>
</svg>
<span class="sr-only">Edit</span>
</a>
<a class="btn btn-sm btn-icon btn-success" href="{$recordUrl}">
<svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="icon">
<path stroke="none" d="M0 0h24v24H0z" fill="none"/>
<path d="M9 5h-2a2 2 0 0 0 -2 2v12a2 2 0 0 0 2 2h10a2 2 0 0 0 2 -2v-12a2 2 0 0 0 -2 -2h-2"/>
<path d="M9 3m0 2a2 2 0 0 1 2 -2h2a2 2 0 0 1 2 2v0a2 2 0 0 1 -2 2h-2a2 2 0 0 1 -2 -2z"/>
<path d="M9 14h.01"/><path d="M9 17h.01"/><path d="M12 16l1 1l3 -3"/>
</svg>
<span class="sr-only">Records</span>
</a>
<a class="btn btn-sm btn-icon btn-icon__remove btn-danger"
href="{$deleteUrl}"
data-dt-single-action=""
data-method="DELETE"
data-confirmation-modal="true"
data-bs-target="#modal-remove-tags"
data-bs-toggle="modal"
data-confirmation-modal-title="Confirm delete"
data-confirmation-modal-message="Do you really want to delete this record?"
data-confirmation-modal-button="Delete"
data-confirmation-modal-cancel-button="Cancel">
<svg class="icon" xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round">
<path stroke="none" d="M0 0h24v24H0z" fill="none"/>
<path d="M4 7l16 0"/><path d="M10 11l0 6"/><path d="M14 11l0 6"/>
<path d="M5 7l1 12a2 2 0 0 0 2 2h8a2 2 0 0 0 2 -2l1 -12"/>
<path d="M9 7v-3a1 1 0 0 1 1 -1h4a1 1 0 0 1 1 1v3"/>
</svg>
<span class="sr-only">Delete</span>
</a>
HTML;
}
private function clearDomainCaches($domain): void
{
try {
Cache::forget("domain.{$domain->id}");
Cache::forget("domain.{$domain->domain_name}");
Cache::forget('domains.all');
Cache::forget('domains.filtered');
if ($domain->users) {
foreach ($domain->users as $user) {
Cache::forget("user.{$user->id}.domains");
}
}
if ($domain->dns_manager_server_id) {
Cache::forget("server.{$domain->dns_manager_server_id}.domains");
}
} catch (Exception $e) {
Log::warning('Failed to clear domain caches: ' . $e->getMessage());
}
}
}
@@ -0,0 +1,312 @@
<?php
namespace App\Http\Controllers\Backend;
use App\Http\Controllers\Controller;
use App\Http\Requests\Record\RecordRemoveRequest;
use App\Repositories\DnsManager\Domain\DnsDomainRepositoryInterface;
use App\Repositories\DnsManager\Record\DnsRecordRepositoryInterface;
use App\Services\ServerService;
use Exception;
use Illuminate\Http\Request;
use Illuminate\Support\Carbon;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Validator;
use Illuminate\Support\Str;
class RecordController extends Controller
{
protected array $rules = [
'A' => [
'name' => 'required',
'type' => 'required',
'ttl' => 'required',
'value' => 'required|ip|ipv4',
'plesk_id' => 'nullable',
],
'AAAA' => [
'name' => 'required',
'type' => 'required',
'ttl' => 'required',
'value' => 'required|ip|ipv6',
'plesk_id' => 'nullable',
],
'CNAME' => [
'name' => 'required',
'type' => 'required',
'ttl' => 'required',
'value' => 'required',
'plesk_id' => 'nullable',
],
'MX' => [
'name' => 'required',
'type' => 'required',
'ttl' => 'required',
'value' => 'required',
'priority' => 'required',
'plesk_id' => 'nullable',
],
'TXT' => [
'name' => 'required',
'type' => 'required',
'ttl' => 'required',
'value' => 'required',
'plesk_id' => 'nullable',
],
'CAA' => [
'name' => 'required',
'type' => 'required',
'ttl' => 'required',
'value' => 'required',
'plesk_id' => 'nullable',
],
'SRV' => [
'type' => 'required',
'ttl' => 'required',
'value' => 'required',
'service_name' => 'required',
'protocol' => 'required',
'weight' => 'required',
'port' => 'required',
'priority' => 'required',
'plesk_id' => 'nullable',
],
];
public function __construct(
public DnsDomainRepositoryInterface $domainRepository,
public DnsRecordRepositoryInterface $recordRepository,
public ServerService $serverService
) {}
public function index()
{
$title = 'All Records';
return view('Backend.records.index', compact('title'));
}
public function getAll()
{
$records = $this->recordRepository->filter([], ['domain'])->map(function ($record) {
return [
'id' => $record->id,
'name' => Str::limit($record->name, 20, '...'),
'type' => $record->type,
'value' => Str::limit($record->value, 20, '...'),
'domain' => $record->domain ? $record->domain->domain_name : '',
'checkbox' => '<input class="form-check-input m-0 align-middle" value="' . $record->id . '" type="checkbox" aria-label="Select tag">',
'created_at' => Carbon::parse($record->created_at)->format('Y-m-d H:i:s'),
'action' => $this->recordActionButtons($record),
];
});
return response()->json(['data' => $records]);
}
public function create(int $id)
{
$title = 'Add Records';
return view('Backend.records.create', compact('title', 'id'));
}
public function store(Request $request, int $id = 0)
{
$domain = $this->domainRepository->find($id);
if (empty($domain)) {
return response()->json(['success' => false, 'message' => 'These credentials do not match our records.']);
}
$validated = Validator::make($request->all(), $this->rules[$request->get('type')] ?? []);
if ($validated->fails()) {
return response()->json(['success' => false, 'message' => $validated->errors()->first()]);
}
$request->merge(['dns_manager_domain_id' => $id, 'domain' => $domain->domain_name]);
if (empty($request->get('plesk_id'))) {
$this->serverService->findServerByID($domain->dns_manager_server_id);
$this->serverService->handlePayload($request->all());
$record = $this->serverService->addRecord();
if (!empty($record) && $record['error']) {
return response()->json(['success' => false, 'message' => $record['message'] ?? 'Failed to add record on server.']);
}
}
DB::beginTransaction();
try {
if (isset($record)) {
$request->merge(['plesk_id' => $record['data']['id'] ?? 0]);
}
$this->recordRepository->create($request->all());
DB::commit();
} catch (Exception $error) {
DB::rollBack();
return response()->json(['success' => false, 'message' => $error->getMessage()]);
}
return response()->json([
'success' => true,
'message' => 'Add record successfully',
'url' => route('admin.domain.record', ['id' => $id]),
]);
}
public function edit(int $id = 0, int $record_id = 0)
{
$domain = $this->domainRepository->find($id);
if (empty($domain)) {
return back()->withErrors('These credentials do not match our records.');
}
$record = $this->recordRepository->find($record_id);
if (empty($record)) {
return back()->withErrors('These credentials do not match our records.');
}
$title = 'Edit Record';
return view('Backend.records.edit', compact('title', 'record', 'id'));
}
public function update(Request $request, int $id = 0, int $record_id = 0)
{
$domain = $this->domainRepository->find($id);
if (empty($domain)) {
return response()->json(['success' => false, 'message' => 'These credentials do not match our records.']);
}
$record = $this->recordRepository->find($record_id);
if (empty($record)) {
return response()->json(['success' => false, 'message' => 'These credentials do not match our records.']);
}
$validated = Validator::make($request->all(), $this->rules[$request->get('type')] ?? []);
if ($validated->fails()) {
return response()->json(['success' => false, 'message' => $validated->errors()->first()]);
}
$this->serverService->findServerByID($domain->dns_manager_server_id);
$this->serverService->handlePayload($request->all());
$updatedRecord = $this->serverService->updateRecord($record->plesk_id);
if (!empty($updatedRecord) && $updatedRecord['error']) {
return response()->json(['success' => false, 'message' => $updatedRecord['message'] ?? 'Failed to update record on server.']);
}
DB::beginTransaction();
try {
$this->recordRepository->update($record_id, $request->all());
DB::commit();
} catch (Exception $error) {
DB::rollBack();
return response()->json(['success' => false, 'message' => $error->getMessage()]);
}
return response()->json([
'success' => true,
'message' => 'Update record successfully',
'url' => route('admin.domain.record', ['id' => $id]),
]);
}
public function destroy(int $id = 0)
{
$record = $this->recordRepository->findWithRelations($id, ['domain']);
if (empty($record) || empty($record->domain)) {
return response()->json(['success' => false, 'message' => 'These credentials do not match our records.']);
}
$this->serverService->findServerByID($record->domain->dns_manager_server_id);
$record_delete = $this->serverService->deleteRecord($record->plesk_id);
if (!empty($record_delete) && $record_delete['error']) {
return response()->json(['success' => false, 'message' => $record_delete['message'] ?? 'Failed to delete record on server.']);
}
DB::beginTransaction();
try {
$domainId = $record->dns_manager_domain_id;
$this->recordRepository->delete($id);
DB::commit();
} catch (Exception $error) {
DB::rollBack();
return response()->json(['success' => false, 'message' => $error->getMessage()]);
}
return response()->json([
'success' => true,
'message' => 'Deleted record successfully',
'url' => route('admin.domain.record', ['id' => $domainId]),
]);
}
public function destroyCustom(RecordRemoveRequest $request)
{
$request->validated();
$records = $this->recordRepository->findWhereIn('id', $request->get('ids'), ['domain']);
if (!$records) {
return response()->json(['success' => false, 'message' => 'These credentials do not match our records.']);
}
try {
foreach ($records->all() as $record) {
if (empty($record->domain)) continue;
$this->serverService->findServerByID($record->domain->dns_manager_server_id);
$this->serverService->deleteRecord($record->plesk_id);
$this->recordRepository->delete($record->id);
}
} catch (Exception $error) {
return response()->json(['success' => false, 'message' => $error->getMessage()]);
}
return response()->json([
'success' => true,
'message' => 'Deleted successfully!',
'url' => route('admin.record.index'),
]);
}
private function recordActionButtons($record): string
{
$domainId = $record->domain ? $record->domain->id : 0;
$editUrl = route('admin.record.edit', ['id' => $domainId, 'record_id' => $record->id]);
$deleteUrl = route('admin.record.destroy', ['id' => $record->id]);
return <<<HTML
<a class="btn btn-sm btn-icon btn-primary" href="{$editUrl}">
<svg class="icon" xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round">
<path stroke="none" d="M0 0h24v24H0z" fill="none"/>
<path d="M7 7h-1a2 2 0 0 0 -2 2v9a2 2 0 0 0 2 2h9a2 2 0 0 0 2 -2v-1"/>
<path d="M20.385 6.585a2.1 2.1 0 0 0 -2.97 -2.97l-8.415 8.385v3h3l8.385 -8.415z"/>
<path d="M16 5l3 3"/>
</svg>
<span class="sr-only">Edit</span>
</a>
<a class="btn btn-sm btn-icon btn-icon__remove btn-danger"
href="{$deleteUrl}"
data-dt-single-action=""
data-method="DELETE"
data-confirmation-modal="true"
data-bs-target="#modal-remove-tags"
data-bs-toggle="modal"
data-confirmation-modal-title="Confirm delete"
data-confirmation-modal-message="Do you really want to delete this record?"
data-confirmation-modal-button="Delete"
data-confirmation-modal-cancel-button="Cancel">
<svg class="icon" xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round">
<path stroke="none" d="M0 0h24v24H0z" fill="none"/>
<path d="M4 7l16 0"/><path d="M10 11l0 6"/><path d="M14 11l0 6"/>
<path d="M5 7l1 12a2 2 0 0 0 2 2h8a2 2 0 0 0 2 -2l1 -12"/>
<path d="M9 7v-3a1 1 0 0 1 1 -1h4a1 1 0 0 1 1 1v3"/>
</svg>
<span class="sr-only">Delete</span>
</a>
HTML;
}
}
@@ -0,0 +1,204 @@
<?php
namespace App\Http\Controllers\Backend;
use App\Http\Controllers\Controller;
use App\Http\Requests\Server\ServerRemoveRequest;
use App\Http\Requests\Server\ServerRequest;
use App\Repositories\DnsManager\Server\DnsServerRepositoryInterface;
use App\Services\ServerService;
use Exception;
use Illuminate\Http\Request;
use Illuminate\Support\Carbon;
use Illuminate\Support\Facades\Crypt;
use Illuminate\Support\Facades\DB;
class ServerController extends Controller
{
public function __construct(
public DnsServerRepositoryInterface $serverRepository,
public ServerService $serverService
) {}
public function index()
{
$title = 'Server Hosting Manager';
return view('Backend.servers.index', compact('title'));
}
public function getAll()
{
$servers = $this->serverRepository->getAll()->map(function ($server) {
return [
'id' => $server->id,
'name' => $server->name,
'ip' => $server->ip_address,
'status' => $server->getStatus(),
'checkbox' => '<input class="form-check-input m-0 align-middle" value="' . $server->id . '" type="checkbox" aria-label="Select tag">',
'created_at' => Carbon::parse($server->created_at)->format('Y-m-d H:i:s'),
'action' => $this->serverActionButtons($server),
];
});
return response()->json(['data' => $servers]);
}
public function create()
{
$title = 'Add New Server Hosting';
return view('Backend.servers.create', compact('title'));
}
public function connect(ServerRequest $request)
{
$request->validated();
if (!$request->ajax()) {
return response()->json(['success' => false, 'message' => 'Method not support']);
}
$status = $this->serverService->checkConnection($request->all());
if ($status) {
return response()->json(['success' => true, 'message' => 'Connection established successfully.']);
}
return response()->json([
'success' => false,
'message' => 'Connection attempt failed. Please check your credentials or server settings.',
]);
}
public function store(ServerRequest $request)
{
$request->validated();
DB::beginTransaction();
try {
$data = $request->all();
$data['password'] = Crypt::encryptString($request->password);
$this->serverRepository->create($data);
DB::commit();
} catch (Exception $error) {
DB::rollBack();
return response()->json(['success' => false, 'message' => $error->getMessage()]);
}
return response()->json([
'success' => true,
'message' => 'Created successfully!',
'url' => route('admin.server.index'),
]);
}
public function edit(int $id)
{
$server = $this->serverRepository->find($id);
if (!$server) {
return back()->withErrors('Invalid Request');
}
$title = 'Update Server Hosting';
return view('Backend.servers.edit', compact('title', 'server'));
}
public function update(ServerRequest $request, int $id)
{
$request->validated();
DB::beginTransaction();
try {
$data = $request->all();
$data['password'] = Crypt::encryptString($request->password);
$this->serverRepository->update($id, $data);
DB::commit();
} catch (Exception $error) {
DB::rollBack();
return response()->json(['success' => false, 'message' => $error->getMessage()]);
}
return response()->json([
'success' => true,
'message' => 'Updated successfully!',
'url' => route('admin.server.index'),
]);
}
public function destroy(int $id)
{
$server = $this->serverRepository->find($id);
if (!$server) {
return response()->json(['success' => false, 'message' => 'Invalid Request']);
}
$this->serverRepository->delete($id);
return response()->json([
'success' => true,
'message' => 'Deleted successfully!',
'url' => route('admin.server.index'),
]);
}
public function destroyCustom(ServerRemoveRequest $request)
{
$request->validated();
$servers = $this->serverRepository->findWhereIn('id', $request->get('ids'));
if (!$servers) {
return response()->json(['success' => false, 'message' => 'Invalid Request']);
}
foreach ($servers->all() as $server) {
$this->serverRepository->delete($server->id);
}
return response()->json([
'success' => true,
'message' => 'Deleted successfully!',
'url' => route('admin.server.index'),
]);
}
private function serverActionButtons($server): string
{
$editUrl = route('admin.server.edit', ['id' => $server->id]);
$deleteUrl = route('admin.server.destroy', ['id' => $server->id]);
return <<<HTML
<a class="btn btn-sm btn-icon btn-primary" href="{$editUrl}">
<svg class="icon" xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round">
<path stroke="none" d="M0 0h24v24H0z" fill="none"></path>
<path d="M7 7h-1a2 2 0 0 0 -2 2v9a2 2 0 0 0 2 2h9a2 2 0 0 0 2 -2v-1"></path>
<path d="M20.385 6.585a2.1 2.1 0 0 0 -2.97 -2.97l-8.415 8.385v3h3l8.385 -8.415z"></path>
<path d="M16 5l3 3"></path>
</svg>
<span class="sr-only">Edit</span>
</a>
<a class="btn btn-sm btn-icon btn-icon__remove btn-danger"
href="{$deleteUrl}"
data-dt-single-action=""
data-method="DELETE"
data-confirmation-modal="true"
data-bs-target="#modal-remove-tags"
data-bs-toggle="modal"
data-confirmation-modal-title="Confirm delete"
data-confirmation-modal-message="Do you really want to delete this record?"
data-confirmation-modal-button="Delete"
data-confirmation-modal-cancel-button="Cancel">
<svg class="icon" xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round">
<path stroke="none" d="M0 0h24v24H0z" fill="none"></path>
<path d="M4 7l16 0"></path>
<path d="M10 11l0 6"></path>
<path d="M14 11l0 6"></path>
<path d="M5 7l1 12a2 2 0 0 0 2 2h8a2 2 0 0 0 2 -2l1 -12"></path>
<path d="M9 7v-3a1 1 0 0 1 1 -1h4a1 1 0 0 1 1 1v3"></path>
</svg>
<span class="sr-only">Delete</span>
</a>
HTML;
}
}