82 lines
2.9 KiB
PHP
82 lines
2.9 KiB
PHP
<?php
|
|
|
|
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;
|
|
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
|
|
*/
|
|
public function __construct(App $app)
|
|
{
|
|
$this->blogData = new blogData();
|
|
$this->createRoutes($app);
|
|
}
|
|
|
|
/**
|
|
* creates the routes for the blog
|
|
* @param App $app - the slim app used to create the routes
|
|
* @return void - returns nothing
|
|
*/
|
|
public function createRoutes(App $app): void
|
|
{
|
|
$app->post("/blog/post", function (Request $request, Response $response, array $args)
|
|
{
|
|
$data = $request->getParsedBody();
|
|
$files = $request->getUploadedFiles();
|
|
$headerImg = $files["headerImg"];
|
|
if (empty($data["title"]) || empty($data["body"]) || empty($data["dateCreated"]) || empty($data["featured"]) || empty($data["categories"]))
|
|
{
|
|
// uh oh sent some empty data
|
|
$response->getBody()->write(json_encode(array("error" => "Error, empty data sent")));
|
|
return $response->withStatus(400);
|
|
}
|
|
|
|
if (empty($files["headerImg"]))
|
|
{
|
|
$headerImg = null;
|
|
}
|
|
|
|
$insertedID = $this->blogData->createPost($data["title"], $data["body"], $data["dateCreated"], $data["featured"], $data["categories"], $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);
|
|
});
|
|
}
|
|
} |