Update source into github
This commit is contained in:
@@ -0,0 +1,145 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers\admin;
|
||||
|
||||
use App\Http\Controllers\Controller;
|
||||
use App\Http\Facade\SignRSA;
|
||||
use App\Http\Requests\ModuleDNS\CreateDNSRequest;
|
||||
use App\Http\Requests\ModuleDNS\UpdateDNSRequest;
|
||||
use App\Http\Services\Upload;
|
||||
use App\Imports\ModuleDNSImport;
|
||||
use App\Models\ModuleDnsServer;
|
||||
use App\Repositories\Module\DnsServer\DnsServerRepositoryInterface;
|
||||
use Exception;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Illuminate\Support\Facades\Storage;
|
||||
use Illuminate\Support\Facades\Validator;
|
||||
use Maatwebsite\Excel\Facades\Excel;
|
||||
|
||||
class ModuleDNSController extends Controller
|
||||
{
|
||||
public function __construct(
|
||||
public DnsServerRepositoryInterface $dnsServerRepository
|
||||
) {}
|
||||
// show all hosting module
|
||||
public function index(Request $request)
|
||||
{
|
||||
$modules = $this->dnsServerRepository->filter(
|
||||
[
|
||||
'page' => $request->get('page') ?? 1
|
||||
]
|
||||
);
|
||||
$title = 'List all server dns';
|
||||
return view('admin.module.dns.index', compact('modules', 'title'));
|
||||
}
|
||||
public function import(Request $request)
|
||||
{
|
||||
$data = Excel::toArray([], $request->file);
|
||||
if (count($data) > 0) {
|
||||
$data = array_slice($data[0], 1);
|
||||
$allData = [];
|
||||
foreach ($data as $row) {
|
||||
if (isset($row[1]) && isset($row[2]) && isset($row[3]) && !empty($row[1]) && !empty($row[1]) && !empty($row[1])) {
|
||||
$allData[] = [
|
||||
'name' => $row[1],
|
||||
'user' => $row[2],
|
||||
'password' => SignRSA::EncryptFunc($row[3])
|
||||
];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (! isset($allData) || (isset($allData) && count($allData) < 1)) {
|
||||
return back()->withErrors('No data imported');
|
||||
}
|
||||
DB::beginTransaction();
|
||||
try {
|
||||
$this->dnsServerRepository->insert($allData);
|
||||
DB::commit();
|
||||
} catch (Exception $error) {
|
||||
DB::rollBack();
|
||||
return back()->withErrors($error->getMessage());
|
||||
}
|
||||
|
||||
return back()->with('success', 'Imported successfully');
|
||||
}
|
||||
public function create()
|
||||
{
|
||||
$title = 'Add new server dns';
|
||||
return view('admin.module.dns.add', compact('title'));
|
||||
}
|
||||
public function store(CreateDNSRequest $request)
|
||||
{
|
||||
$request->validated();
|
||||
|
||||
$isExitsHost = $this->dnsServerRepository->findByName($request->get('name'));
|
||||
if ($isExitsHost) {
|
||||
return back()->withErrors('The server already exists');
|
||||
}
|
||||
DB::beginTransaction();
|
||||
try {
|
||||
$data = $request->all();
|
||||
$data['password'] = SignRSA::EncryptFunc($request->password);
|
||||
$this->dnsServerRepository->create($data);
|
||||
DB::commit();
|
||||
} catch (\Exception $error) {
|
||||
DB::rollBack();
|
||||
return back()->withErrors($error->getMessage());
|
||||
}
|
||||
return redirect()->route('module.dns.index')->with('success', 'Added successfully');
|
||||
}
|
||||
public function edit(int $id = 0)
|
||||
{
|
||||
$server = $this->dnsServerRepository->find($id);
|
||||
if (! $server) return back()->withErrors('Can\'t find server by id ' . $server);
|
||||
$title = 'View server dns information';
|
||||
return view('admin.module.dns.edit', compact('title', 'server'));
|
||||
}
|
||||
public function update(UpdateDNSRequest $request, int $id = 0)
|
||||
{
|
||||
$server = $this->dnsServerRepository->find($id);
|
||||
|
||||
if (! $server) return back()->withErrors('Invalid Request');
|
||||
|
||||
$request->validated();
|
||||
|
||||
if ($request->get('name') != $server->name) {
|
||||
$isExitsHost = $this->dnsServerRepository->findByName($request->get('name'));
|
||||
if ($isExitsHost) {
|
||||
return back()->withErrors('The server already exists');
|
||||
}
|
||||
}
|
||||
|
||||
DB::beginTransaction();
|
||||
try {
|
||||
$data = $request->all();
|
||||
if (! empty($request->get('password'))) {
|
||||
$data['password'] = SignRSA::EncryptFunc($request->password);
|
||||
}
|
||||
$this->dnsServerRepository->update($id, $data);
|
||||
DB::commit();
|
||||
} catch (\Exception $error) {
|
||||
DB::rollBack();
|
||||
return back()->withErrors($error->getMessage());
|
||||
}
|
||||
return redirect()->route('module.dns.index')->with('success', 'Updated Successfully');
|
||||
}
|
||||
public function destroy(int $id = 0)
|
||||
{
|
||||
$server = $this->dnsServerRepository->find($id);
|
||||
if (! $server) {
|
||||
return back()->withErrors('Invalid Request');
|
||||
}
|
||||
DB::beginTransaction();
|
||||
try {
|
||||
|
||||
$this->dnsServerRepository->delete($id);
|
||||
DB::commit();
|
||||
} catch (\Exception $error) {
|
||||
DB::rollBack();
|
||||
return back()->withErrors($error->getMessage());
|
||||
}
|
||||
return redirect()->route('module.dns.index')->with('success', 'Deleted successfully.');
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user