Refactor entry API: consolidate entry handling, update naming conventions, and enhance error reporting
This commit is contained in:
1 parent
205f2e2f73
commit
ad9290d2ad
9 files changed
+72
-12
No files matched your search
Executable
+52
@@ -0,0 +1,52 @@
|
||||
<?php
|
||||
// Set internal to false so that the answer goes to the client
|
||||
if (!isset($internal)) {
|
||||
$internal = false;
|
||||
define("API_DIR", "../");
|
||||
}
|
||||
|
||||
// Load Main Library
|
||||
require_once API_DIR . "api.php";
|
||||
|
||||
$api = new gbAPI(false, $internal);
|
||||
|
||||
$input = $api->getInput();
|
||||
|
||||
if (!isset($input["name"]) || !isset($input["text"])) {
|
||||
$api->answer(400, [
|
||||
"error" => "missing_fields",
|
||||
"error_text" => "Some fields are missing"
|
||||
], true);
|
||||
return;
|
||||
}
|
||||
|
||||
$html_allowed = $api->getSetting("allow_html");
|
||||
|
||||
if ($html_allowed == "true") {
|
||||
$name = $input["name"];
|
||||
$text = $input["text"];
|
||||
}else{
|
||||
$name = htmlspecialchars($input["name"]);
|
||||
$text = htmlspecialchars($input["text"]);
|
||||
}
|
||||
|
||||
$date = date("Y-m-d H:i:s");
|
||||
$approve = $api->getSetting("approve_entrys") == "true" ? true : false;
|
||||
|
||||
if ($approve) {
|
||||
$status = 0;
|
||||
}else{
|
||||
$status = 1;
|
||||
}
|
||||
|
||||
|
||||
// Insert Into Database
|
||||
$command = "INSERT INTO entrys (name, text, date, status) VALUES (:name, :text, :date, :status)";
|
||||
$api->dbCommand($command, false, true, [
|
||||
":name" => $name,
|
||||
":text" => $text,
|
||||
":date" => $date,
|
||||
":status" => $status
|
||||
]);
|
||||
|
||||
$api->answer(200);
|
||||
Executable
+47
@@ -0,0 +1,47 @@
|
||||
<?php
|
||||
// Set internal to false so that the answer goes to the client
|
||||
if (!isset($internal)) {
|
||||
$internal = false;
|
||||
define("API_DIR", "../");
|
||||
}
|
||||
|
||||
// Load Main Library
|
||||
require_once API_DIR . "api.php";
|
||||
|
||||
$api = new gbAPI(false, $internal);
|
||||
|
||||
// Get Entrys
|
||||
$command = "SELECT * FROM entrys WHERE status = :status ORDER BY date DESC";
|
||||
$result = $api->dbCommand($command, true, true, [":status" => 1]);
|
||||
|
||||
$entrys = [];
|
||||
|
||||
if (!$result) {
|
||||
$api->answer(200, [
|
||||
"entrys" => [],
|
||||
"empty" => true
|
||||
]);
|
||||
return;
|
||||
}
|
||||
|
||||
foreach($result as $row) {
|
||||
$entry_name = $row["name"];
|
||||
$entry_text = $row["text"];
|
||||
$date = new DateTime($row["date"]);
|
||||
$entry_date = $date->format("d.m.Y H:i:s");
|
||||
|
||||
$entry = [
|
||||
"name" => $entry_name,
|
||||
"text" => $entry_text,
|
||||
"date" => $entry_date
|
||||
];
|
||||
|
||||
$entrys[] = $entry;
|
||||
}
|
||||
|
||||
$json = json_encode($entrys);
|
||||
|
||||
$api->answer(200, [
|
||||
"entrys" => $json,
|
||||
"empty" => false
|
||||
]);
|
||||
@@ -0,0 +1,37 @@
|
||||
<?php
|
||||
// Set internal to false so that the answer goes to the client
|
||||
if (!isset($internal)) {
|
||||
$internal = false;
|
||||
define("API_DIR", "../");
|
||||
}
|
||||
|
||||
// Load Main Library
|
||||
require_once API_DIR . "api.php";
|
||||
|
||||
$api = new gbAPI(true, $internal);
|
||||
|
||||
$input = $api->getInput();
|
||||
|
||||
|
||||
// Check given Status
|
||||
if (!isset($input["status"])) {
|
||||
$api->answer(400, [
|
||||
"error" => "missing_fields",
|
||||
"error_text" => "Some fields are missing"
|
||||
], true);
|
||||
return;
|
||||
}
|
||||
|
||||
if ($status !== 0 && $status !== 1 && $status !== 2) {
|
||||
$api->answer(400, [
|
||||
"error" => "status_invalid",
|
||||
"error_text" => "The given status is invalid."
|
||||
], true);
|
||||
}
|
||||
|
||||
$command = "SELECT * FROM entrys WHERE status = :status";
|
||||
|
||||
$response = $api->dbCommand($command, true, true, [
|
||||
":status" => $status
|
||||
]);
|
||||
|
||||
Reference in new issue
Block a user