205 lines
7.2 KiB
PHP
205 lines
7.2 KiB
PHP
<?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;
|
|
}
|
|
}
|