Added stubs for test pages
authorKrzysztof Wieclaw <k.wieclaw@samsung.com>
Fri, 2 Nov 2018 16:19:26 +0000 (17:19 +0100)
committerLukasz Stanislawski <l.stanislaws@samsung.com>
Mon, 4 Mar 2019 06:29:16 +0000 (07:29 +0100)
GearRacingCloud/Controllers/RacingController.cs
GearRacingCloud/Controllers/TestController.cs [new file with mode: 0644]
GearRacingCloud/Models/CarPageModel.cs [new file with mode: 0644]
GearRacingCloud/Models/Cars.cs
GearRacingCloud/Models/LapPageModel.cs [new file with mode: 0644]
GearRacingCloud/Startup.cs
GearRacingCloud/Views/Test/RacingCars.cshtml [new file with mode: 0644]
GearRacingCloud/Views/Test/RacingLaps.cshtml [new file with mode: 0644]
GearRacingCloud/appsettings.json

index f909f4f..4a8396f 100644 (file)
@@ -27,7 +27,8 @@ namespace GearRacingCloud.Controllers
         [HttpPost]
         public void Post([FromBody] Car car)
         {
-            Cars.Add(car);
+            if(ModelState.IsValid)
+                Cars.Add(car);
         }
     }
 }
diff --git a/GearRacingCloud/Controllers/TestController.cs b/GearRacingCloud/Controllers/TestController.cs
new file mode 100644 (file)
index 0000000..b894614
--- /dev/null
@@ -0,0 +1,78 @@
+using System;
+using System.Collections.Generic;
+using System.Diagnostics;
+using System.IO;
+using System.Linq;
+using System.Net.Http;
+using System.Threading.Tasks;
+using GearRacingCloud.Models;
+using Microsoft.AspNetCore.Mvc;
+using Microsoft.Extensions.Configuration;
+using Newtonsoft.Json;
+
+namespace GearRacingCloud.Controllers
+{
+    public class TestController : Controller
+    {
+        private HttpContent _CreateJsonContent(string json)
+        {
+            var buffer = System.Text.Encoding.UTF8.GetBytes(json);
+            var content = new ByteArrayContent(buffer);
+            content.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("application/json");
+            return content;
+        }
+        private HttpContent _CreateContentFrom(object o)
+        {
+            var json = JsonConvert.SerializeObject(o);
+            return _CreateJsonContent(json);
+        }
+
+        private static readonly HttpClient client = new HttpClient();
+        public TestController(IConfiguration configuration)
+        {
+            this.configuration = configuration;
+        }
+
+        IConfiguration configuration;
+        [Route("/test/racing-lap-test.html")]
+        public IActionResult RacingLaps()
+        {
+            Debug.Print(configuration.GetValue<string>("Defaults:Texts:PostLap"));
+            return View();
+        }
+        [Route("test/racing-test.html")]
+        public IActionResult RacingCars()
+        {
+            Debug.Print(configuration.GetValue<string>("Defaults:Texts:PostCar"));
+            return View();
+        }
+
+        [HttpPost]
+        public async Task<IActionResult> Car()
+        {
+               string uri = "https://"+Request.Host.ToUriComponent()+"/api/racing";
+               var content = _CreateJsonContent(HttpContext.Request.Form["post"]);
+               var res = await client.PostAsync(uri, content);
+               TempData["PostResult"] = res.ToString();
+               return RedirectToAction("RacingCars");
+        }
+
+        [HttpGet]
+        public async Task<IActionResult> Car(object o)
+        {
+            string uri = "https://" + Request.Host.ToUriComponent() + "/api/racing";
+            string mac;
+            if (HttpContext.Request.Form.ContainsKey("get"))
+                mac = HttpContext.Request.Form["get"];
+            else
+                mac = null;
+            uri += "?apMac=" + mac;
+            var res = await client.GetAsync(uri);
+            if (mac == null)
+                TempData["AllResult"] = res.ToString();
+            else
+                TempData["MACResult"] = res.ToString();
+            return RedirectToAction("RacingCars");
+        }
+    }
+}
\ No newline at end of file
diff --git a/GearRacingCloud/Models/CarPageModel.cs b/GearRacingCloud/Models/CarPageModel.cs
new file mode 100644 (file)
index 0000000..8fba364
--- /dev/null
@@ -0,0 +1,14 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Threading.Tasks;
+
+namespace GearRacingCloud.Models
+{
+    public class CarPageModel
+    {
+        public string PostResponse { get; set; }
+        public string MACResponse { get; set; }
+        public string GetAllResponse { get; set; }
+    }
+}
index ee6e831..b862a7f 100644 (file)
@@ -28,15 +28,18 @@ namespace GearRacingCloud.Models
 
         private void updateCollections(object sender, ElapsedEventArgs e)
         {
-            var carsToRemove = carsRefreshed.Where(p => !p.Value);
-            foreach (var car in carsToRemove)
+            lock (cars)
             {
-                carsRefreshed.Remove(car.Key);
-                cars.Remove(car.Key);
-            }
-            foreach(var car in carsRefreshed.Keys)
-            {
-                carsRefreshed[car] = false;
+                var carsToRemove = carsRefreshed.Where(p => !p.Value);
+                foreach (var car in carsToRemove)
+                {
+                    carsRefreshed.Remove(car.Key);
+                    cars.Remove(car.Key);
+                }
+                foreach (var car in carsRefreshed.Keys.ToList())
+                {
+                    carsRefreshed[car] = false;
+                }
             }
         }
 
