summaryrefslogtreecommitdiff
path: root/intern/api
diff options
context:
space:
mode:
Diffstat (limited to 'intern/api')
-rw-r--r--intern/api/config.php3
-rw-r--r--intern/api/db.php29
-rw-r--r--intern/api/termine.php19
3 files changed, 42 insertions, 9 deletions
diff --git a/intern/api/config.php b/intern/api/config.php
new file mode 100644
index 0000000..728ff37
--- /dev/null
+++ b/intern/api/config.php
@@ -0,0 +1,3 @@
+<?php
+
+$json_file = __DIR__ . "/../json/termine.json";
diff --git a/intern/api/db.php b/intern/api/db.php
index 9521148..8385ed5 100644
--- a/intern/api/db.php
+++ b/intern/api/db.php
@@ -1,8 +1,25 @@
<?php
-require_once __DIR__ . '/config.php';
-$db = new pdo($dsn, $db_user, $db_passwd);
-$db->exec('SET CHARACTER SET utf8');
+function get_db() {
+ require_once __DIR__ . '/config.php';
+ require_once __DIR__ . '/secrets.php'; // beinhaltet die Parameter für die Datenbank
+
+ try {
+ $db = new pdo($dsn, $db_user, $db_passwd);
+ } catch (Exception $e) {
+ error_log($e->getMessage());
+
+ unset($dsn);
+ unset($db_user);
+ unset($db_passwd);
+
+ return false;
+ }
+ $db->exec('SET CHARACTER SET utf8');
+
+ unset($dsn);
+ unset($db_user);
+ unset($db_passwd);
+
+ return $db;
+}
-unset($dsn);
-unset($db_user);
-unset($db_passwd);
diff --git a/intern/api/termine.php b/intern/api/termine.php
index 9c9d454..b5ff1db 100644
--- a/intern/api/termine.php
+++ b/intern/api/termine.php
@@ -1,12 +1,25 @@
<?php
+header('Content-Type: application/json');
require_once __DIR__ . '/db.php';
+require_once __DIR__ . '/config.php';
+
+$db = get_db();
+
+if ( false === $db ) {
+ echo file_get_contents($json_file); // Liest veralte JSON Datei
+ error_log (json_encode( array( "error" => true, "reason" => "Can't connect to the database", "build" => time() ) ) );
+ exit;
+}
$res = $db->query('select termin, uhrzeit from proben where date(STR_TO_DATE(termin, "%d.%m.%Y")) > (SELECT CURDATE() );');
$proben = $res->fetchAll(PDO::FETCH_ASSOC);
$res = $db->query('select termin,uhrzeit,beschreibung,anfahrt from konzert order by id desc limit 1;');
$konzert = $res->fetchAll(PDO::FETCH_ASSOC);
-$output = array("konzert" => $konzert, "proben" => $proben);
-header('Content-Type: application/json');
-echo( json_encode($output, JSON_PRETTY_PRINT) );
+$output = array("konzert" => $konzert, "proben" => $proben, "build" => time());
+$data = json_encode($output, JSON_PRETTY_PRINT);
+
+file_put_contents($json_file, $data); // speichert die JSON Datei für den Fall, dass es später Fehler gibt
+
+echo $file;