Phase 1: API + Frontend Skelett
This commit is contained in:
104
www/api.fahrschuldesk.de/app/Support/Database.php
Normal file
104
www/api.fahrschuldesk.de/app/Support/Database.php
Normal file
@@ -0,0 +1,104 @@
|
||||
<?php
|
||||
/**
|
||||
* Database Support Class
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Support;
|
||||
|
||||
use PDO;
|
||||
use PDOStatement;
|
||||
|
||||
class Database
|
||||
{
|
||||
private static ?PDO $connection = null;
|
||||
|
||||
public static function setConnection(PDO $pdo): void
|
||||
{
|
||||
self::$connection = $pdo;
|
||||
}
|
||||
|
||||
public static function getConnection(): PDO
|
||||
{
|
||||
if (self::$connection === null) {
|
||||
throw new \RuntimeException('Database connection not initialized');
|
||||
}
|
||||
return self::$connection;
|
||||
}
|
||||
|
||||
public static function fetch(string $sql, array $params = []): ?array
|
||||
{
|
||||
$stmt = self::getConnection()->prepare($sql);
|
||||
$stmt->execute($params);
|
||||
$result = $stmt->fetch();
|
||||
return $result ?: null;
|
||||
}
|
||||
|
||||
public static function fetchAll(string $sql, array $params = []): array
|
||||
{
|
||||
$stmt = self::getConnection()->prepare($sql);
|
||||
$stmt->execute($params);
|
||||
return $stmt->fetchAll();
|
||||
}
|
||||
|
||||
public static function insert(string $table, array $data): string
|
||||
{
|
||||
$columns = implode(', ', array_keys($data));
|
||||
$placeholders = implode(', ', array_fill(0, count($data), '?'));
|
||||
|
||||
$sql = "INSERT INTO {$table} ({$columns}) VALUES ({$placeholders}) RETURNING id";
|
||||
$stmt = self::getConnection()->prepare($sql);
|
||||
$stmt->execute(array_values($data));
|
||||
|
||||
$result = $stmt->fetch();
|
||||
return $result['id'] ?? '';
|
||||
}
|
||||
|
||||
public static function update(string $table, array $data, string $where, array $whereParams = []): int
|
||||
{
|
||||
$set = implode(' = ?, ', array_keys($data)) . ' = ?';
|
||||
$sql = "UPDATE {$table} SET {$set} WHERE {$where}";
|
||||
|
||||
$stmt = self::getConnection()->prepare($sql);
|
||||
$stmt->execute([...array_values($data), ...$whereParams]);
|
||||
|
||||
return $stmt->rowCount();
|
||||
}
|
||||
|
||||
public static function delete(string $table, string $where, array $whereParams = []): int
|
||||
{
|
||||
$sql = "DELETE FROM {$table} WHERE {$where}";
|
||||
$stmt = self::getConnection()->prepare($sql);
|
||||
$stmt->execute($whereParams);
|
||||
|
||||
return $stmt->rowCount();
|
||||
}
|
||||
|
||||
public static function query(string $sql, array $params = []): PDOStatement
|
||||
{
|
||||
$stmt = self::getConnection()->prepare($sql);
|
||||
$stmt->execute($params);
|
||||
return $stmt;
|
||||
}
|
||||
|
||||
public static function beginTransaction(): bool
|
||||
{
|
||||
return self::getConnection()->beginTransaction();
|
||||
}
|
||||
|
||||
public static function commit(): bool
|
||||
{
|
||||
return self::getConnection()->commit();
|
||||
}
|
||||
|
||||
public static function rollback(): bool
|
||||
{
|
||||
return self::getConnection()->rollBack();
|
||||
}
|
||||
|
||||
public static function lastInsertId(): string
|
||||
{
|
||||
return self::getConnection()->lastInsertId();
|
||||
}
|
||||
}
|
||||
77
www/api.fahrschuldesk.de/app/Support/Response.php
Normal file
77
www/api.fahrschuldesk.de/app/Support/Response.php
Normal file
@@ -0,0 +1,77 @@
|
||||
<?php
|
||||
/**
|
||||
* JSON Response Helper
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Support;
|
||||
|
||||
use Psr\Http\Message\ResponseInterface;
|
||||
|
||||
class Response
|
||||
{
|
||||
public static function json(
|
||||
ResponseInterface $response,
|
||||
mixed $data,
|
||||
int $status = 200
|
||||
): ResponseInterface {
|
||||
$response->getBody()->write(json_encode($data, JSON_THROW_ON_ERROR | JSON_UNESCAPED_UNICODE));
|
||||
return $response
|
||||
->withStatus($status)
|
||||
->withHeader('Content-Type', 'application/json');
|
||||
}
|
||||
|
||||
public static function success(
|
||||
ResponseInterface $response,
|
||||
mixed $data = null,
|
||||
string $message = 'OK',
|
||||
int $status = 200
|
||||
): ResponseInterface {
|
||||
return self::json($response, [
|
||||
'success' => true,
|
||||
'message' => $message,
|
||||
'data' => $data,
|
||||
], $status);
|
||||
}
|
||||
|
||||
public static function error(
|
||||
ResponseInterface $response,
|
||||
string $message,
|
||||
int $status = 400,
|
||||
array $errors = []
|
||||
): ResponseInterface {
|
||||
$body = [
|
||||
'success' => false,
|
||||
'message' => $message,
|
||||
];
|
||||
|
||||
if (!empty($errors)) {
|
||||
$body['errors'] = $errors;
|
||||
}
|
||||
|
||||
return self::json($response, $body, $status);
|
||||
}
|
||||
|
||||
public static function notFound(ResponseInterface $response, string $message = 'Not found'): ResponseInterface
|
||||
{
|
||||
return self::error($response, $message, 404);
|
||||
}
|
||||
|
||||
public static function unauthorized(ResponseInterface $response, string $message = 'Unauthorized'): ResponseInterface
|
||||
{
|
||||
return self::error($response, $message, 401);
|
||||
}
|
||||
|
||||
public static function forbidden(ResponseInterface $response, string $message = 'Forbidden'): ResponseInterface
|
||||
{
|
||||
return self::error($response, $message, 403);
|
||||
}
|
||||
|
||||
public static function validationError(
|
||||
ResponseInterface $response,
|
||||
array $errors
|
||||
): ResponseInterface {
|
||||
return self::error($response, 'Validation failed', 422, $errors);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user