Added in ability to add a new project with and without an image

Signed-off-by: rodude123 <rodude123@gmail.com>
This commit is contained in:
2023-02-06 01:26:35 +00:00
parent db7c12857e
commit fd64eb92b0
10 changed files with 413 additions and 91 deletions
+114 -29
View File
@@ -1,6 +1,7 @@
<?php
namespace api;
use PDO;
use Psr\Http\Message\UploadedFileInterface;
require_once "./config.php";
@@ -17,7 +18,7 @@ class projectData
function getProjectData(): array
{
$conn = dbConn();
$stmt = $conn->prepare("SELECT title, isMainProject, information, imgLocation, projectLink, gitLink FROM projects order by date LIMIT 4;");
$stmt = $conn->prepare("SELECT ID, title, isMainProject, information, imgLocation, projectLink, gitLink FROM projects;");
$stmt->execute();
// set the resulting array to associative
@@ -27,57 +28,141 @@ class projectData
{
return $result;
}
return array("errorMessage" => "Error, project data not found");
}
function updateProjectData(string $title, string $isMainProject, string $information, string $projectLink, string $githubLink, string $id): bool
/**
* 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 $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
* @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
{
$conn = dbConn();
$stmt = $conn->prepare("UPDATE projects SET title = :title, isMainProject = :isMainProject, information = :information, projectLink = :projectLink, githubLink = :githubLink WHERE ID = :id");
$stmt = $conn->prepare("UPDATE projects SET title = :title, isMainProject = :isMainProject, information = :information, imgLocation = :imgLocation, projectLink = :projectLink, gitLink = :gitLink WHERE ID = :ID");
$stmt->bindParam(":title", $title);
$stmt->bindParam(":isMainProject", $isMainProject);
$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;
}
/**
* 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
*/
function deleteProjectData(int $ID): bool
{
$conn = dbConn();
$stmt = $conn->prepare("DELETE FROM projects WHERE ID = :ID");
$stmt->bindParam(":ID", $ID);
$stmt->execute();
if ($stmt->rowCount() > 0)
{
return true;
}
return false;
}
/**
* 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();
$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);
$stmt->bindParam(":information", $information);
$stmt->bindParam(":projectLink", $projectLink);
$stmt->bindParam(":githubLink", $githubLink);
$stmt->bindParam(":id", $id);
$stmt->bindParam(":gitLink", $gitLink);
$stmt->execute();
if ($stmt->rowCount() > 0)
{
return true;
return $conn->lastInsertId();
}
return false;
}
function deleteProjectData(int $id): bool
/**
* 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
{
$conn = dbConn();
$stmt = $conn->prepare("DELETE FROM projects WHERE ID = :id");
$stmt->bindParam(":id", $id);
$stmt->execute();
$targetDir = "../imgs/projects/";
$targetFile = $targetDir . basename($img->getClientFilename());
$uploadOk = 1;
$imageFileType = strtolower(pathinfo($targetFile, PATHINFO_EXTENSION));
if ($stmt->rowCount() > 0)
// Check if file already exists
if (file_exists($targetFile))
{
return true;
return "The file already exists";
}
return false;
}
function addProjectData(string $title, string $isMainProject, string $information, string $projectLink, string $githubLink): bool
{
$conn = dbConn();
$stmt = $conn->prepare("INSERT INTO projects (title, isMainProject, information, projectLink, githubLink) VALUES (:title, :isMainProject, :information, :projectLink, :githubLink)");
$stmt->bindParam(":title", $title);
$stmt->bindParam(":isMainProject", $isMainProject);
$stmt->bindParam(":information", $information);
$stmt->bindParam(":projectLink", $projectLink);
$stmt->bindParam(":githubLink", $githubLink);
$stmt->execute();
if ($stmt->rowCount() > 0)
// Check file size
if ($img->getSize() > 2000000)
{
return true;
return "The file is too large, max 2MB";
}
return false;
// 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))
{
// 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";
}
}