2022-03-08 11:05:52 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
use GraphQL\Type\Definition\Type;
|
|
|
|
use GraphQL\Type\Definition\ObjectType;
|
|
|
|
|
|
|
|
$blogPostFields = new ObjectType([
|
|
|
|
"name" => "Blog",
|
|
|
|
"fields" => [
|
|
|
|
"title" => Type::string(),
|
|
|
|
"content" => Type::string(),
|
|
|
|
"date" => Type::string(),
|
2022-03-08 11:14:31 +01:00
|
|
|
"id" => Type::string()
|
2022-03-08 11:05:52 +01:00
|
|
|
],
|
|
|
|
]);
|
|
|
|
|
|
|
|
function blogPost($id, $conn)
|
|
|
|
{
|
|
|
|
$id = $conn->real_escape_string($id);
|
|
|
|
$result = $conn->query("SELECT * FROM posts WHERE id=$id");
|
|
|
|
if ($result->num_rows > 0) {
|
|
|
|
$row = $result->fetch_assoc();
|
|
|
|
} else {
|
|
|
|
return [
|
2022-03-08 11:14:31 +01:00
|
|
|
"title" => "Nicht Gefunden",
|
|
|
|
"content" => "Post wurde nicht gefunden",
|
|
|
|
"date" => "2000-01-01 00:00:00",
|
|
|
|
"id" => "-1"
|
2022-03-08 11:05:52 +01:00
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
return [
|
2022-03-08 12:06:56 +01:00
|
|
|
"title" => $row["title"],
|
|
|
|
"content" => $row["content"],
|
|
|
|
"date" => $row["date"],
|
|
|
|
"id" => $row["id"],
|
2022-03-08 11:05:52 +01:00
|
|
|
];
|
|
|
|
}
|
2022-03-08 12:06:56 +01:00
|
|
|
|
|
|
|
function blogPosts($count, $contentLength, $conn)
|
|
|
|
{
|
|
|
|
$response = [];
|
|
|
|
$result = $conn->query("SELECT * FROM posts order by id desc limit $count");
|
|
|
|
if ($result->num_rows > 0) {
|
|
|
|
while ($row = $result->fetch_assoc()) {
|
|
|
|
$content = $row["content"];
|
|
|
|
if($contentLength != null && strlen($content) > $contentLength) {
|
|
|
|
$contentNew = substr($content, 0, $contentLength);
|
|
|
|
$contentRest = substr($content, $contentLength);
|
|
|
|
|
|
|
|
$content = $contentNew . explode(" ", $contentRest)[0] . " ...";
|
|
|
|
|
|
|
|
}
|
|
|
|
$blogElement = [
|
|
|
|
"title" => $row["title"],
|
|
|
|
"content" => $content,
|
|
|
|
"date" => $row["date"],
|
|
|
|
"id" => $row["id"],
|
|
|
|
];
|
|
|
|
|
|
|
|
array_push($response, $blogElement);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $response;
|
|
|
|
}
|