Created individual categories page
Signed-off-by: rodude123 <rodude123@gmail.com>
This commit is contained in:
Vendored
+8
-8
@@ -1,5 +1,7 @@
|
||||
<?php
|
||||
|
||||
namespace api\blog;
|
||||
|
||||
use api\utils\imgUtils;
|
||||
use DOMDocument;
|
||||
use PDO;
|
||||
@@ -125,7 +127,7 @@ class blogData
|
||||
public function getCategories(): array
|
||||
{
|
||||
$conn = dbConn();
|
||||
$stmt = $conn->prepare("SELECT categories FROM blog;");
|
||||
$stmt = $conn->prepare("SELECT DISTINCT categories FROM blog;");
|
||||
$stmt->execute();
|
||||
|
||||
// set the resulting array to associative
|
||||
@@ -415,7 +417,7 @@ class blogData
|
||||
}
|
||||
else
|
||||
{
|
||||
rename($from . $file,$to . $file);
|
||||
rename($from . $file, $to . $file);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -430,17 +432,15 @@ class blogData
|
||||
|
||||
/**
|
||||
* Get all posts with the given category
|
||||
* @param mixed $category - Category of the post
|
||||
* @param string $category - Category of the post
|
||||
* @return array - Array of all posts with the given category or error message
|
||||
*/
|
||||
public function getPostsByCategory(mixed $category)
|
||||
public function getPostsByCategory(string $category): array
|
||||
{
|
||||
$conn = dbConn();
|
||||
$stmt = $conn->prepare("SELECT * FROM blog WHERE categories = :category;");
|
||||
$stmt = $conn->prepare("SELECT * FROM blog WHERE LOCATE(:category, categories) > 0;");
|
||||
$stmt->bindParam(":category", $category);
|
||||
$stmt->execute();
|
||||
$result = $stmt->fetchAll(PDO::FETCH_ASSOC);
|
||||
|
||||
|
||||
return $stmt->fetchAll(PDO::FETCH_ASSOC);
|
||||
}
|
||||
}
|
||||
Vendored
+4
-3
@@ -12,6 +12,7 @@ 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
|
||||
@@ -134,14 +135,14 @@ class blogRoutes implements routesInterface
|
||||
return $response->withStatus(400);
|
||||
}
|
||||
|
||||
if (!preg_match('/(?:^|,)(?=[^"]|(")?)"?((?(1)(?:[^"]|"")*|[^,"]*))"?(?=,|$)/mx', $data["categories"]))
|
||||
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["dateModified"], $data["categories"]);
|
||||
$message = $this->blogData->updatePost($args["id"], $data["title"], intval($data["featured"]), $data["abstract"], $data["body"], $data["dateModified"], $data["categories"]);
|
||||
|
||||
if ($message === "post not found")
|
||||
{
|
||||
@@ -218,7 +219,7 @@ class blogRoutes implements routesInterface
|
||||
return $response->withStatus(400);
|
||||
}
|
||||
|
||||
if (!preg_match('/(?:^|,)(?=[^"]|(")?)"?((?(1)(?:[^"]|"")*|[^,"]*))"?(?=,|$)/mx', $data["categories"]))
|
||||
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")));
|
||||
|
||||
Reference in New Issue
Block a user