LMIV-SNACKAUTOMAT/app/Http/Controllers/ProductController.php

143 lines
4.3 KiB
PHP

<?php
namespace App\Http\Controllers;
use App\Models\Product;
use App\Http\Requests\ProductRequest;
use App\Http\Controllers\Concerns\HasTenantSecurity;
use Illuminate\Http\Request;
use Illuminate\View\View;
use Illuminate\Http\RedirectResponse;
class ProductController extends Controller
{
use HasTenantSecurity;
/**
* Display a listing of the resource.
*/
public function index(): View
{
$this->ensureCorrectTenantSession();
$tenantId = $this->getCurrentTenantId();
$query = Product::query();
if ($tenantId) {
$query->where('tenant_id', $tenantId);
}
$products = $query->paginate(15);
return view('admin.products.index', compact('products'));
}
/**
* Show the form for creating a new resource.
*/
public function create(): View
{
$this->ensureCorrectTenantSession();
return view('admin.products.create');
}
/**
* Store a newly created resource in storage.
*/
public function store(ProductRequest $request): RedirectResponse
{
$this->ensureCorrectTenantSession();
$tenantId = $this->getCurrentTenantId();
if (!$tenantId) {
return redirect()->back()->with('error', 'Kein Mandant ausgewählt.');
}
$validated = $request->validated();
if ($request->hasFile('image')) {
$image = $request->file('image');
$extension = $image->getClientOriginalExtension();
// Stelle sicher, dass WebP-Dateien korrekt behandelt werden
if (strtolower($extension) === 'webp' || $image->getMimeType() === 'image/webp') {
$fileName = time() . '_' . uniqid() . '.webp';
$imagePath = $image->storeAs('products', $fileName, 'public');
} else {
$imagePath = $image->store('products', 'public');
}
$validated['image'] = $imagePath;
}
// Setze automatisch die tenant_id
$validated['tenant_id'] = $tenantId;
Product::create($validated);
return redirect()->route('products.index')->with('success', 'Produkt erfolgreich erstellt.');
}
/**
* Display the specified resource.
*/
public function show(Product $product): View
{
$this->ensureCorrectTenantSession();
$this->ensureTenantAccess($product->tenant_id);
return view('admin.products.show', compact('product'));
}
/**
* Show the form for editing the specified resource.
*/
public function edit(Product $product): View
{
$this->ensureCorrectTenantSession();
$this->ensureTenantAccess($product->tenant_id);
return view('admin.products.edit', compact('product'));
}
/**
* Update the specified resource in storage.
*/
public function update(ProductRequest $request, Product $product): RedirectResponse
{
$this->ensureCorrectTenantSession();
$this->ensureTenantAccess($product->tenant_id);
$validated = $request->validated();
if ($request->hasFile('image')) {
$image = $request->file('image');
$extension = $image->getClientOriginalExtension();
// Stelle sicher, dass WebP-Dateien korrekt behandelt werden
if (strtolower($extension) === 'webp' || $image->getMimeType() === 'image/webp') {
$fileName = time() . '_' . uniqid() . '.webp';
$imagePath = $image->storeAs('products', $fileName, 'public');
} else {
$imagePath = $image->store('products', 'public');
}
$validated['image'] = $imagePath;
}
$product->update($validated);
return redirect()->route('products.index')->with('success', 'Produkt erfolgreich aktualisiert.');
}
/**
* Remove the specified resource from storage.
*/
public function destroy(Product $product): RedirectResponse
{
$this->ensureCorrectTenantSession();
$this->ensureTenantAccess($product->tenant_id);
$product->delete();
return redirect()->route('products.index')->with('success', 'Produkt erfolgreich gelöscht.');
}
}