feat: Implement product and product category management with dedicated CRUD operations, services, and repositories.
This commit is contained in:
@@ -0,0 +1,88 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers\admin\product;
|
||||
|
||||
use App\Http\Controllers\Controller;
|
||||
use App\Http\Requests\ProductCategories\ProductCategoryRequest;
|
||||
use App\Http\Services\Admin\ProductCategory\ProductCategoryServiceInterface;
|
||||
use App\Repositories\Admin\ProductCategory\ProductCategoryRepositoryInterface;
|
||||
use Illuminate\Http\Request;
|
||||
use Yajra\DataTables\Facades\DataTables;
|
||||
|
||||
|
||||
class ProductCategoryController extends Controller
|
||||
{
|
||||
public function __construct(
|
||||
public ProductCategoryRepositoryInterface $productCategoryRepository,
|
||||
public ProductCategoryServiceInterface $productCategoryService
|
||||
) {
|
||||
parent::__construct();
|
||||
}
|
||||
|
||||
|
||||
public function index()
|
||||
{
|
||||
return view('administrator.pages.product.categories.index');
|
||||
}
|
||||
|
||||
public function create()
|
||||
{
|
||||
$categories = \App\Models\ProductCategories::all();
|
||||
return view('administrator.pages.product.categories.create', compact('categories'));
|
||||
}
|
||||
|
||||
public function store(ProductCategoryRequest $request)
|
||||
{
|
||||
$category = $this->productCategoryService->store($request->validated());
|
||||
return redirect()->route('products.group.edit', ['id' => $category->id])->with('success', 'Create Group Success');
|
||||
}
|
||||
|
||||
public function edit(int $id)
|
||||
{
|
||||
$category = $this->productCategoryRepository->find($id);
|
||||
if (!$category) {
|
||||
return redirect()->route('products.group.index')->with('error', 'Category not found');
|
||||
}
|
||||
$categories = \App\Models\ProductCategories::where('id', '!=', $id)->get();
|
||||
return view('administrator.pages.product.categories.edit', compact('category', 'categories'));
|
||||
}
|
||||
|
||||
public function update(ProductCategoryRequest $request, int $id)
|
||||
{
|
||||
$this->productCategoryService->update($id, $request->validated());
|
||||
return redirect()->route('products.group.edit', ['id' => $id])->with('success', 'Update Group Success');
|
||||
}
|
||||
|
||||
public function destroy(int $id)
|
||||
{
|
||||
$this->productCategoryService->delete($id);
|
||||
return response()->json(['success' => true]);
|
||||
}
|
||||
|
||||
public function datatable(Request $request)
|
||||
{
|
||||
$query = $this->productCategoryRepository->filter(
|
||||
$request->all(),
|
||||
[],
|
||||
[],
|
||||
['*'],
|
||||
true
|
||||
);
|
||||
|
||||
return DataTables::eloquent($query)
|
||||
->addColumn(
|
||||
'action',
|
||||
function ($d) {
|
||||
return view('administrator.pages.product.categories.datatable.action', ['row' => $d])->render();
|
||||
}
|
||||
)
|
||||
->addColumn(
|
||||
'is_active',
|
||||
function ($d) {
|
||||
return view('administrator.pages.product.categories.datatable.status', ['row' => $d])->render();
|
||||
}
|
||||
)
|
||||
->rawColumns(['action', 'is_active'])
|
||||
->make(true);
|
||||
}
|
||||
}
|
||||
@@ -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