Used CKEditor for being able to edit the post body. Added the ability to make a post and send it to the backend. Formatted post on backend by moving images into respective directory with the post name and unique ID. Showed message if errored or succeeded.
Signed-off-by: rodude123 <rodude123@gmail.com>
This commit is contained in:
Vendored
+43
-2
@@ -12,7 +12,6 @@ use Slim\App;
|
||||
class blogRoutes implements routesInterface
|
||||
{
|
||||
private blogData $blogData;
|
||||
|
||||
/**
|
||||
* constructor used to instantiate a base blog routes, to be used in the index.php file.
|
||||
* @param App $app - the slim app used to create the routes
|
||||
@@ -30,6 +29,48 @@ class blogRoutes implements routesInterface
|
||||
*/
|
||||
public function createRoutes(App $app): void
|
||||
{
|
||||
// TODO: Implement createRoutes() method.
|
||||
$app->post("/blog/post", function (Request $request, Response $response, array $args)
|
||||
{
|
||||
$data = $request->getParsedBody();
|
||||
$files = $request->getUploadedFiles();
|
||||
if (empty($data["title"]) || empty($data["body"]) || empty($data["dateCreated"]) || empty($data["featured"]) || empty($data["categories"]) || empty($files["headerImg"]))
|
||||
{
|
||||
// uh oh sent some empty data
|
||||
$response->getBody()->write(json_encode(array("error" => "Error, empty data sent")));
|
||||
return $response->withStatus(400);
|
||||
}
|
||||
|
||||
$insertedID = $this->blogData->createPost($data["title"], $data["body"], $data["dateCreated"], $data["featured"], $data["categories"], $files["headerImg"]);
|
||||
if (!is_int($insertedID))
|
||||
{
|
||||
// uh oh something went wrong
|
||||
$response->getBody()->write(json_encode(array("error" => $insertedID)));
|
||||
return $response->withStatus(500);
|
||||
}
|
||||
$response->getBody()->write(json_encode(array("ID" => $insertedID)));
|
||||
return $response->withStatus(201);
|
||||
});
|
||||
|
||||
$app->post("/blog/uploadPostImage", function (Request $request, Response $response)
|
||||
{
|
||||
$files = $request->getUploadedFiles();
|
||||
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->uploadPostImage($files["upload"]);
|
||||
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);
|
||||
});
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user