diff --git a/GearRacingCloud/Models/LapPageModel.cs b/GearRacingCloud/Models/LapPageModel.cs
new file mode 100644 (file)
index 0000000..39249bb
--- /dev/null
@@ -0,0 +1,11 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Threading.Tasks;
+
+namespace GearRacingCloud.Models
+{
+    public class LapPageModel
+    {
+    }
+}
index 3628312..93f53f9 100644 (file)
@@ -33,6 +33,7 @@ namespace GearRacingCloud
 
 
             services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
+            services.AddSingleton<IConfiguration>(Configuration);
         }
 
         // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
diff --git a/GearRacingCloud/Views/Test/RacingCars.cshtml b/GearRacingCloud/Views/Test/RacingCars.cshtml
new file mode 100644 (file)
index 0000000..5184025
--- /dev/null
@@ -0,0 +1,49 @@
+@using Microsoft.Extensions.Configuration
+@inject IConfiguration configuration
+@model CarPageModel
+
+@{
+    ViewData["Title"] = "RacingCars";
+}
+
+<h2>Racing API Test</h2>
+<div class="panel panel-default">
+    <div class="panel-heading">POST /api/racing</div>
+    <div class="panel-body">
+        @using (Html.BeginForm("car", "test", FormMethod.Post))
+        {
+            @Html.TextArea("post", configuration.GetValue<string>("Defaults:Texts:PostCar"), new { style = "width: 100%; max-width: 100%;", rows = 8 });
+            <input type="submit" value="Send POST" />
+        }
+    </div>
+    <div class="panel-footer">
+        <p>@TempData["PostResult"]</p>
+    </div>
+</div>
+
+<div class="panel panel-default">
+    <div class="panel-heading">POST /api/racing</div>
+    <div class="panel-body">
+        @using (Html.BeginForm("car", "test", FormMethod.Get))
+        {
+            @Html.TextArea("get", configuration.GetValue<string>("Defaults:Texts:CarMAC"), new { style = "width: 100%; max-width: 100%;", rows = 8 });
+            <input type="submit" value="Send GET" />
+        }
+    </div>
+    <div class="panel-footer">
+        <p>@TempData["MACResult"]</p>
+    </div>
+</div>
+
+<div class="panel panel-default">
+    <div class="panel-heading">POST /api/racing</div>
+    <div class="panel-body">
+        @using (Html.BeginForm("car", "test", FormMethod.Get))
+        {
+            <input type="submit" value="Send GET" />
+        }
+    </div>
+    <div class="panel-footer">
+        <p>@TempData["AllResult"]</p>
+    </div>
+</div>
diff --git a/GearRacingCloud/Views/Test/RacingLaps.cshtml b/GearRacingCloud/Views/Test/RacingLaps.cshtml
new file mode 100644 (file)
index 0000000..c3b7399
--- /dev/null
@@ -0,0 +1,8 @@
+@model IEnumerable<Lap>
+
+@{
+    ViewData["Title"] = "RacingLaps";
+}
+
+    <h2>RacingLaps</h2>
+
index def9159..f3fce2f 100644 (file)
@@ -4,5 +4,11 @@
       "Default": "Warning"
     }
   },
-  "AllowedHosts": "*"
+  "AllowedHosts": "*",
+  "Defaults": {
+    "Texts": {
+      "PostCar": "{\n\"carId\": \"5894098509845549509348509481234\",\n\"carName\": \"Yellow Porsche\",\n\"carIp\": \"192.168.0.12\",\n\"apMac\": \"23:45:56:58:23:AB:3G\",\n\"apSsid\": \"Tizen AP\"\n}",
+      "CarMAC":  "1a:2b:3c:4d:5e:6f"
+    }
+  }
 }