2023-06-08 15:10:27 +01:00
|
|
|
<?php
|
|
|
|
namespace api\timeline;
|
|
|
|
require_once __DIR__ . "/../utils/routesInterface.php";
|
|
|
|
require_once "timelineData.php";
|
|
|
|
|
|
|
|
use api\utils\routesInterface;
|
|
|
|
use Psr\Http\Message\ResponseInterface as Response;
|
|
|
|
use Psr\Http\Message\ServerRequestInterface as Request;
|
|
|
|
use Slim\App;
|
|
|
|
|
|
|
|
class timelineRoutes implements routesInterface
|
|
|
|
{
|
|
|
|
private timelineData $timelineData;
|
2023-06-09 14:50:20 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* constructor used to instantiate a base timeline 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->timelineData = new timelineData();
|
|
|
|
$this->createRoutes($app);
|
|
|
|
}
|
|
|
|
|
2023-06-09 14:50:20 +01:00
|
|
|
/**
|
|
|
|
* creates the routes for the timeline
|
|
|
|
* @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("/timelineData/{timeline}", function (Request $request, Response $response, array $args)
|
|
|
|
{
|
|
|
|
//check if route is available if it is get the data
|
|
|
|
//otherwise return an error
|
2023-06-09 14:50:20 +01:00
|
|
|
if ($args["timeline"] == "edu")
|
2023-06-08 15:10:27 +01:00
|
|
|
{
|
|
|
|
$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();
|
2023-07-12 03:29:56 +01:00
|
|
|
if ($args["timeline"] == "edu" && $args["id"] != null)
|
2023-06-08 15:10:27 +01:00
|
|
|
{
|
|
|
|
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);
|
|
|
|
}
|
2023-07-12 03:29:56 +01:00
|
|
|
$message = $this->timelineData->updateEduData($data["dateFrom"], $data["dateTo"], $data["grade"], $data["course"], $args["id"]);
|
|
|
|
|
|
|
|
if ($message == "not found")
|
|
|
|
{
|
|
|
|
// uh oh sent some empty data
|
|
|
|
$response->getBody()->write(json_encode(array("error" => "Edu data with ID " . $args["id"] . " was not found")));
|
|
|
|
return $response->withStatus(404);
|
|
|
|
}
|
2023-06-08 15:10:27 +01:00
|
|
|
|
2023-07-12 03:29:56 +01:00
|
|
|
if ($message == "error")
|
2023-06-08 15:10:27 +01:00
|
|
|
{
|
|
|
|
// 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);
|
|
|
|
}
|
|
|
|
|
2023-07-12 03:29:56 +01:00
|
|
|
$message = $this->timelineData->updateWorkData($data["dateFrom"], $data["dateTo"], $data["companyName"], $data["area"], $data["title"], $args["id"]);
|
|
|
|
|
|
|
|
if ($message == "not found")
|
|
|
|
{
|
|
|
|
// uh oh sent some empty data
|
|
|
|
$response->getBody()->write(json_encode(array("error" => "Work data with ID " . $args["id"] . " was not found")));
|
|
|
|
return $response->withStatus(404);
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($message == "error")
|
2023-06-08 15:10:27 +01:00
|
|
|
{
|
|
|
|
// 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)
|
|
|
|
{
|
2023-07-12 03:29:56 +01:00
|
|
|
$message = $this->timelineData->deleteEduData($args["id"]);
|
|
|
|
|
|
|
|
if ($message == "not found")
|
|
|
|
{
|
|
|
|
// uh oh sent some empty data
|
|
|
|
$response->getBody()->write(json_encode(array("error" => "Edu data with ID " . $args["id"] . " was not found")));
|
|
|
|
return $response->withStatus(404);
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($message == "error")
|
2023-06-08 15:10:27 +01:00
|
|
|
{
|
|
|
|
// 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)
|
|
|
|
{
|
2023-07-12 03:29:56 +01:00
|
|
|
$message = $this->timelineData->deleteWorkData($args["id"]);
|
|
|
|
|
|
|
|
if ($message == "not found")
|
|
|
|
{
|
|
|
|
// uh oh sent some empty data
|
|
|
|
$response->getBody()->write(json_encode(array("error" => "Work data with ID " . $args["id"] . " was not found")));
|
|
|
|
return $response->withStatus(404);
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($message == "error")
|
2023-06-08 15:10:27 +01:00
|
|
|
{
|
|
|
|
// 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);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|