new class based syntax for handling api requests in php
This commit is contained in:
parent
d3c3ee3044
commit
fd9a54209d
24
api/RequestBase.php
Normal file
24
api/RequestBase.php
Normal file
@ -0,0 +1,24 @@
|
|||||||
|
<?php
|
||||||
|
require 'Database.php';
|
||||||
|
|
||||||
|
abstract class RequestBase {
|
||||||
|
private array $actions = array();
|
||||||
|
protected mysqli $conn;
|
||||||
|
|
||||||
|
abstract function initIdentifiers();
|
||||||
|
|
||||||
|
function addIdentifier($action, $callback) {
|
||||||
|
$this->actions[$action] = $callback;
|
||||||
|
}
|
||||||
|
|
||||||
|
function handleAction() {
|
||||||
|
$this->conn = Database::getInstance()->getConnection();
|
||||||
|
|
||||||
|
if (isset($_POST['action'])) {
|
||||||
|
$this->initIdentifiers();
|
||||||
|
|
||||||
|
$action = $_POST['action'];
|
||||||
|
call_user_func($this->actions[$action]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -1,29 +1,25 @@
|
|||||||
<?php
|
<?php
|
||||||
require 'Database.php';
|
require 'RequestBase.php';
|
||||||
require 'SSettings.php';
|
|
||||||
|
|
||||||
$conn = Database::getInstance()->getConnection();
|
class Settings extends RequestBase {
|
||||||
$settings = new SSettings();
|
function initIdentifiers() {
|
||||||
|
$this->addIdentifier("loadGeneralSettings", function () {
|
||||||
if (isset($_POST['action'])) {
|
|
||||||
$action = $_POST['action'];
|
|
||||||
switch ($action) {
|
|
||||||
case "loadGeneralSettings":
|
|
||||||
$query = "SELECT * from settings";
|
$query = "SELECT * from settings";
|
||||||
|
|
||||||
$result = $conn->query($query);
|
$result = $this->conn->query($query);
|
||||||
if ($result->num_rows > 1) {
|
if ($result->num_rows > 1) {
|
||||||
// todo throw error
|
// todo throw error
|
||||||
}
|
}
|
||||||
|
|
||||||
$r = mysqli_fetch_assoc($result);
|
$r = mysqli_fetch_assoc($result);
|
||||||
// booleans need to be set manually
|
// booleans need to be set manually
|
||||||
$r['passwordEnabled'] = $r['password'] != "-1";
|
$r['passwordEnabled'] = $r['password'] != "-1";
|
||||||
$r['TMDB_grabbing'] = ($r['TMDB_grabbing'] != '0');
|
$r['TMDB_grabbing'] = ($r['TMDB_grabbing'] != '0');
|
||||||
|
|
||||||
echo json_encode($r);
|
echo json_encode($r);
|
||||||
break;
|
});
|
||||||
case "saveGeneralSettings":
|
|
||||||
|
$this->addIdentifier("saveGeneralSettings", function () {
|
||||||
$mediacentername = $_POST['mediacentername'];
|
$mediacentername = $_POST['mediacentername'];
|
||||||
$password = $_POST['password'];
|
$password = $_POST['password'];
|
||||||
$videopath = $_POST['videopath'];
|
$videopath = $_POST['videopath'];
|
||||||
@ -38,16 +34,17 @@ if (isset($_POST['action'])) {
|
|||||||
TMDB_grabbing=$tmdbsupport
|
TMDB_grabbing=$tmdbsupport
|
||||||
WHERE 1";
|
WHERE 1";
|
||||||
|
|
||||||
if ($conn->query($query) === true) {
|
if ($this->conn->query($query) === true) {
|
||||||
echo '{"success": true}';
|
echo '{"success": true}';
|
||||||
} else {
|
} else {
|
||||||
echo '{"success": true}';
|
echo '{"success": true}';
|
||||||
}
|
}
|
||||||
break;
|
});
|
||||||
case "loadInitialData":
|
|
||||||
|
$this->addIdentifier("loadInitialData", function () {
|
||||||
$query = "SELECT * from settings";
|
$query = "SELECT * from settings";
|
||||||
|
|
||||||
$result = $conn->query($query);
|
$result = $this->conn->query($query);
|
||||||
if ($result->num_rows > 1) {
|
if ($result->num_rows > 1) {
|
||||||
// todo throw error
|
// todo throw error
|
||||||
}
|
}
|
||||||
@ -60,6 +57,9 @@ if (isset($_POST['action'])) {
|
|||||||
}
|
}
|
||||||
unset($r['password']);
|
unset($r['password']);
|
||||||
echo json_encode($r);
|
echo json_encode($r);
|
||||||
break;
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
$sett = new Settings();
|
||||||
|
$sett->handleAction();
|
||||||
|
Loading…
Reference in New Issue
Block a user