Files
drivetimeplaner/www/fahrschultermin.de/app/Repositories/InstructorRepository.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

83 lines
3.1 KiB
PHP

<?php
declare(strict_types=1);
namespace App\Repositories;
final class InstructorRepository extends BaseRepository
{
public function allForTenant(int $tenantId): array
{
$statement = $this->db->prepare('SELECT * FROM instructors WHERE tenant_id = :tenant_id ORDER BY last_name, first_name');
$statement->execute(['tenant_id' => $tenantId]);
return $statement->fetchAll();
}
public function findByUserId(int $tenantId, int $userId): ?array
{
$statement = $this->db->prepare(
'SELECT * FROM instructors WHERE tenant_id = :tenant_id AND user_id = :user_id LIMIT 1'
);
$statement->execute(['tenant_id' => $tenantId, 'user_id' => $userId]);
return $statement->fetch() ?: null;
}
public function create(int $tenantId, array $data): array
{
$timestamp = $this->now();
$statement = $this->db->prepare(
'INSERT INTO instructors
(tenant_id, user_id, first_name, last_name, color, notes, is_active, pre_start_buffer_minutes, created_at, updated_at)
VALUES (:tenant_id, :user_id, :first_name, :last_name, :color, :notes, :is_active, :pre_start_buffer_minutes, :created_at, :updated_at)'
);
$statement->execute([
'tenant_id' => $tenantId,
'user_id' => $data['user_id'] ?? null,
'first_name' => $data['first_name'],
'last_name' => $data['last_name'],
'color' => $data['color'],
'notes' => $data['notes'] ?? '',
'is_active' => !empty($data['is_active']) ? 1 : 0,
'pre_start_buffer_minutes' => max(0, (int) ($data['pre_start_buffer_minutes'] ?? 0)),
'created_at' => $timestamp,
'updated_at' => $timestamp,
]);
return $this->find($tenantId, (int) $this->lastInsertId());
}
public function update(int $tenantId, int $id, array $data): ?array
{
$statement = $this->db->prepare(
'UPDATE instructors
SET user_id = :user_id, first_name = :first_name, last_name = :last_name, color = :color,
notes = :notes, is_active = :is_active, pre_start_buffer_minutes = :pre_start_buffer_minutes, updated_at = :updated_at
WHERE tenant_id = :tenant_id AND id = :id'
);
$statement->execute([
'tenant_id' => $tenantId,
'id' => $id,
'user_id' => $data['user_id'] ?? null,
'first_name' => $data['first_name'],
'last_name' => $data['last_name'],
'color' => $data['color'],
'notes' => $data['notes'] ?? '',
'is_active' => !empty($data['is_active']) ? 1 : 0,
'pre_start_buffer_minutes' => max(0, (int) ($data['pre_start_buffer_minutes'] ?? 0)),
'updated_at' => $this->now(),
]);
return $this->find($tenantId, $id);
}
public function find(int $tenantId, int $id): ?array
{
$statement = $this->db->prepare('SELECT * FROM instructors WHERE tenant_id = :tenant_id AND id = :id');
$statement->execute(['tenant_id' => $tenantId, 'id' => $id]);
return $statement->fetch() ?: null;
}
}