Update source into github
This commit is contained in:
@@ -0,0 +1,132 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers\admin;
|
||||
|
||||
use App\Http\Controllers\Controller;
|
||||
use App\Http\Requests\MenuRequest;
|
||||
use App\Repositories\Menu\MenuRepositoryInterface;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
|
||||
class MenuController extends Controller
|
||||
{
|
||||
public function __construct(
|
||||
public MenuRepositoryInterface $menuRepository
|
||||
)
|
||||
{
|
||||
}
|
||||
|
||||
public function buildMenuOptions($menus, $prefix = '')
|
||||
{
|
||||
$result = [];
|
||||
|
||||
foreach ($menus as $menu) {
|
||||
$result[] = [
|
||||
'id' => $menu->id,
|
||||
'title' => $prefix . ' ' . $menu->title,
|
||||
'route_name' => $menu->route_name,
|
||||
'is_active' => $menu->is_active,
|
||||
'created_at' => $menu->created_at,
|
||||
];
|
||||
|
||||
if ($menu->children && $menu->children->count() > 0) {
|
||||
$result = array_merge($result, $this->buildMenuOptions($menu->children, $prefix . '--'));
|
||||
}
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
public function index(Request $request)
|
||||
{
|
||||
$menuOptions = $this->buildMenuOptions(
|
||||
$this->menuRepository->getTree()
|
||||
);
|
||||
$title = 'List Menu';
|
||||
|
||||
return view('admin.menu.index', compact('menuOptions', 'title'));
|
||||
}
|
||||
|
||||
public function create()
|
||||
{
|
||||
$title = 'Add new Menu';
|
||||
$menus = $this->menuRepository->getTree();
|
||||
|
||||
$parentMenuOptions = $this->buildMenuOptions($menus);
|
||||
return view('admin.menu.add', compact('title', 'parentMenuOptions'));
|
||||
}
|
||||
|
||||
public function store(MenuRequest $request)
|
||||
{
|
||||
$data = $request->validated();
|
||||
|
||||
DB::beginTransaction();
|
||||
try {
|
||||
$this->menuRepository->store($data);
|
||||
|
||||
DB::commit();
|
||||
} catch (\Exception $exception) {
|
||||
DB::rollBack();
|
||||
return back()->withErrors($exception->getMessage());
|
||||
}
|
||||
|
||||
return redirect()->route('menu.index')->with('success', 'Added successfully');
|
||||
}
|
||||
|
||||
public function edit($id)
|
||||
{
|
||||
$menu = $this->menuRepository->find($id);
|
||||
|
||||
if (!$menu) {
|
||||
return back()->withErrors('Invalid request');
|
||||
}
|
||||
|
||||
$menus = $this->menuRepository->getTree();
|
||||
|
||||
$parentMenuOptions = $this->buildMenuOptions($menus);
|
||||
|
||||
$title = 'Edit Menu';
|
||||
|
||||
return view('admin.menu.edit', compact('title', 'menu', 'parentMenuOptions'));
|
||||
}
|
||||
|
||||
public function update(int $id = 0, MenuRequest $request)
|
||||
{
|
||||
$menu = $this->menuRepository->find($id);
|
||||
|
||||
if (!$menu) {
|
||||
return back()->withErrors('Invalid request');
|
||||
}
|
||||
$request->validated();
|
||||
|
||||
|
||||
DB::beginTransaction();
|
||||
try {
|
||||
$this->menuRepository->update($id, $request->all());
|
||||
DB::commit();
|
||||
} catch (\Exception $exception) {
|
||||
DB::rollBack();
|
||||
return back()->withErrors($exception->getMessage());
|
||||
}
|
||||
return redirect()->route('menu.index')->with('success', 'Updated successfully');
|
||||
}
|
||||
|
||||
public function destroy(int $id = 0)
|
||||
{
|
||||
$menu = $this->menuRepository->find($id);
|
||||
|
||||
if (!$menu) {
|
||||
return back()->withErrors('Invalid request');
|
||||
}
|
||||
|
||||
DB::beginTransaction();
|
||||
try {
|
||||
$this->menuRepository->delete($id);
|
||||
DB::commit();
|
||||
} catch (\Exception $exception) {
|
||||
DB::rollBack();
|
||||
return back()->withErrors($exception->getMessage());
|
||||
}
|
||||
return redirect()->route('menu.index')->with('success', 'Deleted successfully');
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user