productRepository->filter( [ 'page' => $request->get('page') ?? 1 ] ); $title = "List All Product"; return view('administrator.pages.product.index', compact('title', 'products')); } public function create(Request $request) { $step = $request->get('step') ?? 1; $title = 'Add new product'; $group = $this->groupRepository->getAll(); $type = $this->typeRepository->getAll(); if ($step === 1) { return view("administrator.pages.product.create", compact('group', 'type', 'title')); } if (!$request->has(['step', 'type'])) { return back()->withErrors('Missing step or type'); } $type_id = (int) $request->type; $product_name = $request->name; $view = "administrator.pages.product.create"; $data = compact('type_id', 'title', 'group', 'type', 'product_name'); return view($view, $data); } public function store(StoreProductRequest $request) { try { $this->productService->store($request->validated()); return redirect()->route('product.index')->with('success', 'Add new product success'); } catch (Exception $e) { return back()->withErrors($e->getMessage())->withInput(); } } public function edit(Request $request) { $product = $this->productRepository->find($request->product_id); if (!$product) { return back()->withErrors("Can't find product."); } $title = 'View product item'; $group = $this->groupRepository->getAll(); $type = $this->typeRepository->getAll(); $productType = ProductTypeEnums::tryFrom($product->product_type); $type_id = $this->mapEnumToTypeId($productType); $view = "administrator.pages.product.edit"; $data = compact('product', 'title', 'group', 'type', 'type_id'); return view($view, $data); } public function update(UpdateProductRequest $request) { try { $this->productService->update($request->product_id, $request->validated()); return redirect()->route('product.index')->with('success', 'Update product success'); } catch (Exception $e) { return back()->withErrors($e->getMessage())->withInput(); } } public function destroy(Request $request) { try { $this->productService->delete($request->product_id); return redirect()->route('product.index')->with('success', 'Deleted successfully'); } catch (Exception $e) { return back()->withErrors($e->getMessage()); } } public function datatable(Request $request) { $query = $this->productRepository->filter( $request->all(), [], [], ['*'], true ); return DataTables::eloquent($query) ->addColumn( 'action', function ($d) { return view('administrator.pages.product.datatable.action', ['row' => $d])->render(); } ) ->make(true); } private function mapEnumToTypeId(?ProductTypeEnums $enum): int { return match ($enum) { ProductTypeEnums::ADDON => 1, ProductTypeEnums::DOMAIN => 2, ProductTypeEnums::HOSTING => 3, ProductTypeEnums::VPS => 4, default => 0 }; } }