From 11db7d01960c7b3199bf94aea0201a6f8baf0f45 Mon Sep 17 00:00:00 2001
From: Callum Inglis <callum@4oh4.co>
Date: Sat, 8 Jan 2022 16:25:42 +0000
Subject: [PATCH] Brace Yourselves, dotnet upgrade incoming

---
 Public API/Client/Pages/Graph.razor           | 48 +++++++++++++++++++
 Public API/Client/Program.cs                  | 16 +++++++
 Public API/Client/Shared/NavMenu.razor        |  9 +++-
 Public API/Client/_Imports.razor              |  2 +-
 Public API/Client/wwwroot/index.html          |  8 ++++
 Public API/Client/wwwroot/js/app.js           |  9 ++++
 .../Server/Controllers/APIController.cs       | 26 +++++++++-
 .../Server/Controllers/FrontendController.cs  | 14 ++----
 Public API/Server/Public_API.Server.csproj    |  1 +
 9 files changed, 118 insertions(+), 15 deletions(-)
 create mode 100644 Public API/Client/Pages/Graph.razor
 create mode 100644 Public API/Client/wwwroot/js/app.js

diff --git a/Public API/Client/Pages/Graph.razor b/Public API/Client/Pages/Graph.razor
new file mode 100644
index 0000000..c6b4fd0
--- /dev/null
+++ b/Public API/Client/Pages/Graph.razor	
@@ -0,0 +1,48 @@
+@page "/graph"
+@using Public_API.Shared.Models
+@inject HttpClient http
+
+<h3>Graph</h3>
+
+<!-- TODO : List of sensors here-->
+<label for="sensor">Sensor: </label>
+@if (sensors == null)
+{
+    <p><em>Loading...</em></p>
+}
+else
+{
+
+    <select name="sensor" @onchange="@OnSensorSelectChange">
+        <option disabled>Select...</option>
+        @foreach (Sensor s in sensors)
+        {
+            <option value="@s.sensorID">@s.friendlyName</option>
+        }
+    </select>
+}
+
+
+<!-- TODO : Show data for each sensor here -->
+@if (targetSensor != null) {
+    <h3>Sensor Selected: @targetSensor.friendlyName</h3>
+
+    <!-- TODO Graphs here! -->
+
+
+}
+
+@code {
+    private Sensor[] sensors;
+    private Sensor targetSensor;
+
+    protected override async Task OnInitializedAsync()
+    {
+        sensors = await http.GetFromJsonAsync<Sensor[]>("api/GetSensorList");
+    }
+
+    public async Task OnSensorSelectChange(ChangeEventArgs e)
+    {
+        targetSensor = await http.GetFromJsonAsync<Sensor>("api/GetSensorByID/" + e.Value.ToString());
+    }
+}
\ No newline at end of file
diff --git a/Public API/Client/Program.cs b/Public API/Client/Program.cs
index debf151..2feb57a 100644
--- a/Public API/Client/Program.cs	
+++ b/Public API/Client/Program.cs	
@@ -7,6 +7,9 @@ using System.Collections.Generic;
 using System.Net.Http;
 using System.Text;
 using System.Threading.Tasks;
