From b6e5f09c11c116dfcfc3fcbd675b6a2f79e85153 Mon Sep 17 00:00:00 2001 From: Luke Else Date: Wed, 9 Feb 2022 20:19:32 +0000 Subject: [PATCH] Add Update route controller to fetch charts and adapt model to allow for response parsing --- Controllers/RouteController.cs | 40 +++++++++++++++++++++------------- Models/ChartModel.cs | 21 ++++++++++++++++++ 2 files changed, 46 insertions(+), 15 deletions(-) diff --git a/Controllers/RouteController.cs b/Controllers/RouteController.cs index ffc29b7..04492f7 100644 --- a/Controllers/RouteController.cs +++ b/Controllers/RouteController.cs @@ -75,21 +75,7 @@ namespace EFB.Controllers //Make initial Route Request var requestRoute = API.Post("https://api.autorouter.aero/v1.0/router", headerData, content); - //Prepare data to be send off with request(charts) - headerData = new Dictionary(); - headerData.Add("referer", "luke-else.co.uk"); - - Dictionary formData = new Dictionary(); - formData.Add("token", ""); - var body = new FormUrlEncodedContent(formData); - - //make Charts request - var requestDepartureCharts = API.Post($"https://chartfox.org/api/charts/grouped/{departure}", headerData, body); - var requestArrivalCharts = API.Post($"https://chartfox.org/api/charts/grouped/{arrival}", headerData, body); - ResponseModel responseRoute = await requestRoute; - ResponseModel responseDepartureCharts = await requestDepartureCharts; - ResponseModel responseArrivalCharts = await requestArrivalCharts; if (responseRoute.Error == null) {//Update User session and add route ID @@ -137,8 +123,25 @@ namespace EFB.Controllers string routeString = ""; APIInterface API = new APIInterface(); - Dictionary headerData = new Dictionary(); + + /*-----Chart Fetching Operations--------*/ + + //Prepare data to be send off with request(charts) + headerData = new Dictionary(); + headerData.Add("referer", "luke-else.co.uk"); + + Dictionary formData = new Dictionary(); + formData.Add("token", ""); + var body = new FormUrlEncodedContent(formData); + + //make Charts request + var requestDepartureCharts = API.Post($"https://chartfox.org/api/charts/grouped/{departure}", headerData, body); + var requestArrivalCharts = API.Post($"https://chartfox.org/api/charts/grouped/{arrival}", headerData, body); + + /*----------------------------------*/ + + //Run route Polling headerData.Add("Authorization", $"Bearer {user.UserToken.TokenValue}"); while (collected == false && pollCount < 15) @@ -165,6 +168,13 @@ namespace EFB.Controllers if (collected) { + //Get Response from Charts + ResponseModel responseDepartureCharts = await requestDepartureCharts; + user.DepartureCharts = new ChartModel(responseDepartureCharts.Result.Response); + + ResponseModel responseArrivalCharts = await requestArrivalCharts; + user.ArrivalCharts = new ChartModel(responseArrivalCharts.Result.Response); + //fill in route string finalRoute = RouteModel.ParseRoute(routeString); user.Route = finalRoute; diff --git a/Models/ChartModel.cs b/Models/ChartModel.cs index ecec2bd..b62e8c1 100644 --- a/Models/ChartModel.cs +++ b/Models/ChartModel.cs @@ -16,6 +16,26 @@ namespace EFB.Models public Chart[] Approach { get; set; } public Chart[] Transition { get; set; } public Chart[] PilotBriefing { get; set; } + + public ChartModel(ChartList response) + { + General = FillChart(response.General); + TextualData = FillChart(response.TextualData); + GroundLayout = FillChart(response.GroundLayout); + SID = FillChart(response.SID); + STAR = FillChart(response.STAR); + Approach = FillChart(response.Approach); + Transition = FillChart(response.Transition); + PilotBriefing = FillChart(response.PilotBriefing); + } + + private Chart[] FillChart(ChartsCollection collection){ + if (collection != null) + { + return collection.Charts; + } + return new Chart[]{}; + } } public class Pinned @@ -33,4 +53,5 @@ namespace EFB.Models // public string PseudoURL { get; set; } // public string URL { get; set; } // } + } \ No newline at end of file