Files
drivetimeplaner/www/fahrschultermin.de/app/Repositories/TenantRepository.php
Hermes Agent 2d6c0ad3aa Split API and frontend, migrate SQLite to PostgreSQL
- api.fahrschultermin.de: full API entry point with CORS + session auth
- fahrschultermin.de: frontend only, no more /api/v1 routing
- Database.php: PostgreSQL support (pgsql driver + lastval())
- All repositories: use Database::lastInsertId() for PG compatibility
- config/app.php: PostgreSQL connection settings + CORS config
- bootstrap.php: pass full $config to Database::configure()
- public/index.php: fetch-patch to redirect /api/v1 to api.fahrschultermin.de
- database/schema_pgsql.sql: full PostgreSQL schema (BIGSERIAL)
- database/import_pgsql.sql: 518 rows migrated from SQLite
2026-05-18 23:50:00 +02:00

87 lines
3.3 KiB
PHP

<?php
declare(strict_types=1);
namespace App\Repositories;
use PDO;
final class TenantRepository extends BaseRepository
{
public function all(): array
{
return $this->db->query('SELECT * FROM tenants ORDER BY name')->fetchAll();
}
public function find(int $id): ?array
{
$statement = $this->db->prepare('SELECT * FROM tenants WHERE id = :id');
$statement->execute(['id' => $id]);
return $statement->fetch() ?: null;
}
public function create(array $data): array
{
$timestamp = $this->now();
$statement = $this->db->prepare(
'INSERT INTO tenants (name, timezone, federal_state, location_label, latitude, longitude, is_active, created_at, updated_at)
VALUES (:name, :timezone, :federal_state, :location_label, :latitude, :longitude, :is_active, :created_at, :updated_at)'
);
$statement->execute([
'name' => $data['name'],
'timezone' => $data['timezone'] ?? 'Europe/Berlin',
'federal_state' => $data['federal_state'] ?? 'BE',
'location_label' => $data['location_label'] ?? '',
'latitude' => $data['latitude'] !== '' ? $data['latitude'] : null,
'longitude' => $data['longitude'] !== '' ? $data['longitude'] : null,
'is_active' => !empty($data['is_active']) ? 1 : 0,
'created_at' => $timestamp,
'updated_at' => $timestamp,
]);
return $this->find((int) $this->lastInsertId());
}
public function update(int $id, array $data): ?array
{
$statement = $this->db->prepare(
'UPDATE tenants
SET name = :name, timezone = :timezone, federal_state = :federal_state, location_label = :location_label,
latitude = :latitude, longitude = :longitude, is_active = :is_active, updated_at = :updated_at
WHERE id = :id'
);
$statement->execute([
'id' => $id,
'name' => $data['name'],
'timezone' => $data['timezone'],
'federal_state' => $data['federal_state'] ?? 'BE',
'location_label' => $data['location_label'] ?? '',
'latitude' => $data['latitude'] !== '' ? $data['latitude'] : null,
'longitude' => $data['longitude'] !== '' ? $data['longitude'] : null,
'is_active' => !empty($data['is_active']) ? 1 : 0,
'updated_at' => $this->now(),
]);
return $this->find($id);
}
public function updateForTenantAdmin(int $id, array $data): ?array
{
$current = $this->find($id);
if ($current === null) {
return null;
}
return $this->update($id, [
'name' => $data['name'] ?? $current['name'],
'timezone' => $data['timezone'] ?? $current['timezone'],
'federal_state' => $data['federal_state'] ?? ($current['federal_state'] ?? 'BE'),
'location_label' => $data['location_label'] ?? ($current['location_label'] ?? ''),
'latitude' => array_key_exists('latitude', $data) ? $data['latitude'] : ($current['latitude'] ?? null),
'longitude' => array_key_exists('longitude', $data) ? $data['longitude'] : ($current['longitude'] ?? null),
'is_active' => $current['is_active'],
]);
}
}