+using Blazorise;
+using Blazorise.Bootstrap;
+using Blazorise.Icons.FontAwesome;
 
 namespace Public_API.Client
 {
@@ -15,6 +18,19 @@ namespace Public_API.Client
         public static async Task Main(string[] args)
         {
             var builder = WebAssemblyHostBuilder.CreateDefault(args);
+            builder.Services
+                .AddBlazorise(options =>
+                {
+                    options.ChangeTextOnKeyPress = true;
+                })
+                .AddBootstrapProviders()
+                .AddFontAwesomeIcons();
+
+            builder.Services.AddSingleton(new HttpClient
+            {
+                BaseAddress = new Uri(builder.HostEnvironment.BaseAddress)
+            });
+
             builder.RootComponents.Add<App>("app");
 
             builder.Services.AddScoped(sp => new HttpClient { BaseAddress = new Uri(builder.HostEnvironment.BaseAddress) });
diff --git a/Public API/Client/Shared/NavMenu.razor b/Public API/Client/Shared/NavMenu.razor
index 66bb2c3..9a3226a 100644
--- a/Public API/Client/Shared/NavMenu.razor	
+++ b/Public API/Client/Shared/NavMenu.razor	
@@ -12,7 +12,7 @@
                 <span class="oi oi-home" aria-hidden="true"></span> Home
             </NavLink>
         </li>
-        <li class="nav-item px-3">
+        @*        <li class="nav-item px-3">
             <NavLink class="nav-link" href="counter">
                 <span class="oi oi-plus" aria-hidden="true"></span> Counter
             </NavLink>
@@ -21,12 +21,17 @@
             <NavLink class="nav-link" href="fetchdata">
                 <span class="oi oi-list-rich" aria-hidden="true"></span> Fetch data
             </NavLink>
-        </li>
+        </li>*@
         <li class="nav-item px-3">
             <NavLink class="nav-link" href="latestdata">
                 <span class="oi oi-list-rich" aria-hidden="true"></span> Latest Data
             </NavLink>
         </li>
+        <li class="nav-item px-3">
+            <NavLink class="nav-link" href="graph">
+                <span class="oi oi-list-rich" aria-hidden="true"></span> Graphs
+            </NavLink>
+        </li>
     </ul>
 </div>
 
diff --git a/Public API/Client/_Imports.razor b/Public API/Client/_Imports.razor
index da845e2..4248b66 100644
--- a/Public API/Client/_Imports.razor	
+++ b/Public API/Client/_Imports.razor	
@@ -6,4 +6,4 @@
 @using Microsoft.AspNetCore.Components.WebAssembly.Http
 @using Microsoft.JSInterop
 @using Public_API.Client
-@using Public_API.Client.Shared
+@using Public_API.Client.Shared
\ No newline at end of file
diff --git a/Public API/Client/wwwroot/index.html b/Public API/Client/wwwroot/index.html
index 2f123e5..8188970 100644
--- a/Public API/Client/wwwroot/index.html	
+++ b/Public API/Client/wwwroot/index.html	
@@ -6,6 +6,10 @@
     <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no" />
     <title>Public API</title>
     <base href="/" />
+
+    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@4.6.0/dist/css/bootstrap.min.css" integrity="sha384-B0vP5xmATw1+K9KRQjQERJvTumQW0nPEzvF6L/Z6nronJ3oUOFUFpCjEUQouq2+l" crossorigin="anonymous">
+    <link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.12.0/css/all.css">
+
     <link href="css/bootstrap/bootstrap.min.css" rel="stylesheet" />
     <link href="css/app.css" rel="stylesheet" />
 </head>
@@ -13,6 +17,10 @@
 <body>
     <app>Loading...</app>
 
+    <script src="https://code.jquery.com/jquery-3.5.1.slim.min.js" integrity="sha384-DfXdz2htPH0lsSSs5nCTpuj/zy4C+OGpamoFVy38MVBnE+IbbVYUew+OrCXaRkfj" crossorigin="anonymous"></script>
+    <script src="https://cdn.jsdelivr.net/npm/popper.js@1.16.1/dist/umd/popper.min.js" integrity="sha384-9/reFTGAW83EW2RDu2S0VKaIzap3H66lZH81PoYlFhbGU+6BZp6G7niu735Sk7lN" crossorigin="anonymous"></script>
+    <script src="https://cdn.jsdelivr.net/npm/bootstrap@4.6.0/dist/js/bootstrap.min.js" integrity="sha384-+YQ4JLhjyBLPDQt//I+STsc9iw4uQqACwlvpslubQzn4u2UU2UFM80nGisd026JF" crossorigin="anonymous"></script>
+
     <div id="blazor-error-ui">
         An unhandled error has occurred.
         <a href="" class="reload">Reload</a>
diff --git a/Public API/Client/wwwroot/js/app.js b/Public API/Client/wwwroot/js/app.js
new file mode 100644
index 0000000..e4d8cb6
--- /dev/null
+++ b/Public API/Client/wwwroot/js/app.js	
@@ -0,0 +1,9 @@
+// Form Submit Post AJAX
+$(function () {
+    $('#submit').on('click', function (evt) {
+        evt.preventDefault();
+        $.post('', $('form').serialize(), function () {
+            alert('Posted using jQuery');
+        });
+    });
+});
\ No newline at end of file
diff --git a/Public API/Server/Controllers/APIController.cs b/Public API/Server/Controllers/APIController.cs
index b10ddc8..b74137a 100644
--- a/Public API/Server/Controllers/APIController.cs	
+++ b/Public API/Server/Controllers/APIController.cs	
@@ -17,13 +17,19 @@ namespace Public_API.Server.Controllers
     public class APIController : ControllerBase
     {
 
-        ApiContext db = new ApiContext();
+        private ApiContext db = new ApiContext();
 
         public Sensor? getSensorByUID(string sensorUID)
         {
             return db.Sensor.Where(s => s.sensorUID.Equals(sensorUID)).FirstOrDefault();
         }
 
+        [HttpGet("GetSensorByID/{sensorID}")]
+        public Sensor? getSensorByID(int sensorID)
+        {
+            return db.Sensor.Where(s => s.sensorID.Equals(sensorID)).FirstOrDefault();
+        }
+
         // TODO Post
         // TODO Auth required
         // GET api/sensor/logValue/...
@@ -68,6 +74,24 @@ namespace Public_API.Server.Controllers
             return new HttpResponseMessage(HttpStatusCode.OK);
         }
 
+        [HttpGet("LatestData")]
+        public IEnumerable<vw_SensorData> GetLatestSensorData()
+        {
+            return db.vw_SensorData.OrderByDescending(d => d.dateTime).Take(100).ToArray();
+        }
+
+        [HttpGet("GetSensorList")]
+        public IEnumerable<Sensor> GetSensors()
+        {
+            return db.Sensor.OrderBy(d => d.friendlyName).ToArray();
+        }
+
+/*        [HttpPost("G")]
+        public ActionResult onGraphPost(String sensor)
+        {
+            //return new RedirectToPageResult("graph", getSensorByID(sensor));
+        }*/
+
 
 
         // DELETE api/<APIController>/5
diff --git a/Public API/Server/Controllers/FrontendController.cs b/Public API/Server/Controllers/FrontendController.cs
index e430fd3..efc2b73 100644
--- a/Public API/Server/Controllers/FrontendController.cs	
+++ b/Public API/Server/Controllers/FrontendController.cs	
@@ -9,16 +9,8 @@ using System.Threading.Tasks;
 
 namespace Public_API.Server.Controllers
 {
-    [ApiController]
     [Route("[controller]")]
-    public class FrontendController : Controller
-    {
-        private ApiContext db = new ApiContext();
-
-        [HttpGet("LatestData")]
-        public IEnumerable<vw_SensorData> Get()
-        {
-            return db.vw_SensorData.OrderByDescending(d => d.dateTime).Take(100).ToArray();
-        }
-    }
+    [ApiController]
+    public class FrontendController : ControllerBase
+    {    }
 }
diff --git a/Public API/Server/Public_API.Server.csproj b/Public API/Server/Public_API.Server.csproj
index de0ea30..2f5b2c8 100644
--- a/Public API/Server/Public_API.Server.csproj	
+++ b/Public API/Server/Public_API.Server.csproj	
@@ -7,6 +7,7 @@
   </PropertyGroup>
 
   <ItemGroup>
+    <PackageReference Include="Blazorise.Bootstrap" Version="0.9.5.4" />
     <PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly.Server" Version="3.2.1" />
     <PackageReference Include="Microsoft.Bcl.AsyncInterfaces" Version="5.0.0" />
     <PackageReference Include="Microsoft.EntityFrameworkCore" Version="3.1.20" />
-- 
GitLab