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
|
||||
require 'Database.php';
|
||||
require 'SSettings.php';
|
||||
require 'RequestBase.php';
|
||||
|
||||
$conn = Database::getInstance()->getConnection();
|
||||
$settings = new SSettings();
|
||||
|
||||
if (isset($_POST['action'])) {
|
||||
$action = $_POST['action'];
|
||||
switch ($action) {
|
||||
case "loadGeneralSettings":
|
||||
class Settings extends RequestBase {
|
||||
function initIdentifiers() {
|
||||
$this->addIdentifier("loadGeneralSettings", function () {
|
||||
$query = "SELECT * from settings";
|
||||
|
||||
$result = $conn->query($query);
|
||||
$result = $this->conn->query($query);
|
||||
if ($result->num_rows > 1) {
|
||||
// todo throw error
|
||||
}
|
||||
|
||||
$r = mysqli_fetch_assoc($result);
|
||||
// booleans need to be set manually
|
||||
$r['passwordEnabled'] = $r['password'] != "-1";
|
||||
$r['passwordEnabled'] = $r['password'] != "-1";
|
||||
$r['TMDB_grabbing'] = ($r['TMDB_grabbing'] != '0');
|
||||
|
||||
echo json_encode($r);
|
||||
break;
|
||||
case "saveGeneralSettings":
|
||||
});
|
||||
|
||||
$this->addIdentifier("saveGeneralSettings", function () {
|
||||
$mediacentername = $_POST['mediacentername'];
|
||||
$password = $_POST['password'];
|
||||
$videopath = $_POST['videopath'];
|
||||
@ -38,16 +34,17 @@ if (isset($_POST['action'])) {
|
||||
TMDB_grabbing=$tmdbsupport
|
||||
WHERE 1";
|
||||
|
||||
if ($conn->query($query) === true) {
|
||||
if ($this->conn->query($query) === true) {
|
||||
echo '{"success": true}';
|
||||
} else {
|
||||
echo '{"success": true}';
|
||||
}
|
||||
break;
|
||||
case "loadInitialData":
|
||||
});
|
||||
|
||||
$this->addIdentifier("loadInitialData", function () {
|
||||
$query = "SELECT * from settings";
|
||||
|
||||
$result = $conn->query($query);
|
||||
$result = $this->conn->query($query);
|
||||
if ($result->num_rows > 1) {
|
||||
// todo throw error
|
||||
}
|
||||
@ -60,6 +57,9 @@ if (isset($_POST['action'])) {
|
||||
}
|
||||
unset($r['password']);
|
||||
echo json_encode($r);
|
||||
break;
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
$sett = new Settings();
|
||||
$sett->handleAction();
|
||||
|
Loading…
Reference in New Issue
Block a user