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:
Vendored
+222
@@ -0,0 +1,222 @@
|
||||
<?php
|
||||
namespace api\project;
|
||||
use PDO;
|
||||
use Psr\Http\Message\UploadedFileInterface;
|
||||
|
||||
require_once __DIR__ . "/../utils/config.php";
|
||||
|
||||
/**
|
||||
* Project Data Class
|
||||
* Define all functions which either get, update, create or delete timeline data
|
||||
*/
|
||||
class projectData
|
||||
{
|
||||
/**
|
||||
* Get all project data
|
||||
* @return array - Array of all project data or error message
|
||||
*/
|
||||
function getProjectData(): array
|
||||
{
|
||||
$conn = dbConn();
|
||||
$stmt = $conn->prepare("SELECT ID, title, isMainProject, information, imgLocation, projectLink, gitLink FROM projects ORDER BY isMainProject DESC;");
|
||||
$stmt->execute();
|
||||
|
||||
// set the resulting array to associative
|
||||
$result = $stmt->fetchAll(PDO::FETCH_ASSOC);
|
||||
|
||||
if ($result)
|
||||
{
|
||||
return $result;
|
||||
}
|
||||
|
||||
return array("errorMessage" => "Error, project data not found");
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Update project data in the database with the given ID
|
||||
* @param string $ID - ID of the project in the database to update
|
||||
* @param string $title - Title of the project
|
||||
* @param string $isMainProject - Is the project a main project or not
|
||||
* @param string $information - Information about the project
|
||||
* @param string $projectLink - Link to the project
|
||||
* @param string $gitLink - Link to the git repository
|
||||
* @return bool|string - True if project was updated, false if not and there was an error, or an error string
|
||||
*/
|
||||
function updateProjectData(string $ID, string $title, string $isMainProject, string $information, string $projectLink, string $gitLink): bool | string
|
||||
{
|
||||
$conn = dbConn();
|
||||
|
||||
if ($isMainProject === "false")
|
||||
{
|
||||
$stmtMainProject = $conn->prepare("SELECT isMainProject FROM projects WHERE ID = :ID");
|
||||
$stmtMainProject->bindParam(":ID", $ID);
|
||||
$stmtMainProject->execute();
|
||||
$result = $stmtMainProject->fetch(PDO::FETCH_ASSOC);
|
||||
|
||||
if ($result["isMainProject"] === "1")
|
||||
{
|
||||
return "unset main project";
|
||||
}
|
||||
}
|
||||
|
||||
if ($isMainProject === "true")
|
||||
{
|
||||
$stmtMainProject = $conn->prepare("UPDATE projects SET isMainProject = 0;");
|
||||
$stmtMainProject->execute();
|
||||
}
|
||||
|
||||
$stmt = $conn->prepare("UPDATE projects SET title = :title, isMainProject = :isMainProject, information = :information, projectLink = :projectLink, gitLink = :gitLink WHERE ID = :ID");
|
||||
$stmt->bindParam(":title", $title);
|
||||
$isMainProj = ($isMainProject === "true") ? 1 : 0;
|
||||
$stmt->bindParam(":isMainProject", $isMainProj);
|
||||
$stmt->bindParam(":information", $information);
|
||||
$stmt->bindParam(":projectLink", $projectLink);
|
||||
$stmt->bindParam(":gitLink", $gitLink);
|
||||
$stmt->bindParam(":ID", $ID);
|
||||
return $stmt->execute();
|
||||
}
|
||||
|
||||
/**
|
||||
* Delete project data from the database
|
||||
* @param int $ID - ID of the project in the database to delete
|
||||
* @return string - True if project was deleted, false if not and there was an error
|
||||
*/
|
||||
function deleteProjectData(int $ID): string
|
||||
{
|
||||
$conn = dbConn();
|
||||
|
||||
// check if the project is a main project if it is return false
|
||||
|
||||
$stmtMainProject = $conn->prepare("SELECT isMainProject FROM projects WHERE ID = :ID");
|
||||
$stmtMainProject->bindParam(":ID", $ID);
|
||||
$stmtMainProject->execute();
|
||||
$result = $stmtMainProject->fetch(PDO::FETCH_ASSOC);
|
||||
|
||||
if ($result["isMainProject"] === "1")
|
||||
{
|
||||
return "cannot delete";
|
||||
}
|
||||
|
||||
$this->deleteImage($ID);
|
||||
|
||||
$stmt = $conn->prepare("DELETE FROM projects WHERE ID = :ID");
|
||||
$stmt->bindParam(":ID", $ID);
|
||||
$stmt->execute();
|
||||
|
||||
if ($stmt->rowCount() > 0)
|
||||
{
|
||||
return "ok";
|
||||
}
|
||||
|
||||
return "error";
|
||||
}
|
||||
|
||||
/**
|
||||
* Add project data to the database
|
||||
* @param string $title - Title of the project
|
||||
* @param string $isMainProject - Is the project a main project or not
|
||||
* @param string $information - Information about the project
|
||||
* @param string $projectLink - Link to the project
|
||||
* @param string $gitLink - Link to the github repository
|
||||
* @return int|bool - ID of the project if it was added, false if not and there was an error
|
||||
*/
|
||||
function addProjectData(string $title, string $isMainProject, string $information, string $projectLink, string $gitLink): int|bool
|
||||
{
|
||||
|
||||
$conn = dbConn();
|
||||
if ($isMainProject === "true")
|
||||
{
|
||||
$stmtMainProject = $conn->prepare("UPDATE projects SET isMainProject = 0;");
|
||||
$stmtMainProject->execute();
|
||||
}
|
||||
|
||||
$stmt = $conn->prepare("INSERT INTO projects (title, isMainProject, information, projectLink, gitLink) VALUES (:title, :isMainProject, :information, :projectLink, :gitLink)");
|
||||
$stmt->bindParam(":title", $title);
|
||||
$isMainProj = ($isMainProject === "true") ? 1 : 0;
|
||||
$stmt->bindParam(":isMainProject", $isMainProj);
|
||||
$stmt->bindParam(":information", $information);
|
||||
$stmt->bindParam(":projectLink", $projectLink);
|
||||
$stmt->bindParam(":gitLink", $gitLink);
|
||||
$stmt->execute();
|
||||
|
||||
if ($stmt->rowCount() > 0)
|
||||
{
|
||||
return $conn->lastInsertId();
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Upload the image to the server and update the database with the new image location
|
||||
* @param int $ID - ID of the project in the database to update
|
||||
* @param UploadedFileInterface $img - Image preview of the project
|
||||
* @return string|array - String with error message or array with the new image location
|
||||
*/
|
||||
public function uploadImage(int $ID, UploadedFileInterface $img): string | array
|
||||
{
|
||||
$targetDir = "../imgs/projects/";
|
||||
$targetFile = $targetDir . basename($img->getClientFilename());
|
||||
$uploadOk = 1;
|
||||
$imageFileType = strtolower(pathinfo($targetFile, PATHINFO_EXTENSION));
|
||||
|
||||
// Check if file already exists
|
||||
if (file_exists($targetFile))
|
||||
{
|
||||
return "The file already exists";
|
||||
}
|
||||
|
||||
// Check file size
|
||||
if ($img->getSize() > 2000000)
|
||||
{
|
||||
return "The file is too large, max 2MB";
|
||||
}
|
||||
|
||||
// Allow certain file formats
|
||||
if ($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg" && $imageFileType != "gif")
|
||||
{
|
||||
return "Only JPG, JPEG, PNG & GIF files are allowed";
|
||||
}
|
||||
|
||||
$img->moveTo($targetFile);
|
||||
|
||||
if (file_exists($targetFile))
|
||||
{
|
||||
$this->deleteImage($ID);
|
||||
// update the database with the new image location
|
||||
$conn = dbConn();
|
||||
$stmt = $conn->prepare("UPDATE projects SET imgLocation = :imgLocation WHERE ID = :ID");
|
||||
$stmt->bindParam(":imgLocation", $targetFile);
|
||||
$stmt->bindParam(":ID", $ID);
|
||||
$stmt->execute();
|
||||
|
||||
if ($stmt->rowCount() > 0)
|
||||
{
|
||||
return array("imgLocation" => $targetFile);
|
||||
}
|
||||
|
||||
return "Couldn't update the database";
|
||||
}
|
||||
|
||||
return "Couldn't upload the image";
|
||||
}
|
||||
|
||||
/**
|
||||
* Delete the image from the server
|
||||
* @param int $ID - ID of the project in the database
|
||||
*/
|
||||
private function deleteImage(int $ID): void
|
||||
{
|
||||
$conn = dbConn();
|
||||
$imgStmt = $conn->prepare("SELECT imgLocation FROM projects WHERE ID = :ID");
|
||||
$imgStmt->bindParam(":ID", $ID);
|
||||
$imgStmt->execute();
|
||||
$imgLocation = $imgStmt->fetch(PDO::FETCH_ASSOC)["imgLocation"];
|
||||
|
||||
if ($imgLocation != null)
|
||||
{
|
||||
unlink($imgLocation);
|
||||
}
|
||||
}
|
||||
}
|
||||
Vendored
+142
@@ -0,0 +1,142 @@
|
||||
<?php
|
||||
namespace api\project;
|
||||
require_once __DIR__ . "/../utils/routesInterface.php";
|
||||
require_once "projectData.php";
|
||||
|
||||
use api\utils\routesInterface;
|
||||
use Psr\Http\Message\ResponseInterface as Response;
|
||||
use Psr\Http\Message\ServerRequestInterface as Request;
|
||||
use Slim\App;
|
||||
|
||||
class projectRoutes implements routesInterface
|
||||
{
|
||||
private projectData $projectData;
|
||||
public function __construct(App $app)
|
||||
{
|
||||
$this->projectData = new projectData();
|
||||
$this->createRoutes($app);
|
||||
}
|
||||
public function createRoutes(App $app): void
|
||||
{
|
||||
$app->get("/projectData", function (Request $request, Response $response)
|
||||
{
|
||||
$result = $this->projectData->getProjectData();
|
||||
|
||||
$json = json_encode($result);
|
||||
|
||||
$response->getBody()->write($json);
|
||||
|
||||
if(array_key_exists("errorMessage", $result))
|
||||
{
|
||||
$response = $response->withStatus(404);
|
||||
}
|
||||
|
||||
//use content type json to indicate json data on frontend.
|
||||
return $response;
|
||||
});
|
||||
|
||||
$app->patch("/projectData/{id}", function (Request $request, Response $response, array $args)
|
||||
{
|
||||
$data = $request->getParsedBody();
|
||||
if ($args["id"] != "undefined")
|
||||
{
|
||||
if (empty($data["title"]) || empty($data["isMainProject"]) || empty($data["information"]) || empty($data["gitLink"]))
|
||||
{
|
||||
// uh oh sent some empty data
|
||||
$response->getBody()->write(json_encode(array("error" => "Only some of the data was sent")));
|
||||
return $response->withStatus(400);
|
||||
}
|
||||
|
||||
$update = $this->projectData->updateProjectData($args["id"], $data["title"], $data["isMainProject"], $data["information"], $data["projectLink"], $data["gitLink"]);
|
||||
|
||||
if ($update === "unset main project")
|
||||
{
|
||||
// uh oh something went wrong
|
||||
$response->getBody()->write(json_encode(array("error" => "Can't unset project as main project, try updating another project as the main project")));
|
||||
return $response->withStatus(400);
|
||||
}
|
||||
|
||||
if (!$update)
|
||||
{
|
||||
// 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" => "Please provide an ID")));
|
||||
return $response->withStatus(400);
|
||||
});
|
||||
|
||||
$app->delete("/projectData/{id}", function (Request $request, Response $response, array $args)
|
||||
{
|
||||
if ($args["id"] != null)
|
||||
{
|
||||
$message = $this->projectData->deleteProjectData($args["id"]);
|
||||
if ($message === "error")
|
||||
{
|
||||
// uh oh something went wrong
|
||||
$response->getBody()->write(json_encode(array("error" => "Something went wrong or the project with ID ".$args["id"]."does not exist")));
|
||||
return $response->withStatus(500);
|
||||
}
|
||||
|
||||
if ($message === "cannot delete")
|
||||
{
|
||||
//uh oh cannot delete the main project
|
||||
$response->getBody()->write(json_encode(array("error" => "Cannot delete the main project")));
|
||||
return $response->withStatus(409);
|
||||
}
|
||||
|
||||
return $response;
|
||||
}
|
||||
|
||||
$response->getBody()->write(json_encode(array("error" => "Please provide an ID")));
|
||||
return $response->withStatus(400);
|
||||
});
|
||||
|
||||
$app->post("/projectData", function (Request $request, Response $response)
|
||||
{
|
||||
$data = $request->getParsedBody();
|
||||
if (empty($data["title"]) || empty($data["isMainProject"]) || empty($data["information"]) || empty($data["gitLink"]))
|
||||
{
|
||||
// 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->projectData->addProjectData($data["title"], $data["isMainProject"], $data["information"], $data["projectLink"], $data["gitLink"]);
|
||||
if (!is_int($insertedID))
|
||||
{
|
||||
// uh oh something went wrong
|
||||
$response->getBody()->write(json_encode(array("error" => "Something went wrong", "message" => $insertedID)));
|
||||
return $response->withStatus(500);
|
||||
}
|
||||
|
||||
$response->getBody()->write(json_encode(array("ID" => $insertedID)));
|
||||
return $response;
|
||||
});
|
||||
|
||||
$app->post("/projectImage/{id}", function (Request $request, Response $response, array $args)
|
||||
{
|
||||
$files = $request->getUploadedFiles();
|
||||
if (empty($args["id"]) || empty($files))
|
||||
{
|
||||
// uh oh only some of the data was sent
|
||||
$response->getBody()->write(json_encode(array("error" => "Only some of the data was sent")));
|
||||
return $response->withStatus(400);
|
||||
}
|
||||
|
||||
$message = $this->projectData->uploadImage($args["id"], $files["img"]);
|
||||
if (!is_array($message))
|
||||
{
|
||||
// uh oh something went wrong
|
||||
$response->getBody()->write(json_encode(array("error" => $message)));
|
||||
return $response->withStatus(500);
|
||||
}
|
||||
|
||||
$response->getBody()->write(json_encode($message));
|
||||
return $response;
|
||||
});
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user