Moved all current routes to individual folders which use a class system and inherit from an interface. Moved rest of the files for a better structure and for readability. Each editable cms should have its own folder with a routes class file and data class file

Signed-off-by: rodude123 <rodude123@gmail.com>
This commit is contained in:
2023-06-08 15:10:27 +01:00
parent fb75dd2255
commit a3eb4678f9
26 changed files with 1189 additions and 931 deletions
+175
View File
@@ -0,0 +1,175 @@
<?php
namespace api\timeline;
use PDO;
require_once __DIR__ . "/../utils/config.php";
/**
* TimelineData class
* Define all functions which either get, update, create or delete timeline data
*/
class timelineData
{
/**
* Get all education data
* @return array - Array of all education data or error message
*/
function getEduData(): array
{
$conn = dbConn();
$stmt = $conn->prepare("SELECT ID, startPeriod, endPeriod, grade, course FROM edu ORDER BY startPeriod DESC;");
$stmt->execute();
// set the resulting array to associative
$result = $stmt->fetchAll(PDO::FETCH_ASSOC);
if ($result)
{
return $result;
}
return array("errorMessage" => "Error, edu data not found");
}
/**
* Get all work data
* @return array - Array of all work data or error message
*/
function getWorkData(): array
{
$conn = dbConn();
$stmt = $conn->prepare("SELECT ID, startPeriod, endPeriod, companyName, area, title FROM work ORDER BY work.startPeriod DESC;");
$stmt->execute();
// set the resulting array to associative
$result = $stmt->fetchAll(PDO::FETCH_ASSOC);
if ($result)
{
return $result;
}
return array("errorMessage" => "Error, work data not found");
}
/**
* Update education data
* @param string $dateFrom - Start date
* @param string $dateTo - End date
* @param string $grade - Grade
* @param string $course - Course
* @param string $id - ID of the education data
* @return bool - True if successful, false if not
*/
function updateEduData(string $dateFrom, string $dateTo, string $grade, string $course, string $id): bool
{
$conn = dbConn();
$stmt = $conn->prepare("UPDATE edu SET startPeriod = :dateFrom, endPeriod = :dateTo, grade = :grade, course = :course WHERE ID = :id;");
$stmt->bindParam(":dateFrom", $dateFrom);
$stmt->bindParam(":dateTo", $dateTo);
$stmt->bindParam(":grade", $grade);
$stmt->bindParam(":course", $course);
$stmt->bindParam(":id", $id);
return $stmt->execute();
}
/**
* Update work data
* @param string $dateFrom - Start date
* @param string $dateTo - End date
* @param string $companyName - Company name
* @param string $area - Area
* @param string $title - Title
* @param string $id - ID of the work data
* @return bool - True if successful, false if not
*/
public function updateWorkData(string $dateFrom, string $dateTo, string $companyName, string $area, string $title, string $id): bool
{
$conn = dbConn();
$stmt = $conn->prepare("UPDATE work SET startPeriod = :dateFrom, endPeriod = :dateTo, companyName = :companyName, area = :area, title = :title WHERE ID = :id;");
$stmt->bindParam(":dateFrom", $dateFrom);
$stmt->bindParam(":dateTo", $dateTo);
$stmt->bindParam(":companyName", $companyName);
$stmt->bindParam(":area", $area);
$stmt->bindParam(":title", $title);
$stmt->bindParam(":id", $id);
return $stmt->execute();
}
/**
* Delete education data by ID
* @param int $id
* @return bool - True if successful, false if not
*/
function deleteEduData(int $id): bool
{
$conn = dbConn();
$stmt = $conn->prepare("DELETE FROM edu WHERE ID = :id;");
$stmt->bindParam(":id", $id);
return $stmt->execute();
}
/**
* Delete work data by ID
* @param int $id
* @return bool - True if successful, false if not
*/
function deleteWorkData(int $id): bool
{
$conn = dbConn();
$stmt = $conn->prepare("DELETE FROM work WHERE ID = :id;");
$stmt->bindParam(":id", $id);
return $stmt->execute();
}
/**
* Create new education data
* @param string $dateFrom - Start date
* @param string $dateTo - End date
* @param string $grade - Grade
* @param string $course - Course
* @return bool|int - ID of the new education data or false if not successful
*/
function addEduData(string $dateFrom, string $dateTo, string $grade, string $course): bool|int
{
$conn = dbConn();
$stmt = $conn->prepare("INSERT INTO edu (startPeriod, endPeriod, grade, course) VALUES (:dateFrom, :dateTo, :grade, :course);");
$stmt->bindParam(":dateFrom", $dateFrom);
$stmt->bindParam(":dateTo", $dateTo);
$stmt->bindParam(":grade", $grade);
$stmt->bindParam(":course", $course);
if($stmt->execute())
{
return $conn->lastInsertId();
}
return false;
}
/**
* Create new work data
* @param string $dateFrom - Start date
* @param string $dateTo - End date
* @param string $companyName - Company name
* @param string $area - Area
* @param string $title - Title
* @return bool|int - ID of the new work data if successful, false if not
*/
function addWorkData(string $dateFrom, string $dateTo, string $companyName, string $area, string $title): bool|int
{
$conn = dbConn();
$stmt = $conn->prepare("INSERT INTO work (startPeriod, endPeriod, companyName, area, title) VALUES (:dateFrom, :dateTo, :companyName, :area, :title);");
$stmt->bindParam(":dateFrom", $dateFrom);
$stmt->bindParam(":dateTo", $dateTo);
$stmt->bindParam(":companyName", $companyName);
$stmt->bindParam(":area", $area);
$stmt->bindParam(":title", $title);
if($stmt->execute())
{
return $conn->lastInsertId();
}
return false;
}
}
+176
View File
@@ -0,0 +1,176 @@
<?php
namespace api\timeline;
require_once __DIR__ . "/../utils/routesInterface.php";
require_once "timelineData.php";
use api\utils\routesInterface;
use Psr\Http\Message\ResponseInterface as Response;
use Psr\Http\Message\ServerRequestInterface as Request;
use Slim\App;
class timelineRoutes implements routesInterface
{
private timelineData $timelineData;
public function __construct(App $app)
{
$this->timelineData = new timelineData();
$this->createRoutes($app);
}
public function createRoutes(App $app): void
{
$app->get("/timelineData/{timeline}", function (Request $request, Response $response, array $args)
{
//check if route is available if it is get the data
//otherwise return an error
if($args["timeline"] == "edu")
{
$response->getBody()->write(json_encode($this->timelineData->getEduData()));
return $response;
}
if($args["timeline"] == "work")
{
$response->getBody()->write(json_encode($this->timelineData->getWorkData()));
return $response;
}
// something went wrong
$response->getBody()->write(json_encode(array("errorMessage" => "Error, timeline data not found")));
return $response->withStatus(404);
});
$app->patch("/timelineData/{timeline}/{id}", function (Request $request, Response $response, array $args)
{
$data = $request->getParsedBody();
if ($args["timeline"] == "edu" && $args["id"] != "undefined")
{
if (empty($data["dateFrom"]) || empty($data["dateTo"]) || empty($data["grade"]) || empty($data["course"]))
{
// 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 (!$this->timelineData->updateEduData($data["dateFrom"], $data["dateTo"], $data["grade"], $data["course"], $args["id"]))
{
// uh oh something went wrong
$response->getBody()->write(json_encode(array("error" => "Something went wrong")));
return $response->withStatus(500);
}
$response->withStatus(201);
return $response;
}
if ($args["timeline"] == "work" && $args["id"] != "undefined")
{
if (empty($data["dateFrom"]) || empty($data["dateTo"]) || empty($data["companyName"]) || empty($data["area"]) || empty($data["title"]))
{
// 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 (!$this->timelineData->updateWorkData($data["dateFrom"], $data["dateTo"], $data["companyName"], $data["area"], $data["title"], $args["id"]))
{
// uh oh something went wrong
$response->getBody()->write(json_encode(array("error" => "Something went wrong")));
return $response->withStatus(500);
}
$response->withStatus(201);
return $response;
}
$response->getBody()->write(json_encode(array("error" => "The correct data was not sent")));
return $response->withStatus(400);
});
$app->delete("/timelineData/{timeline}/{id}", function (Request $request, Response $response, array $args)
{
if ($args["timeline"] == "edu" && $args["id"] != null)
{
if (!$this->timelineData->deleteEduData($args["id"]))
{
// uh oh something went wrong
$response->getBody()->write(json_encode(array("error" => "Something went wrong")));
return $response->withStatus(500);
}
return $response;
}
if ($args["timeline"] == "work" && $args["id"] != null)
{
if (!$this->timelineData->deleteWorkData($args["id"]))
{
// uh oh something went wrong
$response->getBody()->write(json_encode(array("error" => "Something went wrong")));
return $response->withStatus(500);
}
return $response;
}
$response->getBody()->write(json_encode(array("error" => "The correct data was not sent")));
return $response->withStatus(400);
});
$app->post("/timelineData/{timeline}", function (Request $request, Response $response, array $args)
{
$data = $request->getParsedBody();
if ($args["timeline"] == "edu")
{
if (empty($data["dateFrom"]) || empty($data["dateTo"]) || empty($data["grade"]) || empty($data["course"]))
{
// uh oh sent some empty data
$response->getBody()->write(json_encode(array("error" => "Only some of the data was sent")));
return $response->withStatus(400);
}
$insertedID = $this->timelineData->addEduData($data["dateFrom"], $data["dateTo"], $data["grade"], $data["course"]);
if (!is_int($insertedID))
{
// uh oh something went wrong
$response->getBody()->write(json_encode(array("error" => "Something went wrong")));
return $response->withStatus(500);
}
$response->getBody()->write(json_encode(array("ID" => $insertedID)));
$response->withStatus(201);
return $response;
}
if ($args["timeline"] == "work")
{
if (empty($data["dateFrom"]) || empty($data["companyName"]) || empty($data["area"]) || empty($data["title"]))
{
// 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 (empty($data["dateTo"]))
{
$data["dateTo"] = "";
}
$insertedID = $this->timelineData->addWorkData($data["dateFrom"], $data["dateTo"], $data["companyName"], $data["area"], $data["title"]);
if (!is_int($insertedID))
{
// uh oh something went wrong
$response->getBody()->write(json_encode(array("error" => "Something went wrong")));
return $response->withStatus(500);
}
$response->getBody()->write(json_encode(array("ID" => $insertedID)));
$response->withStatus(201);
return $response;
}
$response->getBody()->write(json_encode(array("error" => "The correct data was not sent")));
return $response->withStatus(400);
});
}
}