From ec5ca5160203f42ea57fd119f3b91fd3075ef57d Mon Sep 17 00:00:00 2001 From: Luke Else Date: Fri, 18 Feb 2022 22:01:53 +0000 Subject: [PATCH] Added double negation to clean code --- Controllers/FlightsimController.cs | 10 +- Controllers/RouteController.cs | 253 ++++++++++++++--------------- Controllers/UserController.cs | 99 ++++++----- 3 files changed, 178 insertions(+), 184 deletions(-) diff --git a/Controllers/FlightsimController.cs b/Controllers/FlightsimController.cs index f4468e3..de82f7b 100644 --- a/Controllers/FlightsimController.cs +++ b/Controllers/FlightsimController.cs @@ -26,10 +26,16 @@ namespace EFB.Controllers { //Retrieve and Check current user status UserModel user = HttpContext.Session.GetObject("User"); - if(user == null) return RedirectToAction("Index", "Home"); + if(user == null){ + TempData["Error"] = "You must be logged in before you are able to view the FlightSim Page"; + return RedirectToAction("Index", "Home"); + } //Retrieve the user's latest sim position and construct it into FlightsimModel - if (user.Route == null) return RedirectToAction("Index", "Route"); + if (user.Route == null){ + TempData["Error"] = "You must have a route planned before you are able to view the Flightsim page"; + return RedirectToAction("Index", "Route"); + } SimPositionModel latestPositionModel = await Mongo.GetLatestData(user.EMail); diff --git a/Controllers/RouteController.cs b/Controllers/RouteController.cs index fc54f07..e948e20 100644 --- a/Controllers/RouteController.cs +++ b/Controllers/RouteController.cs @@ -46,157 +46,150 @@ namespace EFB.Controllers public async Task New(string departure, string arrival, string cruise) { UserModel user = HttpContext.Session.GetObject("User"); - if (!(user == null || user.UserToken.IsExpired())) + if (user == null || user.UserToken.IsExpired()) {//If the user is still authenticated - if (FormAuthenticator.ValidateICAOCode(departure) && FormAuthenticator.ValidateICAOCode(arrival)) - {//If the user has entered valid ICAOs + return RedirectToAction("Index", "Home"); + } - uint cruiseAlt; + if (!FormAuthenticator.ValidateICAOCode(departure) || !FormAuthenticator.ValidateICAOCode(arrival)) + {//If the user has entered valid ICAOs + TempData["Error"] = "Invalid Departure or Arrival ICAO"; + return RedirectToAction("Index", "Route"); + } + + uint cruiseAlt; - if (uint.TryParse(cruise, out cruiseAlt) && FormAuthenticator.ValidateCruiseAlt(cruiseAlt)) - {//If the cruise altitude if within limits. + if (uint.TryParse(cruise, out cruiseAlt) && FormAuthenticator.ValidateCruiseAlt(cruiseAlt)) + {//If the cruise altitude if within limits. - //Submit route request... - APIInterface API = new APIInterface(); + //Submit route request... + APIInterface API = new APIInterface(); - //Prepare data to be send off with request (route) - Dictionary headerData = new Dictionary(); - headerData.Add("Authorization", $"Bearer {user.UserToken.TokenValue}"); + //Prepare data to be send off with request (route) + Dictionary headerData = new Dictionary(); + headerData.Add("Authorization", $"Bearer {user.UserToken.TokenValue}"); - RouteRequest routeRequest = new RouteRequest() - { - departure = departure, - destination = arrival, - preferredminlevel = cruiseAlt / 1000, - preferredmaxlevel = cruiseAlt / 1000, - }; - StringContent content = new StringContent(JsonConvert.SerializeObject(routeRequest), Encoding.UTF8, "application/json"); - - //Make initial Route Request - var requestRoute = API.Post("https://api.autorouter.aero/v1.0/router", headerData, content); + RouteRequest routeRequest = new RouteRequest() + { + departure = departure, + destination = arrival, + preferredminlevel = cruiseAlt / 1000, + preferredmaxlevel = cruiseAlt / 1000, + }; + StringContent content = new StringContent(JsonConvert.SerializeObject(routeRequest), Encoding.UTF8, "application/json"); + + //Make initial Route Request + var requestRoute = API.Post("https://api.autorouter.aero/v1.0/router", headerData, content); - ResponseModel responseRoute = await requestRoute; + ResponseModel responseRoute = await requestRoute; - if (responseRoute.Error == null) - {//Update User session and add route ID - TokenModel routeToken = new TokenModel() - { - TokenValue = responseRoute.Result.ToString() - }; + if (responseRoute.Error == null) + {//Update User session and add route ID + TokenModel routeToken = new TokenModel() + { + TokenValue = responseRoute.Result.ToString() + }; - user.RouteToken = routeToken; - HttpContext.Session.SetObject("User", user); + user.RouteToken = routeToken; + HttpContext.Session.SetObject("User", user); - return await Poll(departure, arrival, cruiseAlt); - - } - - TempData["Error"] = responseRoute.Error; - return RedirectToAction("Index", "Route"); - - } - TempData["Error"] = "Invalid Cruise Altitude"; - TempData["Departure"] = departure; - TempData["Arrival"] = arrival; - return RedirectToAction("Index", "Route"); + return await Poll(departure, arrival, cruiseAlt); } - TempData["Error"] = "Invalid Departure or Arrival ICAO"; + + TempData["Error"] = responseRoute.Error; return RedirectToAction("Index", "Route"); + } - return RedirectToAction("Index", "Home"); + TempData["Error"] = "Invalid Cruise Altitude"; + TempData["Departure"] = departure; + TempData["Arrival"] = arrival; + return RedirectToAction("Index", "Route"); } public async Task Poll(string departure, string arrival, uint cruise) { - if (HttpContext.Session.GetString("User") != null) - {//If the user is currently logged in - UserModel user = HttpContext.Session.GetObject("User"); - - if (user.RouteToken != null) - {//If the user has a route object (e.g, they have been to the route page) - - //Make calls to the server to fetch route - bool collected = false; - int pollCount = 0; - string routeString = ""; - - APIInterface API = new APIInterface(); - Dictionary headerData = new Dictionary(); - - /*-----Chart Fetching Operations--------*/ - - var requestDepartureCharts = ChartModel.FetchAsync(departure); - var requestArrivalCharts = ChartModel.FetchAsync(arrival); - - /*----------------------------------*/ - - //Run route Polling - headerData.Add("Authorization", $"Bearer {user.UserToken.TokenValue}"); - - while (collected == false && pollCount < 15) - { - //Make Polling Request - var pollingRequest = API.Put>($"https://api.autorouter.aero/v1.0/router/{user.RouteToken.TokenValue}/longpoll", headerData, null); - - ResponseModel> responsePoll = await pollingRequest; - - foreach (var item in responsePoll.Result) - { - if (item.Command == "notvalid" || item.Command == "solution") - { - collected = true; - routeString = item.FlightPlan; - break; - } - } - - Thread.Sleep(3000); - pollCount++; - - } - - if (collected) - { - //Get Response from Charts - ChartList departureCharts = await requestDepartureCharts; - if (departureCharts != null) - { - user.DepartureCharts = new ChartModel(departure, departureCharts); - } - - ChartList arrivalCharts = await requestArrivalCharts; - if (arrivalCharts != null) - { - user.ArrivalCharts = new ChartModel(arrival, arrivalCharts); - } - - //fill in route - string finalRoute = RouteModel.ParseRoute(routeString); - user.Route = finalRoute; - user.Departure = departure; - user.Arrival = arrival; - user.Cruise = cruise; - HttpContext.Session.SetObject("User", user); - - return RedirectToAction("Index", "Route"); - } - - TempData["Error"] = $"Unable to get route after {pollCount} Attempts!"; - return RedirectToAction("Index", "Route"); - - } - else - { - return RedirectToAction("Index", "Route"); - } - - } - else - { + if (HttpContext.Session.GetString("User") == null) + {//If the user is not currently logged in + TempData["Error"] = "Please login before trying to plan a route"; return RedirectToAction("Index", "Route"); } + + UserModel user = HttpContext.Session.GetObject("User"); + + if (user.RouteToken == null) + {//If the user has a route object (e.g, they have been to the route page) + return RedirectToAction("Index", "Route"); + } + //Make calls to the server to fetch route + bool collected = false; + int pollCount = 0; + string routeString = ""; + + APIInterface API = new APIInterface(); + Dictionary headerData = new Dictionary(); + + /*-----Chart Fetching Operations--------*/ + + var requestDepartureCharts = ChartModel.FetchAsync(departure); + var requestArrivalCharts = ChartModel.FetchAsync(arrival); + + /*----------------------------------*/ + + //Run route Polling + headerData.Add("Authorization", $"Bearer {user.UserToken.TokenValue}"); + + while (collected == false && pollCount < 15) + { + //Make Polling Request + var pollingRequest = API.Put>($"https://api.autorouter.aero/v1.0/router/{user.RouteToken.TokenValue}/longpoll", headerData, null); + + ResponseModel> responsePoll = await pollingRequest; + + foreach (var item in responsePoll.Result) + { + if (item.Command == "notvalid" || item.Command == "solution") + { + collected = true; + routeString = item.FlightPlan; + break; + } + } + + Thread.Sleep(3000); + pollCount++; + + } + + if (collected) + { + //Get Response from Charts + ChartList departureCharts = await requestDepartureCharts; + if (departureCharts != null) + { + user.DepartureCharts = new ChartModel(departure, departureCharts); + } + + ChartList arrivalCharts = await requestArrivalCharts; + if (arrivalCharts != null) + { + user.ArrivalCharts = new ChartModel(arrival, arrivalCharts); + } + + //fill in route + string finalRoute = RouteModel.ParseRoute(routeString); + user.Route = finalRoute; + user.Departure = departure; + user.Arrival = arrival; + user.Cruise = cruise; + HttpContext.Session.SetObject("User", user); + + return RedirectToAction("Index", "Route"); + } + + TempData["Error"] = $"Unable to get route after {pollCount} Attempts!"; + return RedirectToAction("Index", "Route"); } diff --git a/Controllers/UserController.cs b/Controllers/UserController.cs index be02d7f..33d6f1e 100644 --- a/Controllers/UserController.cs +++ b/Controllers/UserController.cs @@ -31,63 +31,58 @@ namespace EFB.Controllers public async Task Login(string email, string password){ - if (Form.FormAuthenticator.ValidateEMail(email)) + if (!Form.FormAuthenticator.ValidateEMail(email)) { - //API Helper - APIInterface API = new APIInterface(); - - //Dictionary of Formdata to be encoded - Dictionary formData = new Dictionary(); - - formData.Add("grant_type", "client_credentials"); - formData.Add("client_id", email); - formData.Add("client_secret", password); - - HttpContent content = new FormUrlEncodedContent(formData); - - var request = API.Post("https://api.autorouter.aero/v1.0/oauth2/token", null, content); - - //Wait for the response to come through - ResponseModel response = await request; - - if (response.Error != null) - { - TempData["Error"] = response.Error; - TempData["email"] = email; - return RedirectToAction("Index", "Home"); - }else{ - - //Type cast required but we know response will be of known type - LoginResponse login = response.Result; - - //Generate User Session - if (login.error == null) - { - UserModel user = new UserModel{ - EMail = email, - UserToken = new TokenModel{ - TokenValue = login.access_token, - Expiration = DateTime.UtcNow.AddSeconds(login.expires_in) - } - }; - - //Using Session Extensions (Store the user session) - HttpContext.Session.SetObject("User", user); - return RedirectToAction("Index", "App"); - }else{ - TempData["Error"] = login.error_description; - TempData["email"] = email; - return RedirectToAction("Index", "Home"); - } - - - } - - }else{ TempData["Error"] = "Please enter a valid E-Mail"; return RedirectToAction("Index", "Home"); } + //API Helper + APIInterface API = new APIInterface(); + + //Dictionary of Formdata to be encoded + Dictionary formData = new Dictionary(); + + formData.Add("grant_type", "client_credentials"); + formData.Add("client_id", email); + formData.Add("client_secret", password); + + HttpContent content = new FormUrlEncodedContent(formData); + + var request = API.Post("https://api.autorouter.aero/v1.0/oauth2/token", null, content); + + //Wait for the response to come through + ResponseModel response = await request; + + if (response.Error != null) + { + TempData["Error"] = response.Error; + TempData["email"] = email; + return RedirectToAction("Index", "Home"); + } + //Type cast required but we know response will be of known type + LoginResponse login = response.Result; + + //Generate User Session + if (login.error != null) + { + TempData["Error"] = login.error_description; + TempData["email"] = email; + return RedirectToAction("Index", "Home"); + } + + UserModel user = new UserModel{ + EMail = email, + UserToken = new TokenModel{ + TokenValue = login.access_token, + Expiration = DateTime.UtcNow.AddSeconds(login.expires_in) + } + }; + + //Using Session Extensions (Store the user session) + HttpContext.Session.SetObject("User", user); + return RedirectToAction("Index", "App"); + } public IActionResult Logout(){