Продолжение переписывания

This commit is contained in:
2023-11-03 02:38:51 +03:00
parent 12143c148d
commit a573faf5a1
7 changed files with 236 additions and 144 deletions

View File

@@ -2,11 +2,18 @@
require_once("../_auth.php");
require_once("../_utils.php");
require_once("../_errorslist.php");
require_once("../_types.php");
// Increment number of views for post
function Post_AddView ($id) {
// Functions
/*
* FUNCTION
* Increment number of views for post
*/
function Post_AddView (int $id): ReturnT {
global $db;
$s = $db->prepare("UPDATE posts SET views = views + 1 WHERE id = ?");
@@ -14,14 +21,17 @@ function Post_AddView ($id) {
$s->execute();
$d = $s->get_result()->fetch_assoc();
if (!(bool)$d) {
require_once("../_json.php");
ReturnJSONError($Err_Int_Unexpected, "failed to increment number of views");
}
if (!(bool)$d)
return new ReturnT(err_code: E_UIN_WRONGID, err_desc: "failed to increment number of views");
return new ReturnT(data: true);
}
// Get single publication by ID
function Post_GetByID ($id) {
/*
* FUNCTION
* Get post information by ID
*/
function Post_GetByID (int $id): ReturnT {
global $db;
$result = array();
@@ -31,9 +41,8 @@ function Post_GetByID ($id) {
$s->execute();
$d = $s->get_result()->fetch_assoc();
if (!(bool)$d) {
return null;
}
if (!(bool)$d)
return new ReturnT(err_code: E_UIN_WRONGID, err_desc: "failed to get post");
$result["id"] = $d["id"];
$result["author_id"] = $d["author_id"];
@@ -49,29 +58,49 @@ function Post_GetByID ($id) {
$result["preview_path"] = $d["preview_path"];
$result["edit_lock"] = $d["edit_lock"];
Post_AddView($id); // TODO: add rate-limit or completely rework
$r = Post_AddView($id); // TODO: add rate-limit or completely rework
if ($r.IsError())
return $r;
return $result;
return new ReturnT(data: $result);
}
if (ThisFileIsRequested(__FILE__)) {
require_once("../_json.php");
// Methods
if (isset($_REQUEST["id"])) {
if (!ctype_digit($_REQUEST["id"]))
ReturnJSONError($Err_RDP_InvalidID, "id must be numeric");
$UserID = intval($_REQUEST["id"]);
/*
* METHOD
* Get post information by ID
*/
function Post_GetByID_Method (array $req) {
// Input sanity checks
$PostID = null;
if (isset($req["id"])) {
if (!ctype_digit($req["id"]))
return new ReturnT(err_code: E_UIN_BADARGS, "id must be numeric");
$PostID = intval($req["id"]);
} else {
ReturnJSONError($Err_RDP_InvalidID, "id must be specified");
return new ReturnT(err_code: E_UIN_INSUFARGS, "id must be specified");
}
$ResponseData = Post_GetByID($_REQUEST["id"]);
if ($ResponseData)
ReturnJSONData($ResponseData);
// Actions
return Post_GetByID($PostID);
}
if (Utils_ThisFileIsRequested(__FILE__)) {
require_once("../_json.php");
$result = Post_GetByID_Method($_REQUEST);
if ($result->IsError())
$result->ThrowJSONError();
else
ReturnJSONError($Err_DP_IDNotFound, "wrong id");
JSON_ReturnData($result->GetData());
}
?>