Linked backend to frontend and fixed basic formatting in few files

This commit is contained in:
2021-12-28 21:20:00 +00:00
parent 9a282cb305
commit 9c4e487b11
12 changed files with 139 additions and 118 deletions
+35 -13
View File
@@ -3,14 +3,15 @@
/// Creates base routes and runs ///
/// respective functions ///
////////////////////////////////////////////
use Psr\Http\Message\ResponseInterface as Response;
use Psr\Http\Message\ServerRequestInterface as Request;
use Slim\Factory\AppFactory;
//require “routes.php”;
require "../vendor/autoload.php";
include "timelineData.php";
include "projectData.php";
use api\projectData;
use api\timelineData;
use Psr\Http\Message\ResponseInterface as Response;
use Psr\Http\Message\ServerRequestInterface as Request;
use Slim\Factory\AppFactory;
// Start slim
$app = AppFactory::create();
@@ -22,14 +23,15 @@ $errorMiddleware = $app->addErrorMiddleware(true, true, true);
$app->setBasePath("/api");
$timelineData = new TimelineData();
$timelineData = new timelineData();
$projectData = new projectData();
$app->get("/timelineData/{timeline}", function (Request $request, Response $response, array $args)
{
global $timelineData;
$json = $result = "";
//check if route is available if it is get the data
//check if route is available if it is get the data
//otherwise return an error
if($args["timeline"] == "edu")
{
@@ -39,20 +41,40 @@ $app->get("/timelineData/{timeline}", function (Request $request, Response $resp
{
$result = $timelineData->getWorkData();
}
else
else
{
$result = array(array("errorMessage" => "Error, timeline data not found"));
}
$json = json_encode($result);
$response->getBody()->write($json);
//if it is an error give a 404 code since it can't find the data
if(array_key_exists("errorMessage", $result[0]))
//if it is an error give a 403 code since it can't find the data
if(array_key_exists("errorMessage", $result[-1]))
{
$response = $response->withStatus(404);
$response = $response->withStatus(403);
}
//use content type json to indicate json data on frontend.
return $response->withHeader("Content-Type", "application/json");
});
$app->get('/projectData', function (Request $request, Response $response)
{
global $projectData;
$result= $projectData->getProjectData();
$json = json_encode($result);
$response->getBody()->write($json);
if(array_key_exists("errorMessage", $result[-1]))
{
$response = $response->withStatus(403);
}
//use content type json to indicate json data on frontend.
return $response->withHeader("Content-Type", "application/json");
});