diff --git a/src/api/index.php b/src/api/index.php index 0b100d3..8510f90 100644 --- a/src/api/index.php +++ b/src/api/index.php @@ -23,16 +23,36 @@ $app->setBasePath("/api"); $timelineData = new TimelineData(); -$app->get("/timelineData", function (Request $request, Response $response) +$app->get("/timelineData/{timeline}", function (Request $request, Response $response, array $args) { global $timelineData; + $json = $result = ""; - $result = $timelineData->getTimelineData(); + //check if route is available if it is get the data + //otherwise return an error + if($args["timeline"] == "edu") + { + $result = $timelineData->getEduData(); + } + else if($args["timeline"] == "work") + { + $result = $timelineData->getWorkData(); + } + else + { + $result = 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)) + { + $response = $response->withStatus(404); + } + //use content type json to indicate json data on frontend. return $response->withHeader("Content-Type", "application/json"); }); diff --git a/src/api/timelineData.php b/src/api/timelineData.php index fa9ce5b..f302187 100644 --- a/src/api/timelineData.php +++ b/src/api/timelineData.php @@ -3,10 +3,10 @@ require_once "./config.php"; class TimelineData { - function getTimelineData() + function getEduData() { $conn = dbConn(); - $stmt = $conn->prepare("SELECT * FROM timeline;"); + $stmt = $conn->prepare("SELECT * FROM edu;"); $stmt->execute(); // set the resulting array to associative @@ -18,8 +18,28 @@ class TimelineData } else { - return array("errorMessage" => "Error timeline data not found"); + return array("errorMessage" => "Error, edu data not found"); } - } + + function getWorkData() + { + $conn = dbConn(); + $stmt = $conn->prepare("SELECT * FROM work;"); + $stmt->execute(); + + // set the resulting array to associative + $result = $stmt->fetchAll(PDO::FETCH_ASSOC); + + if ($result) + { + return $result; + } + else + { + return array("errorMessage" => "Error, work data not found"); + } + } + + } \ No newline at end of file diff --git a/src/js/main.js b/src/js/main.js index dc653c2..0ba92e8 100644 --- a/src/js/main.js +++ b/src/js/main.js @@ -104,11 +104,11 @@ function StartTextAnimation(i) function getTimelineData() { - fetch("/api/timelineData").then(res => + fetch("/api/timelineData/edu").then(res => { res.json().then(json => { - console.log(JSON.parse(json)); + json }) }); }