timelineData = new timelineData(); $this->createRoutes($app); } /** * creates the routes for the timeline * @param App $app - the slim app used to create the routes * @return void - returns nothing */ public function createRoutes(App $app): void { $app->get("/timelineData/{timeline}", function (Request $request, Response $response, array $args) { //check if route is available if it is get the data //otherwise return an error if ($args["timeline"] == "edu") { $response->getBody()->write(json_encode($this->timelineData->getEduData())); return $response; } if($args["timeline"] == "work") { $response->getBody()->write(json_encode($this->timelineData->getWorkData())); return $response; } // something went wrong $response->getBody()->write(json_encode(array("errorMessage" => "Error, timeline data not found"))); return $response->withStatus(404); }); $app->patch("/timelineData/{timeline}/{id}", function (Request $request, Response $response, array $args) { $data = $request->getParsedBody(); if ($args["timeline"] == "edu" && $args["id"] != "undefined") { if (empty($data["dateFrom"]) || empty($data["dateTo"]) || empty($data["grade"]) || empty($data["course"])) { // 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 (!$this->timelineData->updateEduData($data["dateFrom"], $data["dateTo"], $data["grade"], $data["course"], $args["id"])) { // uh oh something went wrong $response->getBody()->write(json_encode(array("error" => "Something went wrong"))); return $response->withStatus(500); } $response->withStatus(201); return $response; } if ($args["timeline"] == "work" && $args["id"] != "undefined") { if (empty($data["dateFrom"]) || empty($data["dateTo"]) || empty($data["companyName"]) || empty($data["area"]) || empty($data["title"])) { // 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 (!$this->timelineData->updateWorkData($data["dateFrom"], $data["dateTo"], $data["companyName"], $data["area"], $data["title"], $args["id"])) { // uh oh something went wrong $response->getBody()->write(json_encode(array("error" => "Something went wrong"))); return $response->withStatus(500); } $response->withStatus(201); return $response; } $response->getBody()->write(json_encode(array("error" => "The correct data was not sent"))); return $response->withStatus(400); }); $app->delete("/timelineData/{timeline}/{id}", function (Request $request, Response $response, array $args) { if ($args["timeline"] == "edu" && $args["id"] != null) { if (!$this->timelineData->deleteEduData($args["id"])) { // uh oh something went wrong $response->getBody()->write(json_encode(array("error" => "Something went wrong"))); return $response->withStatus(500); } return $response; } if ($args["timeline"] == "work" && $args["id"] != null) { if (!$this->timelineData->deleteWorkData($args["id"])) { // 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" => "The correct data was not sent"))); return $response->withStatus(400); }); $app->post("/timelineData/{timeline}", function (Request $request, Response $response, array $args) { $data = $request->getParsedBody(); if ($args["timeline"] == "edu") { if (empty($data["dateFrom"]) || empty($data["dateTo"]) || empty($data["grade"]) || empty($data["course"])) { // 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->timelineData->addEduData($data["dateFrom"], $data["dateTo"], $data["grade"], $data["course"]); if (!is_int($insertedID)) { // uh oh something went wrong $response->getBody()->write(json_encode(array("error" => "Something went wrong"))); return $response->withStatus(500); } $response->getBody()->write(json_encode(array("ID" => $insertedID))); $response->withStatus(201); return $response; } if ($args["timeline"] == "work") { if (empty($data["dateFrom"]) || empty($data["companyName"]) || empty($data["area"]) || empty($data["title"])) { // 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 (empty($data["dateTo"])) { $data["dateTo"] = ""; } $insertedID = $this->timelineData->addWorkData($data["dateFrom"], $data["dateTo"], $data["companyName"], $data["area"], $data["title"]); if (!is_int($insertedID)) { // uh oh something went wrong $response->getBody()->write(json_encode(array("error" => "Something went wrong"))); return $response->withStatus(500); } $response->getBody()->write(json_encode(array("ID" => $insertedID))); $response->withStatus(201); return $response; } $response->getBody()->write(json_encode(array("error" => "The correct data was not sent"))); return $response->withStatus(400); }); } }