Delete return pages

This commit is contained in:
Kilian Hofmann 2024-07-27 17:01:24 +02:00
parent ce7ba50b9d
commit 6018e8bee5
4 changed files with 20 additions and 7 deletions

View File

@ -30,7 +30,7 @@ class Posts extends Api
// Throw 400 error if a required one is missing.
$content = Input::post("content");
// This one is optional
$limit = constrain(0, 30, intval(Input::post("limit", 10)));
$limit = constrain(0, 30, intval(Input::get("l", 10)));
if (empty($content)) throw ApiError::missingField(["content"]);
// Get logged in user
@ -77,9 +77,11 @@ class Posts extends Api
public function delete($id): void
{
// Fetch ax(0, intval(Input::get("p", 0)));
$limit = constrain(0, 30, intval(Input::get("l", 10)));
// Try delete, 404 if post was not found.
try {
Response::json(Post::getByID($id)->delete());
Response::json(Post::getByID($id)->delete($limit));
} catch (Exception $err) {
switch ($err->getMessage()) {
case "NotFound":

View File

@ -281,7 +281,7 @@ paths:
content:
application/json:
schema:
$ref: "#/components/schemas/PostResponse"
$ref: "#/components/schemas/PostDeleteResponse"
404:
description: Post not found.
content:
@ -697,6 +697,8 @@ components:
type: number
post:
$ref: "#/components/schemas/PostResponse"
PostDeleteResponse:
$ref: "#/components/schemas/PostCreateResponse"
PostListResponse:
type: object
properties:

File diff suppressed because one or more lines are too long

View File

@ -165,14 +165,23 @@ class Post implements JsonSerializable
return Post::getByID($this->id);
}
public function delete(): Post
public function delete(int $limit): array
{
$db = Database::getInstance();
$stmt = $db->prepare("DELETE FROM egb_gaestebuch WHERE id = :ID");
$stmt->bindValue(":ID", $this->id);
$stmt->execute();
return $this;
$stmt = $db->prepare(
"SELECT
COUNT(*)
FROM
egb_gaestebuch"
);
$stmt->execute();
$count = $stmt->fetch(PDO::FETCH_COLUMN, 0);
return ["pages" => intdiv($count, $limit + 1) + 1, "data" => $this];
}
/*