From 58eef9ea67ec487f17b11203cb3facb1e436ead1 Mon Sep 17 00:00:00 2001 From: Luke Else Date: Tue, 1 Feb 2022 20:25:31 +0000 Subject: [PATCH] Added Navdata View (Needs Tweaking) --- Controllers/NavdataController.cs | 27 ++++++++++++- Models/NavdataModel.cs | 17 ++++++--- Views/Navdata/Index.cshtml | 65 ++++++++++++++++++++++++++++++++ 3 files changed, 102 insertions(+), 7 deletions(-) create mode 100644 Views/Navdata/Index.cshtml diff --git a/Controllers/NavdataController.cs b/Controllers/NavdataController.cs index 5452bf4..bec6f5a 100644 --- a/Controllers/NavdataController.cs +++ b/Controllers/NavdataController.cs @@ -5,6 +5,8 @@ using System.Linq; using System.Threading.Tasks; using Microsoft.AspNetCore.Mvc; using Microsoft.Extensions.Logging; +using EFB.Models; +using EFB.Sessions; namespace EFB.Controllers { @@ -18,9 +20,30 @@ namespace EFB.Controllers _logger = logger; } - public IActionResult Index() + public async Task Index(string identifier) { - return View(); + if (identifier == null) + {//In the event we are just going to the base page1 + return View(); + } + + NavdataModel[] data = null; + + 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)); + } + + //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); + + if (navaid == null) + { + TempData["Error"] = $"Sorry, no Navaid found with the name {identifier}"; + } + return View(navaid); } [ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)] diff --git a/Models/NavdataModel.cs b/Models/NavdataModel.cs index 7560776..90a0bb8 100644 --- a/Models/NavdataModel.cs +++ b/Models/NavdataModel.cs @@ -3,6 +3,9 @@ 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 { @@ -10,22 +13,26 @@ namespace EFB.Models { public int Id { get; set; } public string Name { get; set; } + public string Type { get; set; } public int? Frequency { get; set; } public string Latitude { get; set; } public string Longitude { get; set; } - public NavdataModel(int id, string name, string latitude, string longitude){ + public NavdataModel(int id, string name, string type, string latitude, string longitude){ Id = id; Name = name; + Type = type; Frequency = null; Latitude = latitude; Longitude = longitude; } - public NavdataModel(int id, string name, int? frequency, string latitude, string longitude){ + [JsonConstructor] + public NavdataModel(int id, string name, string type, int? frequency, string latitude, string longitude){ Id = id; Name = name; + Type = type; Frequency = frequency; Latitude = latitude; Longitude = longitude; @@ -52,16 +59,16 @@ namespace EFB.Models string latitude = reader.GetString(4); string longitude = reader.GetString(5); - if (reader.GetString(2) == "VOR" || reader.GetString(2) == "NDB") + if (type == "VOR" || type == "NDB") { // int? frequency = reader.GetInt32(3); int? frequency = null; navdata.Add( - new NavdataModel(id, name, frequency, latitude, longitude) + new NavdataModel(id, name, type, frequency, latitude, longitude) ); }else{ navdata.Add( - new NavdataModel(id, name, latitude, longitude) + new NavdataModel(id, name, type, latitude, longitude) ); } } diff --git a/Views/Navdata/Index.cshtml b/Views/Navdata/Index.cshtml new file mode 100644 index 0000000..c1f0f91 --- /dev/null +++ b/Views/Navdata/Index.cshtml @@ -0,0 +1,65 @@ +@model EFB.Models.NavdataModel +@{ + ViewData["Title"] = "Welcome"; +} + +
+ +
+
+

Navdata Lookup

+ +
+
+ +
+
+ +
+ + + @{ + if (TempData["Error"] != null) + {//If an error has been flagged, information will be displayed to the user + +
+
+ +
+ Warning! @TempData["Error"]
+ } + } +
+
+
+ + @{ + if(Model != null){ +
+ +
+

@Model.Name (@Model.Type) [@Model.Id]

+ +
+ +

Latitude

+ @Model.Latitude + +

Longitude

+ @Model.Longitude + + @{ + if(Model.Frequency != null){ +

Frequency

+ @Model.Frequency + } + } +
+
+ + } + } + +
+