feat: refactor ProductVariantController and requests to use scoped product handling and update route parameters
This commit is contained in:
@@ -12,80 +12,72 @@ use App\Domains\Tenant\Models\Tenant;
|
||||
use App\Http\Controllers\Controller;
|
||||
use Illuminate\Http\JsonResponse;
|
||||
use Illuminate\Http\Response;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
|
||||
|
||||
class ProductVariantController extends Controller
|
||||
{
|
||||
public function index(Tenant $tenant): JsonResponse
|
||||
public function index(Tenant $tenant, Product $producto): JsonResponse
|
||||
{
|
||||
|
||||
$query = ProductVariant::query()
|
||||
->whereHas('product', fn ($query) => $query->where('tenant_codigo', $tenant->codigo))
|
||||
->where('producto_id', $producto->id)
|
||||
->with(['product', 'definitions.attribute.options', 'attachments'])
|
||||
->latest();
|
||||
|
||||
return ProductVariantResource::collection($query->paginateFromRequest())->response();
|
||||
}
|
||||
|
||||
public function store(StoreProductVariantRequest $request, Tenant $tenant, ProductService $productService): JsonResponse
|
||||
public function store(StoreProductVariantRequest $request, Tenant $tenant, Product $producto, ProductService $productService): JsonResponse
|
||||
{
|
||||
$validated = $request->validated();
|
||||
$product = $this->resolveTenantProduct($tenant, (int) $validated['producto_id']);
|
||||
$producto = $this->resolveScopedProduct($tenant, $producto);
|
||||
|
||||
$variant = $productService->createVariant($product, $validated);
|
||||
$variant = $productService->createVariant($producto, $request->validated());
|
||||
|
||||
return ProductVariantResource::make($variant)->response()->setStatusCode(201);
|
||||
}
|
||||
|
||||
public function show(Tenant $tenant, ProductVariant $productVariant): ProductVariantResource
|
||||
public function show(Tenant $tenant, Product $producto, ProductVariant $productVariant): ProductVariantResource
|
||||
{
|
||||
$productVariant = $this->resolveScopedVariant($tenant, $productVariant);
|
||||
$producto = $this->resolveScopedProduct($tenant, $producto);
|
||||
$productVariant = $this->resolveScopedVariant($producto, $productVariant);
|
||||
|
||||
return ProductVariantResource::make($productVariant->load(['product', 'definitions.attribute.options', 'attachments']));
|
||||
}
|
||||
|
||||
public function update(UpdateProductVariantRequest $request, Tenant $tenant, ProductVariant $productVariant, ProductService $productService): ProductVariantResource
|
||||
public function update(UpdateProductVariantRequest $request, Tenant $tenant, Product $producto, ProductVariant $productVariant, ProductService $productService): ProductVariantResource
|
||||
{
|
||||
$productVariant = $this->resolveScopedVariant($tenant, $productVariant);
|
||||
$validated = $request->validated();
|
||||
$producto = $this->resolveScopedProduct($tenant, $producto);
|
||||
$productVariant = $this->resolveScopedVariant($producto, $productVariant);
|
||||
|
||||
$this->resolveTenantProduct($tenant, (int) $validated['producto_id']);
|
||||
|
||||
$productVariant = $productService->updateVariant($productVariant, $validated);
|
||||
$productVariant = $productService->updateVariant($productVariant, $request->validated());
|
||||
|
||||
return ProductVariantResource::make($productVariant);
|
||||
}
|
||||
|
||||
public function destroy(Tenant $tenant, ProductVariant $productVariant, ProductService $productService): Response
|
||||
public function destroy(Tenant $tenant, Product $producto, ProductVariant $productVariant, ProductService $productService): Response
|
||||
{
|
||||
$productVariant = $this->resolveScopedVariant($tenant, $productVariant);
|
||||
$producto = $this->resolveScopedProduct($tenant, $producto);
|
||||
$productVariant = $this->resolveScopedVariant($producto, $productVariant);
|
||||
$productService->deleteVariant($productVariant);
|
||||
|
||||
return response()->noContent();
|
||||
}
|
||||
|
||||
protected function resolveScopedVariant(Tenant $tenant, ProductVariant $variant): ProductVariant
|
||||
protected function resolveScopedProduct(Tenant $tenant, Product $product): Product
|
||||
{
|
||||
$variant->loadMissing('product');
|
||||
|
||||
if ($variant->product === null || $variant->product->tenant_codigo !== $tenant->codigo) {
|
||||
throw new NotFoundHttpException('Product variant not found for tenant.');
|
||||
}
|
||||
|
||||
return $variant;
|
||||
}
|
||||
|
||||
protected function resolveTenantProduct(Tenant $tenant, int $productId): Product
|
||||
{
|
||||
$product = Product::query()
|
||||
->whereKey($productId)
|
||||
->where('tenant_codigo', $tenant->codigo)
|
||||
->first();
|
||||
|
||||
if ($product === null) {
|
||||
if ($product->tenant_codigo !== $tenant->codigo) {
|
||||
throw new NotFoundHttpException('Product not found for tenant.');
|
||||
}
|
||||
|
||||
return $product;
|
||||
}
|
||||
|
||||
protected function resolveScopedVariant(Product $product, ProductVariant $variant): ProductVariant
|
||||
{
|
||||
if ($variant->producto_id !== $product->id) {
|
||||
throw new NotFoundHttpException('Product variant not found for product.');
|
||||
}
|
||||
|
||||
return $variant;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user