Added in ability to add a new project with and with an image and update a project with and without image. If a project is the main project then it cannot be deleted and when a new main project is selected it gets moved to the top.
Signed-off-by: rodude123 <rodude123@gmail.com>
This commit is contained in:
Vendored
+15
-7
@@ -218,17 +218,17 @@ $app->patch("/projectData/{id}", function (Request $request, Response $response,
|
||||
$data = $request->getParsedBody();
|
||||
if ($args["id"] != "undefined")
|
||||
{
|
||||
if (empty($data["title"]) || empty($data["isMainProject"]) || empty($data["information"]) || empty($data["projectLink"]) || empty($data["gitLink"]))
|
||||
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);
|
||||
}
|
||||
|
||||
if (!$projectData->updateProjectData($args["id"], $data["title"], $data["isMainProject"], $data["information"], "", $data["projectLink"], $data["gitLink"]))
|
||||
if (!$projectData->updateProjectData($args["id"], $data["title"], $data["isMainProject"], $data["information"], $data["projectLink"], $data["gitLink"]))
|
||||
{
|
||||
// uh oh something went wrong
|
||||
$response->getBody()->write(json_encode(array("error" => "Something went wrong")));
|
||||
$response->getBody()->write(json_encode(array("error" => "Something went wrong", "data" => $projectData->updateProjectData($args["id"], $data["title"], $data["isMainProject"], $data["information"], $data["projectLink"], $data["gitLink"]))));
|
||||
return $response->withStatus(500);
|
||||
}
|
||||
return $response;
|
||||
@@ -243,13 +243,21 @@ $app->delete("/projectData/{id}", function (Request $request, Response $response
|
||||
global $projectData;
|
||||
if ($args["id"] != null)
|
||||
{
|
||||
if (!$projectData->deleteProjectData($args["id"]))
|
||||
$message = $projectData->deleteProjectData($args["id"]);
|
||||
if ($message === "error")
|
||||
{
|
||||
// uh oh something went wrong
|
||||
$response->getBody()->write(json_encode(array("error" => "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;
|
||||
}
|
||||
|
||||
@@ -261,7 +269,7 @@ $app->post("/projectData", function (Request $request, Response $response)
|
||||
{
|
||||
global $projectData;
|
||||
$data = $request->getParsedBody();
|
||||
if (empty($data["title"]) || empty($data["isMainProject"]) || empty($data["information"]) || empty($data["projectLink"]) || empty($data["gitLink"]))
|
||||
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")));
|
||||
@@ -272,7 +280,7 @@ $app->post("/projectData", function (Request $request, Response $response)
|
||||
if (!is_int($insertedID))
|
||||
{
|
||||
// uh oh something went wrong
|
||||
$response->getBody()->write(json_encode(array("error" => "Something went wrong")));
|
||||
$response->getBody()->write(json_encode(array("error" => "Something went wrong", "message" => $insertedID)));
|
||||
return $response->withStatus(500);
|
||||
}
|
||||
|
||||
|
||||
Vendored
+62
-21
@@ -18,7 +18,7 @@ class projectData
|
||||
function getProjectData(): array
|
||||
{
|
||||
$conn = dbConn();
|
||||
$stmt = $conn->prepare("SELECT ID, title, isMainProject, information, imgLocation, projectLink, gitLink FROM projects;");
|
||||
$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
|
||||
@@ -37,52 +37,66 @@ 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 bool $isMainProject - Is the project a main project or not
|
||||
* @param string $isMainProject - Is the project a main project or not
|
||||
* @param string $information - Information about the project
|
||||
* @param string $imgLocation - Location of the image
|
||||
* @param string $projectLink - Link to the project
|
||||
* @param string $gitLink - Link to the github repository
|
||||
* @param string $gitLink - Link to the git repository
|
||||
* @return bool - True if project was updated, false if not and there was an error
|
||||
*/
|
||||
function updateProjectData(string $ID, string $title, bool $isMainProject, string $information, string $imgLocation, string $projectLink, string $gitLink): bool
|
||||
function updateProjectData(string $ID, string $title, string $isMainProject, string $information, string $projectLink, string $gitLink): bool
|
||||
{
|
||||
$conn = dbConn();
|
||||
$stmt = $conn->prepare("UPDATE projects SET title = :title, isMainProject = :isMainProject, information = :information, imgLocation = :imgLocation, projectLink = :projectLink, gitLink = :gitLink WHERE ID = :ID");
|
||||
|
||||
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);
|
||||
$stmt->bindParam(":isMainProject", $isMainProject);
|
||||
$isMainProj = ($isMainProject) ? 1 : 0;
|
||||
$stmt->bindParam(":isMainProject", $isMainProj);
|
||||
$stmt->bindParam(":information", $information);
|
||||
$stmt->bindParam(":imgLocation", $imgLocation);
|
||||
$stmt->bindParam(":projectLink", $projectLink);
|
||||
$stmt->bindParam(":gitLink", $gitLink);
|
||||
$stmt->bindParam(":ID", $ID);
|
||||
$stmt->execute();
|
||||
|
||||
if ($stmt->rowCount() > 0)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
return $stmt->execute();
|
||||
}
|
||||
|
||||
/**
|
||||
* Delete project data from the database
|
||||
* @param int $ID - ID of the project in the database to delete
|
||||
* @return bool - True if project was deleted, false if not and there was an error
|
||||
* @return string - True if project was deleted, false if not and there was an error
|
||||
*/
|
||||
function deleteProjectData(int $ID): bool
|
||||
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 true;
|
||||
return "ok";
|
||||
}
|
||||
|
||||
return false;
|
||||
return "error";
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -96,10 +110,18 @@ class projectData
|
||||
*/
|
||||
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);
|
||||
$stmt->bindParam(":isMainProject", $isMainProject);
|
||||
$isMainProj = ($isMainProject) ? 1 : 0;
|
||||
$stmt->bindParam(":isMainProject", $isMainProj);
|
||||
$stmt->bindParam(":information", $information);
|
||||
$stmt->bindParam(":projectLink", $projectLink);
|
||||
$stmt->bindParam(":gitLink", $gitLink);
|
||||
@@ -148,6 +170,7 @@ class projectData
|
||||
|
||||
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");
|
||||
@@ -165,4 +188,22 @@ class projectData
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user