2023-06-08 15:10:27 +01:00
|
|
|
<?php
|
2023-10-18 23:58:21 +01:00
|
|
|
|
2023-06-08 15:10:27 +01:00
|
|
|
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;
|
2023-06-09 14:50:20 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* constructor used to instantiate a base project routes, to be used in the index.php file.
|
|
|
|
* @param App $app - the slim app used to create the routes
|
|
|
|
*/
|
2023-06-08 15:10:27 +01:00
|
|
|
public function __construct(App $app)
|
|
|
|
{
|
|
|
|
$this->projectData = new projectData();
|
|
|
|
$this->createRoutes($app);
|
|
|
|
}
|
2023-06-09 14:50:20 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* creates the routes for the project
|
|
|
|
* @param App $app - the slim app used to create the routes
|
|
|
|
* @return void - returns nothing
|
|
|
|
*/
|
2023-06-08 15:10:27 +01:00
|
|
|
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);
|
|
|
|
|
2023-10-18 23:58:21 +01:00
|
|
|
if (array_key_exists("errorMessage", $result))
|
2023-06-08 15:10:27 +01:00
|
|
|
{
|
2023-07-12 03:29:56 +01:00
|
|
|
$response->withStatus(404);
|
2023-06-08 15:10:27 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
//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();
|
2023-11-14 01:02:27 +00:00
|
|
|
if ($args["id"] == null)
|
2023-06-08 15:10:27 +01:00
|
|
|
{
|
2023-11-14 01:02:27 +00:00
|
|
|
$response->getBody()->write(json_encode(array("error" => "Please provide an ID")));
|
|
|
|
return $response->withStatus(400);
|
2023-06-08 15:10:27 +01:00
|
|
|
}
|
|
|
|
|
2023-11-14 01:02:27 +00:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
$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")
|
|
|
|
{
|
|
|
|
// 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;
|
2023-06-08 15:10:27 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
$app->delete("/projectData/{id}", function (Request $request, Response $response, array $args)
|
|
|
|
{
|
2023-11-14 01:02:27 +00:00
|
|
|
if ($args["id"] == null)
|
|
|
|
{
|
|
|
|
$response->getBody()->write(json_encode(array("error" => "Please provide an ID")));
|
|
|
|
return $response->withStatus(400);
|
|
|
|
}
|
|
|
|
|
|
|
|
$message = $this->projectData->deleteProjectData($args["id"]);
|
|
|
|
|
|
|
|
if ($message === "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 ($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);
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($message === "error")
|
2023-06-08 15:10:27 +01:00
|
|
|
{
|
2023-11-14 01:02:27 +00:00
|
|
|
// uh oh something went wrong
|
|
|
|
$response->getBody()->write(json_encode(array("error" => "Something went wrong")));
|
|
|
|
return $response->withStatus(500);
|
2023-06-08 15:10:27 +01:00
|
|
|
}
|
|
|
|
|
2023-11-14 01:02:27 +00:00
|
|
|
return $response;
|
2023-06-08 15:10:27 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
$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")));
|
2023-06-26 03:54:25 +01:00
|
|
|
return $response->withStatus(400)->withStatus(201);
|
2023-06-08 15:10:27 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
$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)));
|
2023-06-26 03:54:25 +01:00
|
|
|
return $response->withStatus(201);
|
2023-06-08 15:10:27 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
$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));
|
2023-06-26 03:54:25 +01:00
|
|
|
return $response->withStatus(201);
|
2023-06-08 15:10:27 +01:00
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|