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; }); } }