Code cleanup

This commit is contained in:
2022-02-28 21:53:28 +00:00
parent b1e8bfe8d0
commit 72124e7f15
31 changed files with 250 additions and 312 deletions

View File

@ -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<ResponseModel<T>> Get<T>(string Endpoint, Dictionary<string, string> Headers){
public async Task<ResponseModel<T>> Get<T>(string Endpoint, Dictionary<string, string> 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<T>(resultString);
}
return new ResponseModel<T>(){
return new ResponseModel<T>()
{
//Sender should be aware of type T becuase of Generic type
Result = (T)response
};
}
catch (System.Exception e)
{
return new ResponseModel<T>{Error = e.Message};
return new ResponseModel<T> { Error = e.Message };
}
}
//Returned in the event No other response has been returned
return new ResponseModel<T>{Error = "Invalid Endpoint - Please try again later"};
return new ResponseModel<T> { Error = "Invalid Endpoint - Please try again later" };
}
public async Task<ResponseModel<T>> Post<T>(string Endpoint, Dictionary<string, string> Headers, HttpContent Body){
public async Task<ResponseModel<T>> Post<T>(string Endpoint, Dictionary<string, string> 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<T>(resultString);
}
return new ResponseModel<T>(){
return new ResponseModel<T>()
{
//Sender should be aware of type T becuase of Generic type
Result = (T)response
};
}catch(System.Exception e){
return new ResponseModel<T>{Error = e.Message};
}
catch (System.Exception e)
{
return new ResponseModel<T> { Error = e.Message };
}
}
//Returned in the event No other response has been returned
return new ResponseModel<T>{Error = "Invalid Endpoint - Please try again later"};
return new ResponseModel<T> { Error = "Invalid Endpoint - Please try again later" };
}
public async Task<ResponseModel<T>> Put<T>(string Endpoint, Dictionary<string, string> Headers, HttpContent Body){
public async Task<ResponseModel<T>> Put<T>(string Endpoint, Dictionary<string, string> 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<T>(resultString);
}
return new ResponseModel<T>(){
return new ResponseModel<T>()
{
//Sender should be aware of type T becuase of Generic type
Result = (T)response
};
}catch(System.Exception e){
return new ResponseModel<T>{Error = e.Message};
}
catch (System.Exception e)
{
return new ResponseModel<T> { Error = e.Message };
}
}
//Returned in the event No other response has been returned
return new ResponseModel<T>{Error = "Invalid Endpoint - Please try again later"};
}
return new ResponseModel<T> { Error = "Invalid Endpoint - Please try again later" };
}
}
}

View File

@ -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<UserModel>("User");
if (user == null)
{
return RedirectToAction("Index", "Home");
return RedirectToAction("Index", "Home");
}
if (user.Route == null)
{
return RedirectToAction("Index", "Route");
}
return View(user);
}

View File

@ -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<UserModel>("User");
if (user != null)
{

View File

@ -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<UserModel>("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()

View File

@ -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;

View File

@ -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
{

View File

@ -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<NavdataModel[]>("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<NavdataModel[]>("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)
{

View File

@ -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<string>("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");
}
}
}

View File

@ -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<IActionResult> Login(string email, string password){
public async Task<IActionResult> 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<Models.JSON.LoginResponse>("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");
}