107 lines
3.2 KiB
PHP
107 lines
3.2 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers\Admin\Master;
|
|
|
|
use App\Enums\PriceType;
|
|
use App\Http\Controllers\Concerns\ParsesDataTableQuery;
|
|
use App\Http\Controllers\Controller;
|
|
use App\Http\Requests\Admin\Master\ProductRequest;
|
|
use App\Models\Product;
|
|
use App\Models\ProductVariant;
|
|
use App\Services\Master\ProductService;
|
|
use App\Support\Media\MediaPresenter;
|
|
use Illuminate\Http\RedirectResponse;
|
|
use Illuminate\Http\Request;
|
|
use Inertia\Inertia;
|
|
use Inertia\Response;
|
|
|
|
class ProductController extends Controller
|
|
{
|
|
use ParsesDataTableQuery;
|
|
|
|
public function __construct(
|
|
private readonly ProductService $productService,
|
|
) {}
|
|
|
|
public function index(Request $request): Response
|
|
{
|
|
$tableQuery = $this->parseDataTableQuery($request);
|
|
$isActive = $request->string('is_active')->toString();
|
|
|
|
return Inertia::render('admin/master/products/Index', [
|
|
'products' => $this->productService->paginateForIndex($tableQuery, $isActive),
|
|
'filters' => $this->dataTableFilters($tableQuery, [
|
|
'is_active' => $isActive,
|
|
]),
|
|
]);
|
|
}
|
|
|
|
public function create(): Response
|
|
{
|
|
return Inertia::render('admin/master/products/Create', [
|
|
'categories' => $this->productService->categoryOptions(),
|
|
'priceTypes' => PriceType::selectOptions(),
|
|
]);
|
|
}
|
|
|
|
public function store(ProductRequest $request): RedirectResponse
|
|
{
|
|
$this->productService->create($request->validated());
|
|
|
|
Inertia::flash('success', 'Produk berhasil ditambahkan.');
|
|
|
|
return redirect()->route('admin.master.products.index');
|
|
}
|
|
|
|
public function edit(Product $product): Response
|
|
{
|
|
$product->load([
|
|
'categories',
|
|
'variants' => fn ($query) => $query
|
|
->with(['prices' => fn ($query) => $query->orderBy('type'), 'media'])
|
|
->orderBy('created_at'),
|
|
]);
|
|
|
|
$product->variants->each(function (ProductVariant $variant): void {
|
|
$variant->setAttribute('images', MediaPresenter::collection($variant, 'images'));
|
|
});
|
|
|
|
return Inertia::render('admin/master/products/Edit', [
|
|
'categories' => $this->productService->categoryOptions(),
|
|
'priceTypes' => PriceType::selectOptions(),
|
|
'product' => $product,
|
|
]);
|
|
}
|
|
|
|
public function update(ProductRequest $request, Product $product): RedirectResponse
|
|
{
|
|
$this->productService->update($product, $request->validated());
|
|
|
|
Inertia::flash('success', 'Produk berhasil diperbarui.');
|
|
|
|
return redirect()->route('admin.master.products.index');
|
|
}
|
|
|
|
public function toggleStatus(Request $request, Product $product): RedirectResponse
|
|
{
|
|
$validated = $request->validate([
|
|
'is_active' => ['required', 'boolean'],
|
|
]);
|
|
|
|
$this->productService->toggleStatus($product, $validated['is_active']);
|
|
|
|
Inertia::flash('success', 'Status produk berhasil diperbarui.');
|
|
|
|
return back();
|
|
}
|
|
|
|
public function destroy(Product $product): RedirectResponse
|
|
{
|
|
$this->productService->delete($product);
|
|
|
|
Inertia::flash('success', 'Produk berhasil dihapus.');
|
|
|
|
return redirect()->route('admin.master.products.index');
|
|
}
|
|
}
|