diff --git a/Controllers/API/APIInterface.cs b/Controllers/API/APIInterface.cs index 7cebf6a..cd5f022 100644 --- a/Controllers/API/APIInterface.cs +++ b/Controllers/API/APIInterface.cs @@ -1,10 +1,8 @@ -using System.Collections.Generic; -using System.Text; -using System; -using System.Threading.Tasks; -using Newtonsoft.Json; -using System.Net.Http; using EFB.Models; +using Newtonsoft.Json; +using System.Collections.Generic; +using System.Net.Http; +using System.Threading.Tasks; namespace EFB.Controllers.API { @@ -12,7 +10,8 @@ namespace EFB.Controllers.API { private HttpClient HttpClient { get; set; } - public async Task> Get(string Endpoint, Dictionary Headers){ + public async Task> Get(string Endpoint, Dictionary Headers) + { //Create the HTTP client used for the GetRequest. this.HttpClient = new HttpClient(); this.HttpClient.DefaultRequestHeaders.Clear(); @@ -41,21 +40,23 @@ namespace EFB.Controllers.API {//If the user requests string for return type response = JsonConvert.DeserializeObject(resultString); } - return new ResponseModel(){ + return new ResponseModel() + { //Sender should be aware of type T becuase of Generic type Result = (T)response }; } catch (System.Exception e) { - return new ResponseModel{Error = e.Message}; + return new ResponseModel { Error = e.Message }; } } //Returned in the event No other response has been returned - return new ResponseModel{Error = "Invalid Endpoint - Please try again later"}; + return new ResponseModel { Error = "Invalid Endpoint - Please try again later" }; } - public async Task> Post(string Endpoint, Dictionary Headers, HttpContent Body){ + public async Task> Post(string Endpoint, Dictionary Headers, HttpContent Body) + { //Create a HTTP client to allow for making Post requests this.HttpClient = new HttpClient(); this.HttpClient.DefaultRequestHeaders.Clear(); @@ -70,7 +71,8 @@ namespace EFB.Controllers.API if (Form.FormAuthenticator.ValidateEndpoint(Endpoint)) { - try{//Try statement to catch errors in the process of making the request + try + {//Try statement to catch errors in the process of making the request var pendingResult = this.HttpClient.PostAsync(Endpoint, Body); var result = await pendingResult; string resultString = result.Content.ReadAsStringAsync().Result; @@ -81,18 +83,22 @@ namespace EFB.Controllers.API response = JsonConvert.DeserializeObject(resultString); } - return new ResponseModel(){ + return new ResponseModel() + { //Sender should be aware of type T becuase of Generic type Result = (T)response }; - }catch(System.Exception e){ - return new ResponseModel{Error = e.Message}; + } + catch (System.Exception e) + { + return new ResponseModel { Error = e.Message }; } } //Returned in the event No other response has been returned - return new ResponseModel{Error = "Invalid Endpoint - Please try again later"}; + return new ResponseModel { Error = "Invalid Endpoint - Please try again later" }; } - public async Task> Put(string Endpoint, Dictionary Headers, HttpContent Body){ + public async Task> Put(string Endpoint, Dictionary Headers, HttpContent Body) + { //Create HTTP client to allow for HttpRequests this.HttpClient = new HttpClient(); this.HttpClient.DefaultRequestHeaders.Clear(); @@ -107,11 +113,12 @@ namespace EFB.Controllers.API if (Form.FormAuthenticator.ValidateEndpoint(Endpoint)) { - try{//Try statement to catch errors in the process of making the request + try + {//Try statement to catch errors in the process of making the request var pendingResult = this.HttpClient.PutAsync(Endpoint, Body); var result = await pendingResult; string resultString = result.Content.ReadAsStringAsync().Result; - + //Recieve the response as a string which will be morphed in other types object response = resultString; @@ -120,18 +127,21 @@ namespace EFB.Controllers.API response = JsonConvert.DeserializeObject(resultString); } - return new ResponseModel(){ + return new ResponseModel() + { //Sender should be aware of type T becuase of Generic type Result = (T)response }; - }catch(System.Exception e){ - return new ResponseModel{Error = e.Message}; + } + catch (System.Exception e) + { + return new ResponseModel { Error = e.Message }; } } //Returned in the event No other response has been returned - return new ResponseModel{Error = "Invalid Endpoint - Please try again later"}; - } - + return new ResponseModel { Error = "Invalid Endpoint - Please try again later" }; + } + } } \ No newline at end of file diff --git a/Controllers/AppController.cs b/Controllers/AppController.cs index e8354c2..eb76430 100644 --- a/Controllers/AppController.cs +++ b/Controllers/AppController.cs @@ -1,13 +1,8 @@ -using System; -using System.Collections.Generic; -using System.Diagnostics; -using System.Linq; -using System.Threading.Tasks; +using EFB.Models; +using EFB.Sessions; +using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Mvc; using Microsoft.Extensions.Logging; -using EFB.Models; -using Microsoft.AspNetCore.Http; -using EFB.Sessions; namespace EFB.Controllers { @@ -26,13 +21,13 @@ namespace EFB.Controllers UserModel user = HttpContext.Session.GetObject("User"); if (user == null) { - return RedirectToAction("Index", "Home"); + return RedirectToAction("Index", "Home"); } if (user.Route == null) { return RedirectToAction("Index", "Route"); } - + return View(user); } diff --git a/Controllers/ChartsController.cs b/Controllers/ChartsController.cs index 7ac6db4..488af49 100644 --- a/Controllers/ChartsController.cs +++ b/Controllers/ChartsController.cs @@ -1,14 +1,9 @@ -using System; -using System.Collections.Generic; -using System.Diagnostics; -using System.Linq; -using System.Threading.Tasks; +using EFB.Controllers.Form; +using EFB.Models; +using EFB.Sessions; using Microsoft.AspNetCore.Mvc; using Microsoft.Extensions.Logging; -using EFB.Models; -using EFB.Models.JSON; -using EFB.Controllers.Form; -using EFB.Sessions; +using System.Threading.Tasks; namespace EFB.Controllers { @@ -30,8 +25,8 @@ namespace EFB.Controllers TempData["Error"] = "Must be logged in to view charts"; return RedirectToAction("Index", "Home"); } - - if(ICAO == null) + + if (ICAO == null) return View(); if (FormAuthenticator.ValidateICAOCode(ICAO)) @@ -45,14 +40,16 @@ namespace EFB.Controllers HttpContext.Session.SetObject("User", user); return RedirectToAction("ViewCharts"); } - }else + } + else { TempData["Error"] = "Invalid ICAO"; } return View(); } - public IActionResult ViewCharts(string chart){ + public IActionResult ViewCharts(string chart) + { UserModel user = HttpContext.Session.GetObject("User"); if (user != null) { diff --git a/Controllers/FlightsimController.cs b/Controllers/FlightsimController.cs index de82f7b..2861244 100644 --- a/Controllers/FlightsimController.cs +++ b/Controllers/FlightsimController.cs @@ -1,14 +1,11 @@ -using System; -using System.Collections.Generic; -using System.Diagnostics; -using System.Linq; -using System.Threading.Tasks; -using Microsoft.AspNetCore.Mvc; -using EFB.Sessions; -using Microsoft.Extensions.Logging; using EFB.Models; -using EFB.MongoData; using EFB.Models.Route; +using EFB.MongoData; +using EFB.Sessions; +using Microsoft.AspNetCore.Mvc; +using Microsoft.Extensions.Logging; +using System; +using System.Threading.Tasks; namespace EFB.Controllers { @@ -26,16 +23,18 @@ namespace EFB.Controllers { //Retrieve and Check current user status UserModel user = HttpContext.Session.GetObject("User"); - if(user == null){ + 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){ + 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); @@ -46,7 +45,8 @@ namespace EFB.Controllers } - private IWaypoint DetermineClosest(RouteModel route, SimPosition currentPosition){ + private IWaypoint DetermineClosest(RouteModel route, SimPosition currentPosition) + { IWaypoint closest = null; float closestDistance = float.MaxValue; //Assuming that we are on the earth for simplicity @@ -60,8 +60,8 @@ namespace EFB.Controllers float latitude1 = DegreesToRadians(currentPosition.Latitude); float latitude2 = DegreesToRadians(waypoint.Latitude); - var a = Math.Sin(distanceLat/2) * Math.Sin(distanceLat/2) + Math.Sin(distanceLon/2) * Math.Sin(distanceLon/2) * Math.Cos(latitude1) * Math.Cos(latitude2); - var c = 2 * Math.Atan2(Math.Sqrt(a), Math.Sqrt(1-a)); + var a = Math.Sin(distanceLat / 2) * Math.Sin(distanceLat / 2) + Math.Sin(distanceLon / 2) * Math.Sin(distanceLon / 2) * Math.Cos(latitude1) * Math.Cos(latitude2); + var c = 2 * Math.Atan2(Math.Sqrt(a), Math.Sqrt(1 - a)); float distance = (float)(earthRadius * c); @@ -74,9 +74,10 @@ namespace EFB.Controllers } return closest; } - private float DegreesToRadians(float degrees){ + private float DegreesToRadians(float degrees) + { return (float)(degrees * Math.PI / 180); - } + } [ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)] public IActionResult Error() diff --git a/Controllers/Form/FormAuthenticator.cs b/Controllers/Form/FormAuthenticator.cs index df42554..7854ccc 100644 --- a/Controllers/Form/FormAuthenticator.cs +++ b/Controllers/Form/FormAuthenticator.cs @@ -1,14 +1,12 @@ -using System; -using System.Collections.Generic; using System.Linq; -using System.Threading.Tasks; namespace EFB.Controllers.Form { public static class FormAuthenticator { - - public static bool ValidateEMail(string EMail){ + + public static bool ValidateEMail(string EMail) + { if (EMail != null && EMail.Contains("@") && EMail.Contains(".") && !EMail.Contains(" ")) { if (EMail.Count(x => x == '@') == 1) @@ -19,7 +17,8 @@ namespace EFB.Controllers.Form return false; } - public static bool ValidateEndpoint(string Endpoint){ + public static bool ValidateEndpoint(string Endpoint) + { //If it contains http & :// it can be either https or http if (Endpoint.Contains("http") && Endpoint.Contains("://") && Endpoint.Length > 7) { @@ -28,7 +27,8 @@ namespace EFB.Controllers.Form return false; } - public static bool ValidateICAOCode(string ICAO){ + public static bool ValidateICAOCode(string ICAO) + { if (ICAO.Length == 4) { //If the value contains a Number, then the value will return false @@ -37,7 +37,8 @@ namespace EFB.Controllers.Form return false; } - public static bool ValidateCruiseAlt(uint CruiseAlt){ + public static bool ValidateCruiseAlt(uint CruiseAlt) + { if (CruiseAlt > 0 && CruiseAlt < 50000) { return true; diff --git a/Controllers/HomeController.cs b/Controllers/HomeController.cs index 982ba3b..a851546 100644 --- a/Controllers/HomeController.cs +++ b/Controllers/HomeController.cs @@ -1,11 +1,7 @@ using EFB.Models; using Microsoft.AspNetCore.Mvc; using Microsoft.Extensions.Logging; -using System; -using System.Collections.Generic; using System.Diagnostics; -using System.Linq; -using System.Threading.Tasks; namespace EFB.Controllers { diff --git a/Controllers/NavdataController.cs b/Controllers/NavdataController.cs index bec6f5a..f4e8f12 100644 --- a/Controllers/NavdataController.cs +++ b/Controllers/NavdataController.cs @@ -1,12 +1,8 @@ -using System; -using System.Collections.Generic; -using System.Diagnostics; -using System.Linq; -using System.Threading.Tasks; -using Microsoft.AspNetCore.Mvc; -using Microsoft.Extensions.Logging; using EFB.Models; using EFB.Sessions; +using Microsoft.AspNetCore.Mvc; +using Microsoft.Extensions.Logging; +using System.Threading.Tasks; namespace EFB.Controllers { @@ -32,12 +28,12 @@ namespace EFB.Controllers if (HttpContext.Session.GetObject("Navdata") == null) {//If the navdata needs re-caching data = await NavdataModel.Populate(); - HttpContext.Session.SetObject("Navdata", NavdataModel.MergeSort(ref data, 0, data.Length-1)); + HttpContext.Session.SetObject("Navdata", NavdataModel.MergeSort(ref data, 0, data.Length - 1)); } //get the data out of tempdata and cast it into an array data = HttpContext.Session.GetObject("Navdata"); - NavdataModel navaid = NavdataModel.BinarySearch(ref data, 0, data.Length-1, identifier); + NavdataModel navaid = NavdataModel.BinarySearch(ref data, 0, data.Length - 1, identifier); if (navaid == null) { diff --git a/Controllers/RouteController.cs b/Controllers/RouteController.cs index 51bf22d..c2e4ef5 100644 --- a/Controllers/RouteController.cs +++ b/Controllers/RouteController.cs @@ -1,18 +1,17 @@ -using System; -using System.Threading.Tasks; -using System.Threading; -using System.Collections.Generic; -using System.Text; -using System.Net.Http; -using Microsoft.AspNetCore.Mvc; -using Microsoft.Extensions.Logging; -using Microsoft.AspNetCore.Http; -using Newtonsoft.Json; +using EFB.Controllers.API; +using EFB.Controllers.Form; using EFB.Models; using EFB.Models.JSON; using EFB.Sessions; -using EFB.Controllers.Form; -using EFB.Controllers.API; +using Microsoft.AspNetCore.Http; +using Microsoft.AspNetCore.Mvc; +using Microsoft.Extensions.Logging; +using Newtonsoft.Json; +using System.Collections.Generic; +using System.Net.Http; +using System.Text; +using System.Threading; +using System.Threading.Tasks; namespace EFB.Controllers { @@ -53,10 +52,10 @@ namespace EFB.Controllers 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"); + TempData["Error"] = "Invalid Departure or Arrival ICAO"; + return RedirectToAction("Index", "Route"); } - + uint cruiseAlt; if (uint.TryParse(cruise, out cruiseAlt) && FormAuthenticator.ValidateCruiseAlt(cruiseAlt)) @@ -77,7 +76,7 @@ namespace EFB.Controllers 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); @@ -170,7 +169,7 @@ namespace EFB.Controllers { user.DepartureCharts = new ChartModel(departure, departureCharts); } - + ChartList arrivalCharts = await requestArrivalCharts; if (arrivalCharts != null) { @@ -184,7 +183,7 @@ namespace EFB.Controllers user.Arrival = arrival; user.Cruise = cruise; HttpContext.Session.SetObject("User", user); - + return RedirectToAction("Index", "App"); } @@ -192,6 +191,6 @@ namespace EFB.Controllers return RedirectToAction("Index", "Route"); } - + } } \ No newline at end of file diff --git a/Controllers/UserController.cs b/Controllers/UserController.cs index 33d6f1e..f0b17ab 100644 --- a/Controllers/UserController.cs +++ b/Controllers/UserController.cs @@ -1,15 +1,13 @@ +using EFB.Controllers.API; +using EFB.Models; +using EFB.Models.JSON; +using EFB.Sessions; +using Microsoft.AspNetCore.Mvc; +using Microsoft.Extensions.Logging; using System; using System.Collections.Generic; -using System.Diagnostics; -using System.Linq; -using System.Threading.Tasks; -using Microsoft.AspNetCore.Mvc; using System.Net.Http; -using EFB.Models.JSON; -using Microsoft.Extensions.Logging; -using EFB.Models; -using EFB.Sessions; -using EFB.Controllers.API; +using System.Threading.Tasks; namespace EFB.Controllers { @@ -29,7 +27,8 @@ namespace EFB.Controllers } - public async Task Login(string email, string password){ + public async Task Login(string email, string password) + { if (!Form.FormAuthenticator.ValidateEMail(email)) { @@ -48,7 +47,7 @@ namespace EFB.Controllers 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 @@ -71,9 +70,11 @@ namespace EFB.Controllers return RedirectToAction("Index", "Home"); } - UserModel user = new UserModel{ + UserModel user = new UserModel + { EMail = email, - UserToken = new TokenModel{ + UserToken = new TokenModel + { TokenValue = login.access_token, Expiration = DateTime.UtcNow.AddSeconds(login.expires_in) } @@ -85,7 +86,8 @@ namespace EFB.Controllers } - public IActionResult Logout(){ + public IActionResult Logout() + { HttpContext.Session.SetObject("User", null); return RedirectToAction("Index", "Home"); } diff --git a/Metar/Metar.cs b/Metar/Metar.cs index 80bcc5f..dba3242 100644 --- a/Metar/Metar.cs +++ b/Metar/Metar.cs @@ -1,8 +1,6 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Threading.Tasks; using libmetar.Services; +using System.Collections.Generic; +using System.Threading.Tasks; namespace EFB.Metar { @@ -10,16 +8,18 @@ namespace EFB.Metar { private static MetarService metarService { get; set; } = new MetarService(); private static TafService tafService { get; set; } = new TafService(); - - public static async Task GetMETAR(string ICAO){ + + public static async Task GetMETAR(string ICAO) + { return (await metarService.GetRawAsync(ICAO)).Raw; } - public static async Task> GetTAF(string ICAO){ + public static async Task> GetTAF(string ICAO) + { var downloadedTAF = (await tafService.GetRawAsync(ICAO)).RawSplit; List TAF = new List(); - + foreach (var line in downloadedTAF) { TAF.Add(line); diff --git a/Models/ChartModel.cs b/Models/ChartModel.cs index f3b25bf..4dfa39e 100644 --- a/Models/ChartModel.cs +++ b/Models/ChartModel.cs @@ -1,18 +1,11 @@ -using System; -using System.Threading.Tasks; -using System.Threading; -using System.Collections.Generic; -using System.Text; -using System.Net.Http; -using Microsoft.AspNetCore.Mvc; -using Microsoft.Extensions.Logging; -using Microsoft.AspNetCore.Http; -using Newtonsoft.Json; -using EFB.Models; -using EFB.Models.JSON; -using EFB.Sessions; -using EFB.Controllers.Form; using EFB.Controllers.API; +using EFB.Controllers.Form; +using EFB.Models.JSON; +using Newtonsoft.Json; +using System; +using System.Collections.Generic; +using System.Net.Http; +using System.Threading.Tasks; namespace EFB.Models { @@ -29,7 +22,8 @@ namespace EFB.Models public Chart[] PilotBriefing { get; set; } [JsonConstructor] - public ChartModel(){ + public ChartModel() + { //Empty constructor for JSON Serialisation Purposes } public ChartModel(string ICAO, ChartList response) @@ -45,24 +39,26 @@ namespace EFB.Models PilotBriefing = FillChart(response.PilotBriefing); } - private Chart[] FillChart(ChartsCollection collection){ + private Chart[] FillChart(ChartsCollection collection) + { if (collection != null) { return collection.Charts; } - return new Chart[]{}; + return new Chart[] { }; } - public static async Task FetchAsync(string ICAO){ + public static async Task FetchAsync(string ICAO) + { Console.WriteLine("Start"); if (FormAuthenticator.ValidateICAOCode(ICAO)) { APIInterface API = new APIInterface(); - Dictionary headerData = new Dictionary(); + Dictionary headerData = new Dictionary(); headerData.Add("referer", "luke-else.co.uk"); Dictionary formData = new Dictionary(); - + formData.Add("token", Environment.GetEnvironmentVariable("ChartFoxAPIKey", EnvironmentVariableTarget.User)); FormUrlEncodedContent body = new FormUrlEncodedContent(formData); @@ -71,7 +67,7 @@ namespace EFB.Models Console.WriteLine("End"); if (requestCharts.Result.Status == "success") { - return requestCharts.Result.Response; + return requestCharts.Result.Response; } } return null; diff --git a/Models/ErrorViewModel.cs b/Models/ErrorViewModel.cs index 7e71ca9..40d5ed2 100644 --- a/Models/ErrorViewModel.cs +++ b/Models/ErrorViewModel.cs @@ -1,5 +1,3 @@ -using System; - namespace EFB.Models { public class ErrorViewModel diff --git a/Models/FlightsimModel.cs b/Models/FlightsimModel.cs index 6f9604e..7abf9e9 100644 --- a/Models/FlightsimModel.cs +++ b/Models/FlightsimModel.cs @@ -1,7 +1,3 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Threading.Tasks; using EFB.Models.Route; namespace EFB.Models diff --git a/Models/JSON/ChartResponse.cs b/Models/JSON/ChartResponse.cs index 77c34c8..7f7b344 100644 --- a/Models/JSON/ChartResponse.cs +++ b/Models/JSON/ChartResponse.cs @@ -1,7 +1,3 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Threading.Tasks; using Newtonsoft.Json; namespace EFB.Models.JSON @@ -59,6 +55,6 @@ namespace EFB.Models.JSON [JsonProperty("url")] public string URL { get; set; } } - - + + } \ No newline at end of file diff --git a/Models/JSON/Login.cs b/Models/JSON/Login.cs index 76db256..745af05 100644 --- a/Models/JSON/Login.cs +++ b/Models/JSON/Login.cs @@ -1,8 +1,4 @@ -using System; -using System.Collections.Generic; -using System.Linq; using Newtonsoft.Json; -using System.Threading.Tasks; namespace EFB.Models.JSON { @@ -14,6 +10,6 @@ namespace EFB.Models.JSON public string client_id { get; set; } [JsonProperty] public string client_secret { get; set; } - + } } \ No newline at end of file diff --git a/Models/JSON/LoginResponse.cs b/Models/JSON/LoginResponse.cs index 765fac9..b7ddce7 100644 --- a/Models/JSON/LoginResponse.cs +++ b/Models/JSON/LoginResponse.cs @@ -1,7 +1,3 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Threading.Tasks; using Newtonsoft.Json; namespace EFB.Models.JSON @@ -16,13 +12,13 @@ namespace EFB.Models.JSON public string token_type { get; set; } [JsonProperty] public string scope { get; set; } - + [JsonProperty] public string error { get; set; } = null; [JsonProperty] public string error_description { get; set; } = null; - - + + } } \ No newline at end of file diff --git a/Models/JSON/PollResponse.cs b/Models/JSON/PollResponse.cs index 3c353e9..92738c3 100644 --- a/Models/JSON/PollResponse.cs +++ b/Models/JSON/PollResponse.cs @@ -1,7 +1,3 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Threading.Tasks; using Newtonsoft.Json; namespace EFB.Models.JSON diff --git a/Models/NavdataModel.cs b/Models/NavdataModel.cs index 5092df4..c5b9773 100644 --- a/Models/NavdataModel.cs +++ b/Models/NavdataModel.cs @@ -1,11 +1,9 @@ +using MySql.Data.MySqlClient; +using Newtonsoft.Json; using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; -using MySql.Data.MySqlClient; -using Newtonsoft.Json; -using Microsoft.AspNetCore.Mvc; -using EFB.Sessions; namespace EFB.Models { @@ -17,9 +15,10 @@ namespace EFB.Models public int? Frequency { get; set; } public string Latitude { get; set; } public string Longitude { get; set; } - - public NavdataModel(int id, string name, string type, string latitude, string longitude){ + + public NavdataModel(int id, string name, string type, string latitude, string longitude) + { Id = id; Name = name; Type = type; @@ -29,7 +28,8 @@ namespace EFB.Models } [JsonConstructor] - public NavdataModel(int id, string name, string type, int? frequency, string latitude, string longitude){ + public NavdataModel(int id, string name, string type, int? frequency, string latitude, string longitude) + { Id = id; Name = name; Type = type; @@ -38,7 +38,8 @@ namespace EFB.Models Longitude = longitude; } - public static async Task Populate(){ + public static async Task Populate() + { string password = Environment.GetEnvironmentVariable("MySQLPassword", EnvironmentVariableTarget.User); MySqlConnection con = new MySqlConnection($"server=server.luke-else.co.uk;userid=root;password={password};database=EFB"); con.Open(); @@ -48,7 +49,7 @@ namespace EFB.Models string query = "SELECT * FROM waypoints"; MySqlCommand command = new MySqlCommand(query, con); - MySqlDataReader reader = (MySqlDataReader) await command.ExecuteReaderAsync(); + MySqlDataReader reader = (MySqlDataReader)await command.ExecuteReaderAsync(); List navdata = new List(); @@ -67,7 +68,9 @@ namespace EFB.Models navdata.Add( new NavdataModel(id, name, type, frequency, latitude, longitude) ); - }else{ + } + else + { navdata.Add( new NavdataModel(id, name, type, latitude, longitude) ); @@ -77,13 +80,14 @@ namespace EFB.Models return navdata.ToArray(); } - public static NavdataModel BinarySearch(ref NavdataModel[] data, int start, int end, string target){ + public static NavdataModel BinarySearch(ref NavdataModel[] data, int start, int end, string target) + { int midpoint = start + ((end - start) / 2); target = target.ToUpper().Trim(); string mid = data[midpoint].Name; - if (start == end-1) + if (start == end - 1) { if (mid == target) { @@ -110,26 +114,31 @@ namespace EFB.Models //Split the data down to the left and the right portions NavdataModel[] left = MergeSort(ref data, start, midpoint); - NavdataModel[] right = MergeSort(ref data, midpoint+1, end); + NavdataModel[] right = MergeSort(ref data, midpoint + 1, end); List combined = new List(); int leftPointer = 0; int rightPointer = 0; - while (leftPointer <= left.Length-1 || rightPointer <= right.Length-1) + while (leftPointer <= left.Length - 1 || rightPointer <= right.Length - 1) { if (leftPointer == left.Length) {//Take a value only from the right (left pointer had reached the end) AddValue(ref combined, right[rightPointer], ref rightPointer); - }else if (rightPointer == right.Length) + } + else if (rightPointer == right.Length) {//Take a value only from the left (right pointer has reached the end) AddValue(ref combined, left[leftPointer], ref leftPointer); - }else{ + } + else + { if (String.Compare(left[leftPointer].Name, right[rightPointer].Name) <= 0) {//Take a value from the left hand side of the list. (Left value is considered 'smaller') AddValue(ref combined, left[leftPointer], ref leftPointer); - }else{//Take a value from the right (right value is considered smaller) + } + else + {//Take a value from the right (right value is considered smaller) AddValue(ref combined, right[rightPointer], ref rightPointer); } } @@ -139,10 +148,11 @@ namespace EFB.Models } - private static void AddValue(ref List data, NavdataModel value, ref int pointer){ + private static void AddValue(ref List data, NavdataModel value, ref int pointer) + { pointer++; data.Add(value); } - + } } \ No newline at end of file diff --git a/Models/ResponseModel.cs b/Models/ResponseModel.cs index 92f2f32..8707da4 100644 --- a/Models/ResponseModel.cs +++ b/Models/ResponseModel.cs @@ -1,8 +1,3 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Threading.Tasks; - namespace EFB.Models { public class ResponseModel @@ -10,6 +5,6 @@ namespace EFB.Models //Object should be of known type from sender public T Result { get; set; } = default(T); public string Error { get; set; } = null; - + } } \ No newline at end of file diff --git a/Models/Route/IWaypoint.cs b/Models/Route/IWaypoint.cs index bddfe06..b9a1a46 100644 --- a/Models/Route/IWaypoint.cs +++ b/Models/Route/IWaypoint.cs @@ -1,8 +1,3 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Threading.Tasks; - namespace EFB.Models.Route { public interface IWaypoint @@ -10,11 +5,11 @@ namespace EFB.Models.Route public string Name { get; set; } public float Longitude { get; set; } public float Latitude { get; set; } - + public string Airway { get; set; } public IWaypoint Next { get; set; } public IWaypoint Previous { get; set; } public bool Visited { get; set; } - + } } \ No newline at end of file diff --git a/Models/Route/NavaidModel.cs b/Models/Route/NavaidModel.cs index 0e35e8b..d29f93d 100644 --- a/Models/Route/NavaidModel.cs +++ b/Models/Route/NavaidModel.cs @@ -1,23 +1,19 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Threading.Tasks; - namespace EFB.Models.Route { - public class NavaidModel:IWaypoint + public class NavaidModel : IWaypoint { public string Name { get; set; } public float Longitude { get; set; } public float Latitude { get; set; } public int Frequency { get; set; } - + public string Airway { get; set; } public IWaypoint Next { get; set; } = null; public IWaypoint Previous { get; set; } = null; public bool Visited { get; set; } = false; - - public NavaidModel(string name, string airway, float longitude, float latitude){ + + public NavaidModel(string name, string airway, float longitude, float latitude) + { Name = name; Airway = airway; Longitude = longitude; diff --git a/Models/Route/WaypointModel.cs b/Models/Route/WaypointModel.cs index 7518ed7..46c9773 100644 --- a/Models/Route/WaypointModel.cs +++ b/Models/Route/WaypointModel.cs @@ -1,27 +1,23 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Threading.Tasks; - namespace EFB.Models.Route { - public class WaypointModel:IWaypoint + public class WaypointModel : IWaypoint { public string Name { get; set; } public float Longitude { get; set; } public float Latitude { get; set; } - + public string Airway { get; set; } public IWaypoint Next { get; set; } = null; public IWaypoint Previous { get; set; } = null; public bool Visited { get; set; } - public WaypointModel(string name, string airway, float longitude, float latitude){ + public WaypointModel(string name, string airway, float longitude, float latitude) + { Name = name; Airway = airway; Longitude = longitude; Latitude = latitude; } - + } } \ No newline at end of file diff --git a/Models/RouteModel.cs b/Models/RouteModel.cs index 9805407..986162f 100644 --- a/Models/RouteModel.cs +++ b/Models/RouteModel.cs @@ -1,10 +1,6 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Threading.Tasks; -using EFB.Models.Route; using EFB.Controllers.Form; -using System.Net.Http; +using EFB.Models.Route; +using System.Threading.Tasks; namespace EFB.Models { @@ -19,7 +15,8 @@ namespace EFB.Models public WaypointModel Arrival { get; set; } = null; public IWaypoint Current { get; set; } = null; public uint Cruise { get; set; } = 0; - public RouteModel(string departure, string departureRoute, string arrival, string arrivalRoute, uint cruise){ + public RouteModel(string departure, string departureRoute, string arrival, string arrivalRoute, uint cruise) + { if (FormAuthenticator.ValidateICAOCode(departure)) { Departure = new WaypointModel(departure, departureRoute, 0, 0); @@ -36,16 +33,20 @@ namespace EFB.Models } } - public IWaypoint Next(){ - if(Current.Next != null){ + public IWaypoint Next() + { + if (Current.Next != null) + { Current = Current.Next; return Current; } return null; } - public IWaypoint Previous(){ - if(Current.Previous != null){ + public IWaypoint Previous() + { + if (Current.Previous != null) + { Current = Current.Previous; return Current; } @@ -53,7 +54,8 @@ namespace EFB.Models } //Generate a route Object - public static async Task StringToRoute(string departure, string arrival, uint cruise, string routeString){ + public static async Task StringToRoute(string departure, string arrival, uint cruise, string routeString) + { var navdataFetch = NavdataModel.Populate(); string[] routeTemp = routeString.Split(" "); @@ -69,10 +71,10 @@ namespace EFB.Models NavdataModel[] navdata = await navdataFetch; navdata = NavdataModel.MergeSort(ref navdata, 0, navdata.Length - 1); - for (var i = 1; i < routeTemp.Length-1; i+=2) + for (var i = 1; i < routeTemp.Length - 1; i += 2) {//Already used first item, continue itterating over every other item IWaypoint next; - NavdataModel currentWaypoint = NavdataModel.BinarySearch(ref navdata, 0, navdata.Length-1, routeTemp[i]); + NavdataModel currentWaypoint = NavdataModel.BinarySearch(ref navdata, 0, navdata.Length - 1, routeTemp[i]); if (currentWaypoint == null) { currentWaypoint = new NavdataModel(0, routeTemp[i], null, "0", "0"); @@ -81,26 +83,27 @@ namespace EFB.Models if (routeTemp[i].Length > 3) {//waypoint Type next = new WaypointModel( - routeTemp[i], - routeTemp[i+1], - float.Parse(currentWaypoint.Longitude), + routeTemp[i], + routeTemp[i + 1], + float.Parse(currentWaypoint.Longitude), float.Parse(currentWaypoint.Latitude) - ); - }else + ); + } + else {//Navaid Type next = new NavaidModel( - routeTemp[i], - routeTemp[i+1], - float.Parse(currentWaypoint.Longitude), + routeTemp[i], + routeTemp[i + 1], + float.Parse(currentWaypoint.Longitude), float.Parse(currentWaypoint.Latitude) - ); + ); } next.Previous = route.Current; route.Current.Next = next; route.Current = next; } - + //Connect end of route (linked list) route.Current.Airway = null; route.Current.Next = route.Arrival; @@ -109,8 +112,8 @@ namespace EFB.Models route.Current = route.Departure; //Assign departure and arrival coordinate positions - NavdataModel departureNav = NavdataModel.BinarySearch(ref navdata, 0, navdata.Length - 1, departure); - NavdataModel arrivalNav = NavdataModel.BinarySearch(ref navdata, 0, navdata.Length - 1, arrival); + NavdataModel departureNav = NavdataModel.BinarySearch(ref navdata, 0, navdata.Length - 1, departure); + NavdataModel arrivalNav = NavdataModel.BinarySearch(ref navdata, 0, navdata.Length - 1, arrival); route.Departure.Latitude = float.Parse(departureNav.Latitude); route.Departure.Longitude = float.Parse(departureNav.Longitude); route.Arrival.Latitude = float.Parse(arrivalNav.Latitude); @@ -123,7 +126,8 @@ namespace EFB.Models //Generate a route String - public static string ParseRoute(string route){ + public static string ParseRoute(string route) + { route.Replace('/', ' '); var routeArr = route.Split(' '); @@ -138,13 +142,13 @@ namespace EFB.Models if (waypoint.Length == 7 && finalRoute.Length > 8) break; - + } } return finalRoute; } - + } } \ No newline at end of file diff --git a/Models/SimPositionModel.cs b/Models/SimPositionModel.cs index 85e529d..b992507 100644 --- a/Models/SimPositionModel.cs +++ b/Models/SimPositionModel.cs @@ -1,10 +1,6 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Threading.Tasks; -using MongoDB.Bson.Serialization.Attributes; using MongoDB.Bson; -using MongoDB.Driver; +using MongoDB.Bson.Serialization.Attributes; +using System; namespace EFB.Models { @@ -16,12 +12,13 @@ namespace EFB.Models public DateTime LatestPacketUpdate { get; set; } public SimPosition LatestPosition { get; set; } = null; - public SimPositionModel(string email, SimPosition position){ + public SimPositionModel(string email, SimPosition position) + { EMail = email; LatestPacketUpdate = DateTime.Now; LatestPosition = position; } - + } @@ -30,8 +27,9 @@ namespace EFB.Models public float Latitude { get; set; } public float Longitude { get; set; } public int Altitude { get; set; } - - public SimPosition(float latitude, float longitude, int altitude){ + + public SimPosition(float latitude, float longitude, int altitude) + { Latitude = latitude; Longitude = longitude; Altitude = altitude; diff --git a/Models/TokenModel.cs b/Models/TokenModel.cs index 08faff2..6127299 100644 --- a/Models/TokenModel.cs +++ b/Models/TokenModel.cs @@ -1,7 +1,4 @@ using System; -using System.Collections.Generic; -using System.Linq; -using System.Threading.Tasks; namespace EFB.Models { @@ -13,7 +10,8 @@ namespace EFB.Models public string TokenValue { get; init; } public DateTime Expiration { get; init; } - public bool IsExpired(){ + public bool IsExpired() + { //Check if the current time is beyond expiration if (DateTime.UtcNow > Expiration) { @@ -21,6 +19,6 @@ namespace EFB.Models } return false; } - + } } \ No newline at end of file diff --git a/Models/UserModel.cs b/Models/UserModel.cs index cc4ece7..e24b8e4 100644 --- a/Models/UserModel.cs +++ b/Models/UserModel.cs @@ -1,8 +1,3 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Threading.Tasks; - namespace EFB.Models { public class UserModel @@ -19,14 +14,14 @@ namespace EFB.Models public object Id { get; init; } public string EMail { get; init; } public TokenModel UserToken { get; set; } = null; - + //Contains the most recent route generated by the user through the App public string Departure { get; set; } public string Route { get; set; } public string Arrival { get; set; } public uint Cruise { get; set; } - - + + public TokenModel RouteToken { get; set; } = null; //Contains the Departure and Arrival Charts for the user's route @@ -36,6 +31,6 @@ namespace EFB.Models //Contains the most recently stored position of the user in the simulator public object SimPosition { get; set; } = null; - + } } \ No newline at end of file diff --git a/Models/ViewChartModel.cs b/Models/ViewChartModel.cs index 900e9c7..ff8362e 100644 --- a/Models/ViewChartModel.cs +++ b/Models/ViewChartModel.cs @@ -1,14 +1,5 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Threading.Tasks; -using Microsoft.AspNetCore.Mvc; -using Microsoft.AspNetCore.Html; -using Microsoft.AspNetCore.Razor; -using Newtonsoft.Json; using EFB.Models.JSON; -using Microsoft.AspNetCore; -using EFB.Sessions; +using Newtonsoft.Json; namespace EFB.Models { @@ -16,8 +7,9 @@ namespace EFB.Models { public ChartModel Charts { get; set; } public Chart Selected { get; set; } - - public ViewChartModel(ChartModel current, string selected){ + + public ViewChartModel(ChartModel current, string selected) + { Charts = current; if (selected != null) { diff --git a/Mongo/Mongo.cs b/Mongo/Mongo.cs index 6624770..3d3e6e8 100644 --- a/Mongo/Mongo.cs +++ b/Mongo/Mongo.cs @@ -1,17 +1,15 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Threading.Tasks; -using MongoDB.Driver; -using MongoDB.Bson; using EFB.Models; +using MongoDB.Driver; +using System; +using System.Threading.Tasks; namespace EFB.MongoData { public class Mongo { //function that is responsible to getting the user's latest sim position from the MongoDB - public static async Task GetLatestData(string email){ + public static async Task GetLatestData(string email) + { MongoClient client = new MongoClient( Environment.GetEnvironmentVariable("MongoDBConnectionString", EnvironmentVariableTarget.User) ); diff --git a/Program.cs b/Program.cs index 180bc16..0169f10 100644 --- a/Program.cs +++ b/Program.cs @@ -1,11 +1,5 @@ using Microsoft.AspNetCore.Hosting; -using Microsoft.Extensions.Configuration; using Microsoft.Extensions.Hosting; -using Microsoft.Extensions.Logging; -using System; -using System.Collections.Generic; -using System.Linq; -using System.Threading.Tasks; namespace EFB { diff --git a/Sessions/SessionExtensions.cs b/Sessions/SessionExtensions.cs index e433f17..caa49b9 100644 --- a/Sessions/SessionExtensions.cs +++ b/Sessions/SessionExtensions.cs @@ -7,8 +7,9 @@ namespace EFB.Sessions { public static void SetObject(this ISession session, string key, object value) {//Sets the object of a session to Object - session.SetString(key, JsonConvert.SerializeObject(value, Formatting.None, - new JsonSerializerSettings(){ + session.SetString(key, JsonConvert.SerializeObject(value, Formatting.None, + new JsonSerializerSettings() + { ReferenceLoopHandling = ReferenceLoopHandling.Ignore } )); diff --git a/Startup.cs b/Startup.cs index 61cf386..c5aea48 100644 --- a/Startup.cs +++ b/Startup.cs @@ -1,13 +1,8 @@ using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Hosting; -using Microsoft.AspNetCore.HttpsPolicy; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Hosting; -using System; -using System.Collections.Generic; -using System.Linq; -using System.Threading.Tasks; namespace EFB {