- Updated RoleIndex component to handle pagination, sorting, and searching for roles. - Adjusted data structure for roles to include pagination details. - Enhanced tests for various admin features (Finance, HR, Master) to validate pagination and data structure. - Ensured all relevant tests check for data structure consistency, including total counts and pagination details.
89 lines
2.7 KiB
PHP
89 lines
2.7 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers\Admin\Master;
|
|
|
|
use App\Enums\ProductStatus;
|
|
use App\Http\Controllers\Controller;
|
|
use App\Http\Requests\PaginatedRequest;
|
|
use App\Http\Requests\Admin\Master\ProductRequest;
|
|
use App\Models\Product;
|
|
use App\Services\Admin\Master\CategoryService;
|
|
use App\Services\Admin\Master\ProductService;
|
|
use Illuminate\Http\RedirectResponse;
|
|
use Inertia\Inertia;
|
|
use Inertia\Response;
|
|
|
|
class ProductController extends Controller
|
|
{
|
|
public function __construct(
|
|
private ProductService $service,
|
|
private CategoryService $categoryService,
|
|
) {}
|
|
|
|
public function index(PaginatedRequest $request): Response
|
|
{
|
|
return Inertia::render('admin/master/product/index', [
|
|
'products' => $this->service->paginated(
|
|
...$request->validatedWithDefaults(),
|
|
filters: $request->only(['status']),
|
|
),
|
|
'filters' => $request->only(['status']),
|
|
]);
|
|
}
|
|
|
|
public function create(): Response
|
|
{
|
|
return Inertia::render('admin/master/product/create', [
|
|
'categories' => $this->categoryService->getAll(),
|
|
]);
|
|
}
|
|
|
|
public function store(ProductRequest $request): RedirectResponse
|
|
{
|
|
return $this->handleAction(
|
|
fn() => $this->service->create($request->validated()),
|
|
'Produk berhasil ditambahkan.',
|
|
'admin.master.products.index',
|
|
'admin.master.products.create'
|
|
);
|
|
}
|
|
|
|
public function edit(Product $product): Response
|
|
{
|
|
return Inertia::render('admin/master/product/edit', [
|
|
'product' => $this->service->getForEdit($product),
|
|
'categories' => $this->categoryService->getAll(),
|
|
]);
|
|
}
|
|
|
|
public function update(ProductRequest $request, Product $product): RedirectResponse
|
|
{
|
|
return $this->handleAction(
|
|
fn() => $this->service->update($product, $request->validated()),
|
|
'Produk berhasil diperbarui.',
|
|
'admin.master.products.index',
|
|
'admin.master.products.edit',
|
|
['product' => $product]
|
|
);
|
|
}
|
|
|
|
public function destroy(Product $product): RedirectResponse
|
|
{
|
|
return $this->handleAction(
|
|
fn() => $this->service->delete($product),
|
|
'Produk berhasil dihapus.',
|
|
'admin.master.products.index'
|
|
);
|
|
}
|
|
|
|
public function toggleStatus(Product $product): RedirectResponse
|
|
{
|
|
$this->service->toggleStatus($product);
|
|
$status = $product->fresh()->status;
|
|
|
|
Inertia::flash('toast', ['type' => 'success', 'message' => "Status produk berhasil diubah menjadi {$status->label()}."]);
|
|
|
|
return to_route('admin.master.products.index');
|
|
}
|
|
}
|