diff --git a/src/main/java/com/myapps/flightdash/controller/FlightPlanController.java b/src/main/java/com/myapps/flightdash/controller/FlightPlanController.java index f044264..08c51da 100644 --- a/src/main/java/com/myapps/flightdash/controller/FlightPlanController.java +++ b/src/main/java/com/myapps/flightdash/controller/FlightPlanController.java @@ -14,7 +14,7 @@ import org.springframework.web.client.RestTemplate; @RestController -@RequestMapping("/api/v1/flightplan") +@RequestMapping("/api/v1") public class FlightPlanController { private final RestTemplate restTemplate; @@ -27,7 +27,7 @@ public FlightPlanController() { this.xmlMapper = new XmlMapper(); } - @GetMapping("/{pilotID}") + @GetMapping("flightplan/{pilotID}") public ResponseEntity getFlightPlan(@PathVariable String pilotID) { if (pilotID == null || pilotID.isEmpty()) { return ResponseEntity.badRequest().body("Invalid pilot ID"); @@ -52,4 +52,27 @@ public ResponseEntity getFlightPlan(@PathVariable String pilotID) { return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("An error occurred while processing your request."); } } + + @GetMapping("/flightplan-json/{pilotID}") + public ResponseEntity getFlightPlanJSON(@PathVariable String pilotID) { + if (pilotID == null || pilotID.isEmpty()) { + return ResponseEntity.badRequest().body("Invalid pilot ID"); + } + + try { + // Fetch JSON data directly from Simbrief using the "&json=1" parameter + String jsonData = restTemplate.getForObject("https://www.simbrief.com/api/xml.fetcher.php?userid=" + pilotID + "&json=1", String.class); + + // Return the JSON data + return ResponseEntity.ok(jsonMapper.readTree(jsonData)); + + } catch (HttpStatusCodeException e) { + // This will handle errors returned from the Simbrief API (e.g., 404, 500, etc.) + return ResponseEntity.status(e.getStatusCode()).body(e.getResponseBodyAsString()); + + } catch (Exception e) { + // For other exceptions + return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("An error occurred while processing your request."); + } + } } diff --git a/src/main/resources/static/index.html b/src/main/resources/static/index.html index bf157da..268291e 100644 --- a/src/main/resources/static/index.html +++ b/src/main/resources/static/index.html @@ -13,6 +13,8 @@ + + diff --git a/src/main/resources/static/script.js b/src/main/resources/static/script.js index 9ae605a..5ef1a82 100644 --- a/src/main/resources/static/script.js +++ b/src/main/resources/static/script.js @@ -12,6 +12,7 @@ app.controller('ChecklistController', ['$scope', '$sce', '$timeout', '$http', '$ $scope.icao = ''; $scope.simbriefPilotId = ''; $scope.flightPlanData = ''; + $scope.flightPlanJSONData = ''; $scope.flightPlanTrustedHtml = ''; $scope.airportData = {}; $scope.airportInfo = {}; @@ -909,7 +910,6 @@ $scope.$watch('calculatedBoardingDateTime', function(newVal, oldVal) { .then(function(response) { // Handle the returned data here $scope.flightPlanData = response.data; - console.log($scope.flightPlanData); if ($scope.flightPlanData && $scope.flightPlanData.text) { $scope.flightPlanTrustedHtml = $sce.trustAsHtml($scope.flightPlanData.text.plan_html); } @@ -917,6 +917,25 @@ $scope.$watch('calculatedBoardingDateTime', function(newVal, oldVal) { .catch(function(error) { console.error('Error fetching flight plan:', error); + // Display the toast with an error message + displaySimbriefErrorToast(); + }); + $http.get('/api/v1/flightplan-json/' + $scope.simbriefPilotId) + .then(function(response) { + // Handle the returned data here + $scope.flightPlanJSONData = response.data; + $scope.callSign = $scope.flightPlanJSONData.atc.callsign; + $scope.departureIcao = $scope.flightPlanJSONData.origin.icao_code; + $scope.arrivalIcao = $scope.flightPlanJSONData.destination.icao_code; + $scope.scheduledBoardingDateTime = moment.unix($scope.flightPlanJSONData.times.sched_out).toDate(); + $scope.scheduledDepartureDateTime = moment.unix($scope.flightPlanJSONData.times.sched_off).toDate(); + $scope.scheduledArrivalDateTime = moment.unix($scope.flightPlanJSONData.times.sched_on).toDate(); + $scope.scheduledGateArrivalDateTime = moment.unix($scope.flightPlanJSONData.times.sched_in).toDate(); + + }) + .catch(function(error) { + console.error('Error fetching flight plan:', error); + // Display the toast with an error message displaySimbriefErrorToast(); });