feat: Implement product and product category management with dedicated CRUD operations, services, and repositories.
This commit is contained in:
@@ -0,0 +1,149 @@
|
||||
<?php
|
||||
|
||||
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\Services\ProductService;
|
||||
use App\Repositories\Group\GroupRepositoryInterface;
|
||||
use App\Repositories\Admin\Product\ProductRepositoryInterface;
|
||||
use App\Repositories\Type\TypeRepositoryInterface;
|
||||
use Illuminate\Http\Request;
|
||||
use Yajra\DataTables\Facades\DataTables;
|
||||
use Exception;
|
||||
|
||||
class ProductController extends Controller
|
||||
{
|
||||
public function __construct(
|
||||
public TypeRepositoryInterface $typeRepository,
|
||||
public GroupRepositoryInterface $groupRepository,
|
||||
public ProductRepositoryInterface $productRepository,
|
||||
public ProductService $productService
|
||||
) {
|
||||
parent::__construct();
|
||||
}
|
||||
|
||||
public function index(Request $request)
|
||||
{
|
||||
$products = $this->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
|
||||
};
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user