Update source into github
This commit is contained in:
@@ -0,0 +1,137 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers\admin\package;
|
||||
|
||||
use App\Http\Controllers\Controller;
|
||||
use App\Http\Requests\Package\OsGroupRequest;
|
||||
use App\Http\Services\Upload;
|
||||
use App\Imports\GroupImport;
|
||||
use App\Models\ModuleDatastore;
|
||||
use App\Models\ModuleHost;
|
||||
use Illuminate\Http\Request;
|
||||
use App\Models\OsGroup;
|
||||
use App\Repositories\OsGroup\OsGroupRepositoryInterface;
|
||||
use Exception;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Illuminate\Support\Facades\Storage;
|
||||
use Illuminate\Support\Facades\Validator;
|
||||
use Maatwebsite\Excel\Facades\Excel;
|
||||
|
||||
class Group extends Controller
|
||||
{
|
||||
public function __construct(
|
||||
public OsGroupRepositoryInterface $osGroupRepository
|
||||
) {}
|
||||
//
|
||||
public function index(Request $request)
|
||||
{
|
||||
$group = $this->osGroupRepository->filter(
|
||||
[
|
||||
'page' => $request->get('page') ?? 1
|
||||
],
|
||||
['host']
|
||||
);
|
||||
$title = 'List all group Os for all datastore';
|
||||
|
||||
return view('admin.package.group.index', compact('group', 'title'));
|
||||
}
|
||||
public function import(Request $request)
|
||||
{
|
||||
if (! $request->file) {
|
||||
return back()->withErrors("Please choose your file");
|
||||
}
|
||||
$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],
|
||||
'thumbnail' => $row[2],
|
||||
'module_host_id' => $row[3]
|
||||
];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (! isset($allData) || (isset($allData) && count($allData) < 1)) {
|
||||
return back()->withErrors('No data imported');
|
||||
}
|
||||
DB::beginTransaction();
|
||||
try {
|
||||
$this->osGroupRepository->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 Group OS';
|
||||
|
||||
return view('admin.package.group.add', compact('title'));
|
||||
}
|
||||
public function store(OsGroupRequest $request)
|
||||
{
|
||||
$request->validated();
|
||||
|
||||
DB::beginTransaction();
|
||||
try {
|
||||
$this->osGroupRepository->create($request->all());
|
||||
DB::commit();
|
||||
} catch (\Exception $error) {
|
||||
DB::rollBack();
|
||||
return back()->withErrors($error->getMessage());
|
||||
}
|
||||
return redirect()->route('package.group.index')->with('success', 'Add new Os Group successfully');
|
||||
}
|
||||
public function edit(int $id = 0)
|
||||
{
|
||||
|
||||
$group = $this->osGroupRepository->findWithRelationships($id, ['host.datacenter']);
|
||||
if (! $group) {
|
||||
return back()->withErrors('Can\'t find your group by id ' . $id);
|
||||
}
|
||||
$title = 'Edit Group OS';
|
||||
|
||||
return view('admin.package.group.edit', compact('title', 'group'));
|
||||
}
|
||||
public function update(int $id = 0, OsGroupRequest $request)
|
||||
{
|
||||
$group = $this->osGroupRepository->find($id);
|
||||
if (! $group) {
|
||||
return redirect()->route('package.group.index')->withErrors('Invalid Request');
|
||||
}
|
||||
$request->validated();
|
||||
DB::beginTransaction();
|
||||
try {
|
||||
$this->osGroupRepository->update($id, $request->all());
|
||||
DB::commit();
|
||||
} catch (\Exception $error) {
|
||||
DB::rollBack();
|
||||
return back()->withErrors($error->getMessage());
|
||||
}
|
||||
return redirect()->route('package.group.index')->with('success', 'Update successfully');
|
||||
}
|
||||
public function destroy(int $id = 0)
|
||||
{
|
||||
$group = $this->osGroupRepository->find($id);
|
||||
if (! $group) {
|
||||
return redirect()->route('package.group.index')->withErrors('Invalid Request');
|
||||
}
|
||||
DB::beginTransaction();
|
||||
try {
|
||||
$this->osGroupRepository->delete($id);
|
||||
DB::commit();
|
||||
} catch (Exception $error) {
|
||||
DB::rollBack();
|
||||
return back()->withErrors($error->getMessage());
|
||||
}
|
||||
|
||||
return redirect()->route('package.group.index')->with('success', 'Delete Group success');
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user