This commit is contained in:
Kilian Hofmann 2024-07-13 17:59:54 +02:00
parent 4d25387a47
commit 03da043be3
9 changed files with 106 additions and 6 deletions

3
exam/api/docs/index.css Normal file
View File

@ -0,0 +1,3 @@
body {
background-color: red;
}

View File

@ -1 +1,10 @@
DOCUMENTATION <!DOCTYPE html>
<html lang="en">
<head>
<link href="./index.css" rel="stylesheet" />
<title>Calculator</title>
</head>
<body></body>
</html>

View File

@ -1,4 +1,3 @@
<?php <?php
foreach (glob(__DIR__ . '/*/index.php') as $filename) {
require_once($filename); Headers::redirect("docs/index.html");
}

26
exam/api/posts/index.php Normal file
View File

@ -0,0 +1,26 @@
<?php
$method = $_SERVER['REQUEST_METHOD'];
switch ($method) {
case "GET":
return get();
default:
return Response::api("$method not supported", 500);
}
function get()
{
$db = Database::getInstance();
$query =
"SELECT
*
FROM
egb_gaestebuch";
$stmt = $db->prepare($query);
$stmt->execute();
Response::api($stmt->fetchAll());
}

View File

@ -11,5 +11,16 @@ switch ($method) {
function get() function get()
{ {
Response::api("GET USERS"); $db = Database::getInstance();
$query =
"SELECT
*
FROM
egb_benutzer";
$stmt = $db->prepare($query);
$stmt->execute();
Response::api($stmt->fetchAll());
} }

9
exam/config/database.php Normal file
View File

@ -0,0 +1,9 @@
<?php
return [
"host" => "usersql.zedat.fu-berlin.de",
"user" => "khofmann-sql",
"passwd" => "xz8c7m7p",
"database" => "khofmann-db1",
"charset" => "utf8",
];

View File

@ -4,7 +4,11 @@ $path = ltrim(str_replace(Config::getBasePath(), "", $_SERVER['REQUEST_URI']), "
try { try {
if (substr($path, 0, 3) === "api") { if (substr($path, 0, 3) === "api") {
require_once __DIR__ . "/../api/index.php"; if (strpos($path, "docs") === false) {
require_once __DIR__ . "/../$path/index.php";
} else {
Headers::redirect("index.html");
}
} else { } else {
require_once __DIR__ . "/../pages/index.html"; require_once __DIR__ . "/../pages/index.html";
} }

View File

@ -5,10 +5,12 @@ class Config
private static $instances = []; private static $instances = [];
private array $app; private array $app;
private array $database;
protected function __construct() protected function __construct()
{ {
$this->app = require_once __DIR__ . "/../../config/app.php"; $this->app = require_once __DIR__ . "/../../config/app.php";
$this->database = require_once __DIR__ . "/../../config/database.php";
} }
protected function __clone() protected function __clone()
@ -34,4 +36,9 @@ class Config
{ {
return Config::getInstance()->app["basePath"]; return Config::getInstance()->app["basePath"];
} }
public static function getDatabase()
{
return Config::getInstance()->database;
}
} }

32
exam/vendor/database/database.php vendored Normal file
View File

@ -0,0 +1,32 @@
<?php
class Database extends PDO
{
private static $instances = [];
protected function __construct($dsn, $username = null, $password = null, array $options = null)
{
parent::__construct($dsn, $username, $password, $options);
}
public static function getInstance(): Database
{
$cls = static::class;
if (!isset(self::$instances[$cls])) {
$dataAccess = Config::getDatabase();
self::$instances[$cls] = new static(
"mysql:host={$dataAccess["host"]};dbname={$dataAccess["database"]};charset={$dataAccess["charset"]}",
$dataAccess["user"],
$dataAccess["passwd"],
[
PDO::ATTR_PERSISTENT => false,
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
PDO::ATTR_EMULATE_PREPARES => false,
]
);
}
return self::$instances[$cls];
}
}