BrandController::modelsForBrand() (used by /fahrzeuge/{brand}) never
selected image_path from car_models, so an image set on a model never
reached the public page regardless of the frontend rendering added
earlier - the API response simply didn't include it. index()'s equivalent
query for /brands already had the column; this one was missed.
Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
135 lines
4.3 KiB
PHP
135 lines
4.3 KiB
PHP
<?php
|
|
|
|
namespace App\Controllers;
|
|
|
|
use App\Config\Database;
|
|
use App\Support\Http;
|
|
use App\Support\Slug;
|
|
use App\Support\TextStyleCleanup;
|
|
use App\Support\Uploads;
|
|
|
|
class BrandController
|
|
{
|
|
public static function index(): void
|
|
{
|
|
$stmt = Database::connection()->query(
|
|
'SELECT id, name, slug, image_path, sort_order FROM brands ORDER BY sort_order, name'
|
|
);
|
|
Http::send($stmt->fetchAll());
|
|
}
|
|
|
|
public static function store(): void
|
|
{
|
|
$body = Http::jsonBody();
|
|
$name = trim($body['name'] ?? '');
|
|
if ($name === '') {
|
|
Http::error('Name erforderlich', 422);
|
|
}
|
|
|
|
$slug = Slug::make($name);
|
|
$db = Database::connection();
|
|
$stmt = $db->prepare(
|
|
'INSERT INTO brands (name, slug, image_path, sort_order) VALUES (?, ?, ?, ?)'
|
|
);
|
|
$stmt->execute([
|
|
$name,
|
|
$slug,
|
|
trim($body['image_path'] ?? '') ?: null,
|
|
(int) ($body['sort_order'] ?? 0),
|
|
]);
|
|
|
|
Http::send(['id' => (int) $db->lastInsertId(), 'name' => $name, 'slug' => $slug], 201);
|
|
}
|
|
|
|
public static function update(array $params): void
|
|
{
|
|
$id = (int) $params['id'];
|
|
$body = Http::jsonBody();
|
|
$name = trim($body['name'] ?? '');
|
|
if ($name === '') {
|
|
Http::error('Name erforderlich', 422);
|
|
}
|
|
|
|
$newImagePath = trim($body['image_path'] ?? '') ?: null;
|
|
$db = Database::connection();
|
|
$oldStmt = $db->prepare('SELECT image_path FROM brands WHERE id = ?');
|
|
$oldStmt->execute([$id]);
|
|
$oldImagePath = $oldStmt->fetchColumn();
|
|
|
|
$stmt = $db->prepare(
|
|
'UPDATE brands SET name = ?, slug = ?, image_path = ?, sort_order = ? WHERE id = ?'
|
|
);
|
|
$stmt->execute([
|
|
$name,
|
|
Slug::make($name),
|
|
$newImagePath,
|
|
(int) ($body['sort_order'] ?? 0),
|
|
$id,
|
|
]);
|
|
|
|
Uploads::deleteIfOrphaned($oldImagePath ?: null, $newImagePath);
|
|
|
|
Http::send(['ok' => true]);
|
|
}
|
|
|
|
public static function destroy(array $params): void
|
|
{
|
|
$id = (int) $params['id'];
|
|
$db = Database::connection();
|
|
|
|
$oldStmt = $db->prepare('SELECT image_path FROM brands WHERE id = ?');
|
|
$oldStmt->execute([$id]);
|
|
$oldImagePath = $oldStmt->fetchColumn();
|
|
|
|
// ON DELETE CASCADE raeumt car_models (und darueber packages) in der DB mit auf - deren
|
|
// Bilddateien und text_styles-Zeilen vorher einsammeln, danach sind die Zeilen weg.
|
|
$modelsStmt = $db->prepare('SELECT id, image_path FROM car_models WHERE brand_id = ?');
|
|
$modelsStmt->execute([$id]);
|
|
$models = $modelsStmt->fetchAll();
|
|
$modelIds = array_column($models, 'id');
|
|
|
|
$packageIds = [];
|
|
if ($modelIds) {
|
|
$placeholders = implode(',', array_fill(0, count($modelIds), '?'));
|
|
$packagesStmt = $db->prepare("SELECT id FROM packages WHERE car_model_id IN ($placeholders)");
|
|
$packagesStmt->execute($modelIds);
|
|
$packageIds = $packagesStmt->fetchAll(\PDO::FETCH_COLUMN);
|
|
}
|
|
|
|
$stmt = $db->prepare('DELETE FROM brands WHERE id = ?');
|
|
$stmt->execute([$id]);
|
|
|
|
foreach ([$oldImagePath, ...array_column($models, 'image_path')] as $path) {
|
|
Uploads::deleteIfOrphaned($path ?: null, null);
|
|
}
|
|
|
|
TextStyleCleanup::forgetEntity('brands', (string) $id);
|
|
foreach ($modelIds as $modelId) {
|
|
TextStyleCleanup::forgetEntity('car_models', (string) $modelId);
|
|
}
|
|
foreach ($packageIds as $packageId) {
|
|
TextStyleCleanup::forgetEntity('packages', (string) $packageId);
|
|
}
|
|
|
|
Http::send(['ok' => true]);
|
|
}
|
|
|
|
public static function modelsForBrand(array $params): void
|
|
{
|
|
$db = Database::connection();
|
|
$brandStmt = $db->prepare('SELECT id, name, slug FROM brands WHERE slug = ?');
|
|
$brandStmt->execute([$params['slug']]);
|
|
$brand = $brandStmt->fetch();
|
|
|
|
if (!$brand) {
|
|
Http::error('Marke nicht gefunden', 404);
|
|
}
|
|
|
|
$stmt = $db->prepare(
|
|
'SELECT id, name, slug, image_path, sort_order FROM car_models WHERE brand_id = ? ORDER BY sort_order, name'
|
|
);
|
|
$stmt->execute([$brand['id']]);
|
|
|
|
Http::send(['brand' => $brand, 'models' => $stmt->fetchAll()]);
|
|
}
|
|
}
|