Created feeds and UI for feeds and newsletter
🚀 Deploy website on push / 🎉 Deploy (push) Successful in 19s

Signed-off-by: rodude123 <rodude123@gmail.com>
This commit is contained in:
2023-11-14 01:02:27 +00:00
parent 6cfea3fc98
commit f27a5113b1
32 changed files with 2729 additions and 671 deletions
+171 -131
View File
@@ -4,6 +4,7 @@ namespace api\blog;
require_once __DIR__ . "/../utils/routesInterface.php";
require_once "blogData.php";
use api\utils\routesInterface;
use Psr\Http\Message\ResponseInterface as Response;
use Psr\Http\Message\ServerRequestInterface as Request;
@@ -80,35 +81,15 @@ class blogRoutes implements routesInterface
$app->get("/blog/post/{type}", function (Request $request, Response $response, $args)
{
if ($args["type"] != null)
if ($args["type"] == null)
{
if ($args["type"] == "latest")
{
$post = $this->blogData->getLatestBlogPost();
if (array_key_exists("errorMessage", $post))
{
$response->getBody()->write(json_encode($post));
return $response->withStatus(404);
}
$response->getBody()->write(json_encode(array("error" => "Please provide a title")));
return $response->withStatus(400);
}
$response->getBody()->write(json_encode($post));
return $response;
}
if ($args["type"] == "featured")
{
$post = $this->blogData->getFeaturedBlogPost();
if (array_key_exists("errorMessage", $post))
{
$response->getBody()->write(json_encode($post));
return $response->withStatus(404);
}
$response->getBody()->write(json_encode($post));
return $response;
}
$post = $this->blogData->getBlogPost($args["type"]);
if ($args["type"] == "latest")
{
$post = $this->blogData->getLatestBlogPost();
if (array_key_exists("errorMessage", $post))
{
$response->getBody()->write(json_encode($post));
@@ -119,114 +100,173 @@ class blogRoutes implements routesInterface
return $response;
}
$response->getBody()->write(json_encode(array("error" => "Please provide a title")));
if ($args["type"] == "featured")
{
$post = $this->blogData->getFeaturedBlogPost();
if (array_key_exists("errorMessage", $post))
{
$response->getBody()->write(json_encode($post));
return $response->withStatus(404);
}
$response->getBody()->write(json_encode($post));
return $response;
}
$post = $this->blogData->getBlogPost($args["type"]);
if (array_key_exists("errorMessage", $post))
{
$response->getBody()->write(json_encode($post));
return $response->withStatus(404);
}
$response->getBody()->write(json_encode($post));
return $response;
});
$app->get("/blog/feed/{type}", function (Request $request, Response $response, $args)
{
if ($args["type"] == null)
{
$response->getBody()->write(json_encode(array("error" => "Please provide a title")));
return $response->withStatus(400);
}
$feed = $this->blogData->getFeed($args["type"]);
if (is_array($feed))
{
$response->getBody()->write(json_encode($feed));
return $response->withStatus(404);
}
if ($args["type"] == "atom")
{
$response->getBody()->write($feed);
return $response->withHeader("Content-Type", "application/atom+xml");
}
if ($args["type"] == "rss")
{
$response->getBody()->write($feed);
return $response->withHeader("Content-Type", "application/rss+xml");
}
if ($args["type"] == "json")
{
$response->getBody()->write(json_encode($feed));
return $response->withHeader("Content-Type", "application/feed+json");
}
$response->getBody()->write(json_encode(array("error" => "Invalid feed type")));
return $response->withStatus(400);
});
$app->get("/blog/search/{searchTerm}", function (Request $request, $response, $args)
{
if ($args["searchTerm"] != null)
if ($args["searchTerm"] == null)
{
$posts = $this->blogData->searchBlog($args["searchTerm"]);
$json = json_encode($posts);
$response->getBody()->write($json);
if (array_key_exists("errorMessage", $posts))
{
$response->withStatus(404);
}
return $response;
$response->getBody()->write(json_encode(array("error" => "Please provide a search term")));
return $response->withStatus(400);
}
$response->getBody()->write(json_encode(array("error" => "Please provide a search term")));
return $response->withStatus(400);
$posts = $this->blogData->searchBlog($args["searchTerm"]);
$json = json_encode($posts);
$response->getBody()->write($json);
if (array_key_exists("errorMessage", $posts))
{
$response->withStatus(404);
}
return $response;
});
$app->patch("/blog/post/{id}", function (Request $request, Response $response, $args)
{
$data = $request->getParsedBody();
if ($args["id"] != null)
if ($args["id"] == null)
{
if (empty($data["title"]) || strlen($data["featured"]) == 0 || empty($data["body"]) || empty($data["bodyText"]) || empty($data["dateModified"]) || empty($data["categories"]))
{
// uh oh sent some empty data
$response->getBody()->write(json_encode(array("error" => "Only some of the data was sent")));
return $response->withStatus(400);
}
if (!preg_match('/[a-zA-Z0-9 ]+, |\w+/mx', $data["categories"]))
{
// uh oh sent some empty data
$response->getBody()->write(json_encode(array("error" => "Categories must be in a CSV format")));
return $response->withStatus(400);
}
$message = $this->blogData->updatePost($args["id"], $data["title"], intval($data["featured"]), $data["abstract"], $data["body"], $data["bodyText"], $data["dateModified"], $data["categories"]);
if ($message === "post not found")
{
// uh oh something went wrong
$response->getBody()->write(json_encode(array("error" => "Error, post not found")));
return $response->withStatus(404);
}
if ($message === "unset featured")
{
// uh oh something went wrong
$response->getBody()->write(json_encode(array("error" => "Error, cannot unset featured post, try updating another post to be featured first")));
return $response->withStatus(409);
}
if (!is_bool($message) || $message === false)
{
// uh oh something went wrong
$response->getBody()->write(json_encode(array("error" => $message)));
return $response->withStatus(500);
}
$response->withStatus(201);
return $response;
$response->getBody()->write(json_encode(array("error" => "Please provide an ID")));
return $response->withStatus(400);
}
$response->getBody()->write(json_encode(array("error" => "Please provide an ID")));
return $response->withStatus(400);
if (empty($data["title"]) || strlen($data["featured"]) == 0 || empty($data["body"]) || empty($data["bodyText"]) || empty($data["dateModified"]) || empty($data["categories"]))
{
// uh oh sent some empty data
$response->getBody()->write(json_encode(array("error" => "Only some of the data was sent")));
return $response->withStatus(400);
}
if (!preg_match('/[a-zA-Z0-9 ]+, |\w+/mx', $data["categories"]))
{
// uh oh sent some empty data
$response->getBody()->write(json_encode(array("error" => "Categories must be in a CSV format")));
return $response->withStatus(400);
}
$message = $this->blogData->updatePost($args["id"], $data["title"], intval($data["featured"]), $data["abstract"], $data["body"], $data["bodyText"], $data["dateModified"], $data["categories"]);
if ($message === "post not found")
{
// uh oh something went wrong
$response->getBody()->write(json_encode(array("error" => "Error, post not found")));
return $response->withStatus(404);
}
if ($message === "unset featured")
{
// uh oh something went wrong
$response->getBody()->write(json_encode(array("error" => "Error, cannot unset featured post, try updating another post to be featured first")));
return $response->withStatus(409);
}
if (!is_bool($message) || $message === false)
{
// uh oh something went wrong
$response->getBody()->write(json_encode(array("error" => $message)));
return $response->withStatus(500);
}
$response->withStatus(201);
return $response;
});
$app->delete("/blog/post/{id}", function (Request $request, Response $response, $args)
{
if ($args["id"] != null)
if ($args["id"] == null)
{
$message = $this->blogData->deletePost($args["id"]);
if ($message === "post not found")
{
// uh oh something went wrong
$response->getBody()->write(json_encode(array("error" => "Error, post not found")));
return $response->withStatus(404);
}
if ($message === "error")
{
// uh oh something went wrong
$response->getBody()->write(json_encode(array("error" => "Error, something went wrong")));
return $response->withStatus(500);
}
if ($message === "cannot delete")
{
// uh oh something went wrong
$response->getBody()->write(json_encode(array("error" => "Error, cannot delete featured post")));
return $response->withStatus(409);
}
return $response;
$response->getBody()->write(json_encode(array("error" => "Please provide an ID")));
return $response->withStatus(400);
}
$response->getBody()->write(json_encode(array("error" => "Please provide an ID")));
return $response->withStatus(400);
$message = $this->blogData->deletePost($args["id"]);
if ($message === "post not found")
{
// uh oh something went wrong
$response->getBody()->write(json_encode(array("error" => "Error, post not found")));
return $response->withStatus(404);
}
if ($message === "error")
{
// uh oh something went wrong
$response->getBody()->write(json_encode(array("error" => "Error, something went wrong")));
return $response->withStatus(500);
}
if ($message === "cannot delete")
{
// uh oh something went wrong
$response->getBody()->write(json_encode(array("error" => "Error, cannot delete featured post")));
return $response->withStatus(409);
}
return $response;
});
$app->post("/blog/post", function (Request $request, Response $response)
@@ -290,28 +330,28 @@ class blogRoutes implements routesInterface
{
$files = $request->getUploadedFiles();
if ($args["id"] != null)
if ($args["id"] == null)
{
if (empty($files))
{
// uh oh sent some empty data
$response->getBody()->write(json_encode(array("error" => array("message" => "Error, empty data sent"))));
return $response->withStatus(400);
}
$message = $this->blogData->uploadHeaderImage($args["id"], $files["headerImg"]);
if (!is_array($message))
{
$response->getBody()->write(json_encode(array("error" => array("message" => $message))));
return $response->withStatus(500);
}
$response->getBody()->write(json_encode($message));
return $response->withStatus(201);
$response->getBody()->write(json_encode(array("error" => "Please provide an ID")));
return $response->withStatus(400);
}
$response->getBody()->write(json_encode(array("error" => "Please provide an ID")));
return $response->withStatus(400);
if (empty($files))
{
// uh oh sent some empty data
$response->getBody()->write(json_encode(array("error" => array("message" => "Error, empty data sent"))));
return $response->withStatus(400);
}
$message = $this->blogData->uploadHeaderImage($args["id"], $files["headerImg"]);
if (!is_array($message))
{
$response->getBody()->write(json_encode(array("error" => array("message" => $message))));
return $response->withStatus(500);
}
$response->getBody()->write(json_encode($message));
return $response->withStatus(201);
});
}
}