Added the ability to edit and delete posts which includes uploading of images for the posts and managing those images
Signed-off-by: rodude123 <rodude123@gmail.com>
This commit is contained in:
Vendored
+35
-16
@@ -39,30 +39,32 @@ class projectData
|
||||
* 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 bool $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
|
||||
*/
|
||||
public function updateProjectData(string $ID, string $title, string $isMainProject, string $information, string $projectLink, string $gitLink): bool|string
|
||||
public function updateProjectData(string $ID, string $title, bool $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);
|
||||
$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 (!$result)
|
||||
{
|
||||
return "project not found";
|
||||
}
|
||||
|
||||
if ($isMainProject === "true")
|
||||
if (!$isMainProject && $result["isMainProject"] === "1")
|
||||
{
|
||||
return "unset main project";
|
||||
}
|
||||
|
||||
if ($isMainProject)
|
||||
{
|
||||
$stmtMainProject = $conn->prepare("UPDATE projects SET isMainProject = 0 WHERE isMainProject = 1;");
|
||||
$stmtMainProject->execute();
|
||||
@@ -70,7 +72,7 @@ class projectData
|
||||
|
||||
$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;
|
||||
$isMainProj = $isMainProject ? 1 : 0;
|
||||
$stmt->bindParam(":isMainProject", $isMainProj);
|
||||
$stmt->bindParam(":information", $information);
|
||||
$stmt->bindParam(":projectLink", $projectLink);
|
||||
@@ -89,12 +91,16 @@ class projectData
|
||||
$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)
|
||||
{
|
||||
return "project not found";
|
||||
}
|
||||
|
||||
if ($result["isMainProject"] === "1")
|
||||
{
|
||||
return "cannot delete";
|
||||
@@ -158,6 +164,20 @@ class projectData
|
||||
*/
|
||||
public function uploadImage(int $ID, UploadedFileInterface $img): string | array
|
||||
{
|
||||
|
||||
$conn = dbConn();
|
||||
|
||||
$stmt = $conn->prepare("SELECT ID FROM projects WHERE ID = :ID");
|
||||
$stmt->bindParam(":ID", $ID);
|
||||
$stmt->execute();
|
||||
$result = $stmt->fetch(PDO::FETCH_ASSOC);
|
||||
|
||||
if (!$result)
|
||||
{
|
||||
return "Project with ID $ID not found";
|
||||
}
|
||||
|
||||
|
||||
$targetDir = "../imgs/projects/";
|
||||
$imgUtils = new imgUtils();
|
||||
$targetFile = $imgUtils->uploadFile($targetDir, $img);
|
||||
@@ -171,7 +191,6 @@ class projectData
|
||||
{
|
||||
$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["imgLocation"]);
|
||||
$stmt->bindParam(":ID", $ID);
|
||||
|
||||
Vendored
+23
-6
@@ -39,7 +39,7 @@ class projectRoutes implements routesInterface
|
||||
|
||||
if(array_key_exists("errorMessage", $result))
|
||||
{
|
||||
$response = $response->withStatus(404);
|
||||
$response->withStatus(404);
|
||||
}
|
||||
|
||||
//use content type json to indicate json data on frontend.
|
||||
@@ -49,7 +49,7 @@ class projectRoutes implements routesInterface
|
||||
$app->patch("/projectData/{id}", function (Request $request, Response $response, array $args)
|
||||
{
|
||||
$data = $request->getParsedBody();
|
||||
if ($args["id"] != "undefined")
|
||||
if ($args["id"] != null)
|
||||
{
|
||||
if (empty($data["title"]) || empty($data["isMainProject"]) || empty($data["information"]) || empty($data["gitLink"]))
|
||||
{
|
||||
@@ -58,7 +58,15 @@ class projectRoutes implements routesInterface
|
||||
return $response->withStatus(400);
|
||||
}
|
||||
|
||||
$update = $this->projectData->updateProjectData($args["id"], $data["title"], $data["isMainProject"], $data["information"], $data["projectLink"], $data["gitLink"]);
|
||||
$isMainProject = $data["isMainProject"] === "true";
|
||||
$update = $this->projectData->updateProjectData($args["id"], $data["title"], $isMainProject, $data["information"], $data["projectLink"], $data["gitLink"]);
|
||||
|
||||
if ($update === "project not found")
|
||||
{
|
||||
// uh oh something went wrong
|
||||
$response->getBody()->write(json_encode(array("error" => "Project with ID " . $args["id"] . " not found")));
|
||||
return $response->withStatus(404);
|
||||
}
|
||||
|
||||
if ($update === "unset main project")
|
||||
{
|
||||
@@ -73,6 +81,7 @@ class projectRoutes implements routesInterface
|
||||
$response->getBody()->write(json_encode(array("error" => "Something went wrong")));
|
||||
return $response->withStatus(500);
|
||||
}
|
||||
|
||||
return $response;
|
||||
}
|
||||
|
||||
@@ -85,11 +94,12 @@ class projectRoutes implements routesInterface
|
||||
if ($args["id"] != null)
|
||||
{
|
||||
$message = $this->projectData->deleteProjectData($args["id"]);
|
||||
if ($message === "error")
|
||||
|
||||
if ($message === "project not found")
|
||||
{
|
||||
// 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);
|
||||
$response->getBody()->write(json_encode(array("error" => "Project with ID " . $args["id"] . " not found")));
|
||||
return $response->withStatus(404);
|
||||
}
|
||||
|
||||
if ($message === "cannot delete")
|
||||
@@ -99,6 +109,13 @@ class projectRoutes implements routesInterface
|
||||
return $response->withStatus(409);
|
||||
}
|
||||
|
||||
if ($message === "error")
|
||||
{
|
||||
// uh oh something went wrong
|
||||
$response->getBody()->write(json_encode(array("error" => "Something went wrong")));
|
||||
return $response->withStatus(500);
|
||||
}
|
||||
|
||||
return $response;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user