update
This commit is contained in:
@@ -4,8 +4,7 @@ namespace App\Http\Controllers\admin\product;
|
||||
|
||||
use App\Enums\Product\ProductTypeEnums;
|
||||
use App\Http\Controllers\Controller;
|
||||
use App\Http\Requests\Product\StoreProductRequest;
|
||||
use App\Http\Requests\Product\UpdateProductRequest;
|
||||
use App\Http\Requests\Product\ProductRequest;
|
||||
use App\Http\Services\ProductService;
|
||||
use App\Repositories\Group\GroupRepositoryInterface;
|
||||
use App\Repositories\Admin\Product\ProductRepositoryInterface;
|
||||
@@ -42,11 +41,15 @@ class ProductController extends Controller
|
||||
{
|
||||
$step = $request->get('step') ?? 1;
|
||||
$title = 'Add new product';
|
||||
$group = $this->groupRepository->getAll();
|
||||
// Fetch Product Categories instead of generic groups
|
||||
$group = \App\Models\ProductCategories::where('is_active', \App\Enums\Status::ACTIVE->value)->get();
|
||||
$type = $this->typeRepository->getAll();
|
||||
|
||||
|
||||
// Fetch OS versions with their group for the OS selector component
|
||||
$osVersions = \App\Models\Os::with('osGroup')->where('status', 1)->get();
|
||||
|
||||
if ($step === 1) {
|
||||
return view("administrator.pages.product.create", compact('group', 'type', 'title'));
|
||||
return view("administrator.pages.product.create", compact('group', 'type', 'title', 'osVersions'));
|
||||
}
|
||||
|
||||
if (!$request->has(['step', 'type'])) {
|
||||
@@ -58,59 +61,62 @@ class ProductController extends Controller
|
||||
|
||||
$view = "administrator.pages.product.create";
|
||||
|
||||
$data = compact('type_id', 'title', 'group', 'type', 'product_name');
|
||||
$data = compact('type_id', 'title', 'group', 'type', 'product_name', 'osOptions');
|
||||
|
||||
return view($view, $data);
|
||||
}
|
||||
|
||||
public function store(StoreProductRequest $request)
|
||||
public function store(ProductRequest $request)
|
||||
{
|
||||
try {
|
||||
$this->productService->store($request->validated());
|
||||
|
||||
return redirect()->route('product.index')->with('success', 'Add new product success');
|
||||
return redirect()->route('products.index')->with('success', 'Add new product success');
|
||||
} catch (Exception $e) {
|
||||
return back()->withErrors($e->getMessage())->withInput();
|
||||
}
|
||||
}
|
||||
|
||||
public function edit(Request $request)
|
||||
public function edit(Request $request, $id)
|
||||
{
|
||||
$product = $this->productRepository->find($request->product_id);
|
||||
$product = $this->productRepository->find($id);
|
||||
|
||||
if (!$product) {
|
||||
return back()->withErrors("Can't find product.");
|
||||
}
|
||||
|
||||
$title = 'View product item';
|
||||
$group = $this->groupRepository->getAll();
|
||||
// Load relationships
|
||||
$product->load(['pricings', 'osVersions', 'category']);
|
||||
|
||||
$title = 'Edit product: ' . $product->name;
|
||||
$group = \App\Models\ProductCategories::where('is_active', \App\Enums\Status::ACTIVE->value)->get();
|
||||
$type = $this->typeRepository->getAll();
|
||||
|
||||
$productType = ProductTypeEnums::tryFrom($product->product_type);
|
||||
$type_id = $this->mapEnumToTypeId($productType);
|
||||
// Fetch OS versions for select
|
||||
$osVersions = \App\Models\Os::with('osGroup')->where('status', 1)->get();
|
||||
|
||||
$view = "administrator.pages.product.edit";
|
||||
|
||||
$data = compact('product', 'title', 'group', 'type', 'type_id');
|
||||
$data = compact('product', 'title', 'group', 'type', 'osVersions');
|
||||
|
||||
return view($view, $data);
|
||||
}
|
||||
|
||||
public function update(UpdateProductRequest $request)
|
||||
public function update(ProductRequest $request, $id)
|
||||
{
|
||||
try {
|
||||
$this->productService->update($request->product_id, $request->validated());
|
||||
return redirect()->route('product.index')->with('success', 'Update product success');
|
||||
$this->productService->update((int) $id, $request->validated());
|
||||
return redirect()->route('products.index')->with('success', 'Update product success');
|
||||
} catch (Exception $e) {
|
||||
return back()->withErrors($e->getMessage())->withInput();
|
||||
}
|
||||
}
|
||||
|
||||
public function destroy(Request $request)
|
||||
public function destroy(Request $request, $id)
|
||||
{
|
||||
try {
|
||||
$this->productService->delete($request->product_id);
|
||||
return redirect()->route('product.index')->with('success', 'Deleted successfully');
|
||||
$this->productService->delete((int) $id);
|
||||
return redirect()->route('products.index')->with('success', 'Deleted successfully');
|
||||
} catch (Exception $e) {
|
||||
return back()->withErrors($e->getMessage());
|
||||
}
|
||||
@@ -120,19 +126,33 @@ class ProductController extends Controller
|
||||
{
|
||||
$query = $this->productRepository->filter(
|
||||
$request->all(),
|
||||
[],
|
||||
['category'],
|
||||
[],
|
||||
['*'],
|
||||
true
|
||||
);
|
||||
|
||||
return DataTables::eloquent($query)
|
||||
->addColumn('category_name', function ($d) {
|
||||
return $d->category?->name ?? '-';
|
||||
})
|
||||
->addColumn('product_type_label', function ($d) {
|
||||
$type = ProductTypeEnums::tryFrom($d->product_type);
|
||||
return $type ? $type->label() : '-';
|
||||
})
|
||||
->addColumn('specs', function ($d) {
|
||||
return view('administrator.pages.product.datatable.specs', ['row' => $d])->render();
|
||||
})
|
||||
->addColumn('status', function ($d) {
|
||||
return view('administrator.pages.product.datatable.status', ['row' => $d])->render();
|
||||
})
|
||||
->addColumn(
|
||||
'action',
|
||||
function ($d) {
|
||||
return view('administrator.pages.product.datatable.action', ['row' => $d])->render();
|
||||
}
|
||||
)
|
||||
->rawColumns(['specs', 'status', 'action'])
|
||||
->make(true);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user