From 85028743ed7bc547b4c30ced46a7c373146caab5 Mon Sep 17 00:00:00 2001 From: Mike Phares Date: Thu, 5 Jan 2023 12:40:46 -0700 Subject: [PATCH] CORS, Theme, Better flow and dotnet tools - II --- Client/.vscode/launch.json | 20 +++++++++ Client/.vscode/tasks.json | 41 +++++++++++++++++++ Client/App.razor.cs | 4 ++ Client/Expose.MyIT.Client.csproj | 8 ++-- Client/Pages/FetchData.razor.cs | 20 ++++++--- Client/Pages/FetchServiceShopOrders.razor.cs | 24 +++++++---- Client/Pages/FetchSsaOrders.razor | 42 +++++++++++++++++++ Client/Pages/FetchSsaOrders.razor.cs | 43 ++++++++++++++++++++ Client/Pages/Index.razor.cs | 4 ++ Client/Shared/MainLayout.razor | 1 - Client/Shared/NavMenu.razor.cs | 4 ++ Server/Expose-MyIT.Server.csproj | 2 +- Server/Program.cs | 2 +- Shared/Expose-MyIT.Shared.csproj | 2 +- Shared/ViewModels/ServiceShopOrder.cs | 21 +++++----- Shared/ViewModels/SsaOrder.cs | 21 +++++----- Shared/ViewModels/WeatherForecast.cs | 17 +++++++- Tests/Expose-MyIT.Tests.csproj | 8 ++-- Tests/UnitTestServiceShopOrderController.cs | 12 ++---- Tests/UnitTestSsaOrderController.cs | 12 ++---- Tests/UnitTestWeatherForecastController.cs | 7 +--- 21 files changed, 246 insertions(+), 69 deletions(-) create mode 100644 Client/.vscode/launch.json create mode 100644 Client/.vscode/tasks.json create mode 100644 Client/App.razor.cs create mode 100644 Client/Pages/FetchSsaOrders.razor create mode 100644 Client/Pages/FetchSsaOrders.razor.cs create mode 100644 Client/Pages/Index.razor.cs create mode 100644 Client/Shared/NavMenu.razor.cs diff --git a/Client/.vscode/launch.json b/Client/.vscode/launch.json new file mode 100644 index 0000000..b7bcc2f --- /dev/null +++ b/Client/.vscode/launch.json @@ -0,0 +1,20 @@ +{ + "version": "0.2.0", + "configurations": [ + { + "name": "Attach to Edge", + "port": 9222, + "request": "attach", + "type": "msedge", + "webRoot": "${workspaceFolder}", + "url": "http://localhost:5055/counter", + "inspectUri": "{wsProtocol}://{url.hostname}:{url.port}/_framework/debug/ws-proxy?browser={browserInspectUri}" + }, + { + "name": "Launch Microsoft Edge and open the Edge DevTools", + "request": "launch", + "type": "vscode-edge-devtools.debug", + "url": "http://localhost:5055" + } + ] +} \ No newline at end of file diff --git a/Client/.vscode/tasks.json b/Client/.vscode/tasks.json new file mode 100644 index 0000000..48b462e --- /dev/null +++ b/Client/.vscode/tasks.json @@ -0,0 +1,41 @@ +{ + "version": "2.0.0", + "tasks": [ + { + "label": "build", + "command": "dotnet", + "type": "process", + "args": [ + "build", + "${workspaceFolder}/Expose.MyIT.Client.csproj", + "/property:GenerateFullPaths=true", + "/consoleloggerparameters:NoSummary" + ], + "problemMatcher": "$msCompile" + }, + { + "label": "publish", + "command": "dotnet", + "type": "process", + "args": [ + "publish", + "${workspaceFolder}/Expose.MyIT.Client.csproj", + "/property:GenerateFullPaths=true", + "/consoleloggerparameters:NoSummary" + ], + "problemMatcher": "$msCompile" + }, + { + "label": "watch", + "command": "dotnet", + "type": "process", + "args": [ + "watch", + "run", + "--project", + "${workspaceFolder}/Expose.MyIT.Client.csproj" + ], + "problemMatcher": "$msCompile" + } + ] +} \ No newline at end of file diff --git a/Client/App.razor.cs b/Client/App.razor.cs new file mode 100644 index 0000000..b7e8a22 --- /dev/null +++ b/Client/App.razor.cs @@ -0,0 +1,4 @@ +namespace Expose.MyIT.Client; + +public partial class App +{ } \ No newline at end of file diff --git a/Client/Expose.MyIT.Client.csproj b/Client/Expose.MyIT.Client.csproj index 3cfec0a..98d8596 100644 --- a/Client/Expose.MyIT.Client.csproj +++ b/Client/Expose.MyIT.Client.csproj @@ -9,12 +9,12 @@ service-worker-assets.js - - + + - - + + diff --git a/Client/Pages/FetchData.razor.cs b/Client/Pages/FetchData.razor.cs index 27c8c7e..752acbb 100644 --- a/Client/Pages/FetchData.razor.cs +++ b/Client/Pages/FetchData.razor.cs @@ -7,16 +7,24 @@ namespace Expose.MyIT.Client.Pages; public partial class FetchData { - [Inject] protected HttpClient? Http { get; set; } + [Inject] protected HttpClient? HttpClient { get; set; } + [Inject] protected ILogger? Logger { get; set; } private WeatherForecast[]? _Forecasts; protected override async Task OnInitializedAsync() { - if (Http is null) - throw new NullReferenceException(nameof(Http)); - string controllerName = MyIT.Shared.Models.Stateless.Methods.IWeatherForecastController.GetRouteName(); - // _SsaOrders = await Http.GetFromJsonAsync("sample-data/weather.json"); - _Forecasts = await Http.GetFromJsonAsync($"api/{controllerName}"); + if (Logger is null) + throw new NullReferenceException(nameof(Logger)); + if (HttpClient is null) + throw new NullReferenceException(nameof(HttpClient)); + string controllerName = MyIT.Shared.Models.Stateless.IWeatherForecastController.GetRouteName(); + try + { _Forecasts = await HttpClient.GetFromJsonAsync($"api/{controllerName}"); } + catch (Exception) + { + string json = await HttpClient.GetStringAsync($"api/{controllerName}"); + Logger.LogInformation(message: json); + } } } \ No newline at end of file diff --git a/Client/Pages/FetchServiceShopOrders.razor.cs b/Client/Pages/FetchServiceShopOrders.razor.cs index 1f19b80..790cd9f 100644 --- a/Client/Pages/FetchServiceShopOrders.razor.cs +++ b/Client/Pages/FetchServiceShopOrders.razor.cs @@ -1,4 +1,5 @@ -using Expose.MyIT.Shared.ViewModels; +using Expose.MyIT.Shared.Models.Stateless; +using Expose.MyIT.Shared.ViewModels; using Microsoft.AspNetCore.Components; using System.Net.Http.Json; @@ -7,7 +8,8 @@ namespace Expose.MyIT.Client.Pages; public partial class FetchServiceShopOrders { - [Inject] protected HttpClient? Http { get; set; } + [Inject] protected HttpClient? HttpClient { get; set; } + [Inject] protected ILogger? Logger { get; set; } private ServiceShopOrder[]? _ServiceShopOrders; @@ -24,10 +26,18 @@ public partial class FetchServiceShopOrders protected override async Task OnInitializedAsync() { - if (Http is null) - throw new NullReferenceException(nameof(Http)); - string controllerName = MyIT.Shared.Models.Stateless.Methods.IServiceShopOrderController.GetRouteName(); - string actionName = nameof(MyIT.Shared.Models.Stateless.Methods.ISsaOrderController.GetAllSsaOrders).Substring(3, 3); - _ServiceShopOrders = await Http.GetFromJsonAsync($"api/{controllerName}/{actionName}"); + if (Logger is null) + throw new NullReferenceException(nameof(Logger)); + if (HttpClient is null) + throw new NullReferenceException(nameof(HttpClient)); + string actionName = nameof(IServiceShopOrderController.Action.All); + string controllerName = IServiceShopOrderController.GetRouteName(); + try + { _ServiceShopOrders = await HttpClient.GetFromJsonAsync($"api/{controllerName}/{actionName}"); } + catch (Exception) + { + string json = await HttpClient.GetStringAsync($"api/{controllerName}/{actionName}"); + Logger.LogInformation(message: json); + } } } \ No newline at end of file diff --git a/Client/Pages/FetchSsaOrders.razor b/Client/Pages/FetchSsaOrders.razor new file mode 100644 index 0000000..b2f77d7 --- /dev/null +++ b/Client/Pages/FetchSsaOrders.razor @@ -0,0 +1,42 @@ +@page "/fetchssaorders" +@using Expose.MyIT.Shared.ViewModels + +SSA Orders + +SSA Orders +@if (_SsaOrders == null) +{ + +} +else +{ + + + Id + Name + Booking Names + Type + State + Item Number + Created Date + Decided Date + Recipient + Requestor + + + @context.Id + @context.Name + @string.Join(' ', context.BookingNames) + @context.Type + @context.State + @context.ItemNumber + @context.CreatedDate + @context.DecidedDate + @context.Recipient + @context.Requestor + + + + + +} \ No newline at end of file diff --git a/Client/Pages/FetchSsaOrders.razor.cs b/Client/Pages/FetchSsaOrders.razor.cs new file mode 100644 index 0000000..feac8f9 --- /dev/null +++ b/Client/Pages/FetchSsaOrders.razor.cs @@ -0,0 +1,43 @@ +using Expose.MyIT.Shared.Models.Stateless; +using Expose.MyIT.Shared.ViewModels; +using Microsoft.AspNetCore.Components; +using System.Net.Http.Json; + +namespace Expose.MyIT.Client.Pages; + +public partial class FetchSsaOrders +{ + + [Inject] protected HttpClient? HttpClient { get; set; } + [Inject] protected ILogger? Logger { get; set; } + + private SsaOrder[]? _SsaOrders; + + //Id + //Name + //BookingNames + //Type + //State + //ItemNumber + //CreatedDate + //DecidedDate + //Recipient + //Requestor + + protected override async Task OnInitializedAsync() + { + if (Logger is null) + throw new NullReferenceException(nameof(Logger)); + if (HttpClient is null) + throw new NullReferenceException(nameof(HttpClient)); + string actionName = nameof(ISsaOrderController.Action.All); + string controllerName = ISsaOrderController.GetRouteName(); + try + { _SsaOrders = await HttpClient.GetFromJsonAsync($"api/{controllerName}/{actionName}"); } + catch (Exception) + { + string json = await HttpClient.GetStringAsync($"api/{controllerName}/{actionName}"); + Logger.LogInformation(message: json); + } + } +} \ No newline at end of file diff --git a/Client/Pages/Index.razor.cs b/Client/Pages/Index.razor.cs new file mode 100644 index 0000000..796a5c3 --- /dev/null +++ b/Client/Pages/Index.razor.cs @@ -0,0 +1,4 @@ +namespace Expose.MyIT.Client.Pages; + +public partial class Index +{ } \ No newline at end of file diff --git a/Client/Shared/MainLayout.razor b/Client/Shared/MainLayout.razor index a0b8193..a253588 100644 --- a/Client/Shared/MainLayout.razor +++ b/Client/Shared/MainLayout.razor @@ -1,6 +1,5 @@ @inherits LayoutComponentBase - diff --git a/Client/Shared/NavMenu.razor.cs b/Client/Shared/NavMenu.razor.cs new file mode 100644 index 0000000..2de8d65 --- /dev/null +++ b/Client/Shared/NavMenu.razor.cs @@ -0,0 +1,4 @@ +namespace Expose.MyIT.Client.Shared; + +public partial class NavMenu +{ } \ No newline at end of file diff --git a/Server/Expose-MyIT.Server.csproj b/Server/Expose-MyIT.Server.csproj index b1a79a7..5b0961a 100644 --- a/Server/Expose-MyIT.Server.csproj +++ b/Server/Expose-MyIT.Server.csproj @@ -7,7 +7,7 @@ $(AssemblyName.Replace(' ', '_')) - + diff --git a/Server/Program.cs b/Server/Program.cs index 7a226c9..b004cb0 100644 --- a/Server/Program.cs +++ b/Server/Program.cs @@ -53,7 +53,7 @@ public class Program _ = ApplicationBuilderSerilogClientExtensions.UseSerilogIngestion(app); _ = SerilogApplicationBuilderExtensions.UseSerilogRequestLogging(app); - _ = app.UseHttpsRedirection(); + // _ = app.UseHttpsRedirection(); _ = app.UseBlazorFrameworkFiles(); _ = app.UseStaticFiles(); diff --git a/Shared/Expose-MyIT.Shared.csproj b/Shared/Expose-MyIT.Shared.csproj index 4c8e5df..02a19db 100644 --- a/Shared/Expose-MyIT.Shared.csproj +++ b/Shared/Expose-MyIT.Shared.csproj @@ -8,6 +8,6 @@ - + \ No newline at end of file diff --git a/Shared/ViewModels/ServiceShopOrder.cs b/Shared/ViewModels/ServiceShopOrder.cs index b0f15e7..f2be7f0 100644 --- a/Shared/ViewModels/ServiceShopOrder.cs +++ b/Shared/ViewModels/ServiceShopOrder.cs @@ -2,19 +2,20 @@ using System.Text.Json; namespace Expose.MyIT.Shared.ViewModels; +// [JsonConstructor] public record ServiceShopOrder(string Id, - string Name, - string[] BookingNames, - string Type, - string State, - string ItemNumber, - DateTime CreatedDate, - DateTime DecidedDate, - string Recipient, - string Requestor) + string Name, + string[] BookingNames, + string Type, + string State, + string ItemNumber, + DateTime CreatedDate, + DateTime DecidedDate, + string Recipient, + string Requestor) { - public ServiceShopOrder(Models.Order order) : + internal ServiceShopOrder(Models.Order order) : this(order.Id, order.Name, order.Bookings is null || !order.Bookings.Any() ? Array.Empty() : order.Bookings.Select(l => l.Name).ToArray(), diff --git a/Shared/ViewModels/SsaOrder.cs b/Shared/ViewModels/SsaOrder.cs index f4166e2..c1501f7 100644 --- a/Shared/ViewModels/SsaOrder.cs +++ b/Shared/ViewModels/SsaOrder.cs @@ -2,19 +2,20 @@ using System.Text.Json; namespace Expose.MyIT.Shared.ViewModels; +// [JsonConstructor] public record SsaOrder(string Id, - string Name, - string[] BookingNames, - string Type, - string State, - string ItemNumber, - DateTime CreatedDate, - DateTime DecidedDate, - string Recipient, - string Requestor) + string Name, + string[] BookingNames, + string Type, + string State, + string ItemNumber, + DateTime CreatedDate, + DateTime DecidedDate, + string Recipient, + string Requestor) { - public SsaOrder(Models.Order order) : + internal SsaOrder(Models.Order order) : this(order.Id, order.Name, order.Bookings is null || !order.Bookings.Any() ? Array.Empty() : order.Bookings.Select(l => l.Name).ToArray(), diff --git a/Shared/ViewModels/WeatherForecast.cs b/Shared/ViewModels/WeatherForecast.cs index 9a8d1e1..f25bd93 100644 --- a/Shared/ViewModels/WeatherForecast.cs +++ b/Shared/ViewModels/WeatherForecast.cs @@ -6,9 +6,22 @@ namespace Expose.MyIT.Shared.ViewModels; public record WeatherForecast { + public DateOnly Date { init; get; } + + public int TemperatureC { init; get; } + + public string? Summary { init; get; } + + public int TemperatureF { init; get; } + [JsonConstructor] - public WeatherForecast(DateOnly Date, int TemperatureC, string? Summary, int TemperatureF) - { } + public WeatherForecast(DateOnly date, int temperatureC, string? summary, int temperatureF) + { + Date = date; + TemperatureC = temperatureC; + Summary = summary; + TemperatureF = temperatureF; + } public WeatherForecast(Models.WeatherForecast weatherForecast) : this(weatherForecast.Date, diff --git a/Tests/Expose-MyIT.Tests.csproj b/Tests/Expose-MyIT.Tests.csproj index 9f87bbd..19524a8 100644 --- a/Tests/Expose-MyIT.Tests.csproj +++ b/Tests/Expose-MyIT.Tests.csproj @@ -12,10 +12,10 @@ - - - - + + + + diff --git a/Tests/UnitTestServiceShopOrderController.cs b/Tests/UnitTestServiceShopOrderController.cs index 37aba96..322c272 100644 --- a/Tests/UnitTestServiceShopOrderController.cs +++ b/Tests/UnitTestServiceShopOrderController.cs @@ -3,7 +3,7 @@ using Expose.MyIT.Shared.ViewModels; using Microsoft.AspNetCore.Mvc.Testing; using Microsoft.Extensions.DependencyInjection; using Serilog; -using System.Net; +using System.Net.Http.Json; namespace Expose.MyIT.Tests; @@ -68,13 +68,9 @@ public class UnitTestServiceShopOrderController HttpClient httpClient = _WebApplicationFactory.CreateClient(); _Logger.Information("Starting Web Application"); string actionName = nameof(IServiceShopOrderController.Action.All); - HttpResponseMessage httpResponseMessage = await httpClient.GetAsync($"api/{_ControllerName}/{actionName}"); - Assert.AreEqual(HttpStatusCode.OK, httpResponseMessage.StatusCode); - Assert.AreEqual("application/json; charset=utf-8", httpResponseMessage.Content.Headers.ContentType?.ToString()); - string result = await httpResponseMessage.Content.ReadAsStringAsync(); - httpClient.Dispose(); - Assert.IsNotNull(result); - Assert.IsTrue(result != "[]"); + ServiceShopOrder[]? serviceShopOrders = await httpClient.GetFromJsonAsync($"api/{_ControllerName}/{actionName}"); + Assert.IsNotNull(serviceShopOrders); + Assert.IsTrue(serviceShopOrders.Any()); _Logger.Information($"{_TestContext?.TestName} completed"); } diff --git a/Tests/UnitTestSsaOrderController.cs b/Tests/UnitTestSsaOrderController.cs index 3b30e21..6e181af 100644 --- a/Tests/UnitTestSsaOrderController.cs +++ b/Tests/UnitTestSsaOrderController.cs @@ -3,7 +3,7 @@ using Expose.MyIT.Shared.ViewModels; using Microsoft.AspNetCore.Mvc.Testing; using Microsoft.Extensions.DependencyInjection; using Serilog; -using System.Net; +using System.Net.Http.Json; namespace Expose.MyIT.Tests; @@ -68,13 +68,9 @@ public class UnitTestSsaOrderController HttpClient httpClient = _WebApplicationFactory.CreateClient(); _Logger.Information("Starting Web Application"); string actionName = nameof(ISsaOrderController.Action.All); - HttpResponseMessage httpResponseMessage = await httpClient.GetAsync($"api/{_ControllerName}/{actionName}"); - Assert.AreEqual(HttpStatusCode.OK, httpResponseMessage.StatusCode); - Assert.AreEqual("application/json; charset=utf-8", httpResponseMessage.Content.Headers.ContentType?.ToString()); - string result = await httpResponseMessage.Content.ReadAsStringAsync(); - httpClient.Dispose(); - Assert.IsNotNull(result); - Assert.IsTrue(result != "[]"); + SsaOrder[]? ssaOrders = await httpClient.GetFromJsonAsync($"api/{_ControllerName}/{actionName}"); + Assert.IsNotNull(ssaOrders); + Assert.IsTrue(ssaOrders.Any()); _Logger.Information($"{_TestContext?.TestName} completed"); } diff --git a/Tests/UnitTestWeatherForecastController.cs b/Tests/UnitTestWeatherForecastController.cs index 200dc2b..80eec0d 100644 --- a/Tests/UnitTestWeatherForecastController.cs +++ b/Tests/UnitTestWeatherForecastController.cs @@ -1,6 +1,5 @@ using Microsoft.AspNetCore.Mvc.Testing; using Serilog; -using System.Net; namespace Expose.MyIT.Tests; @@ -39,12 +38,8 @@ public class UnitTestWeatherForecastController { HttpClient httpClient = _WebApplicationFactory.CreateClient(); _Logger.Information("Starting Web Application"); - HttpResponseMessage httpResponseMessage = await httpClient.GetAsync($"api/{_ControllerName}"); - Assert.AreEqual(HttpStatusCode.OK, httpResponseMessage.StatusCode); - Assert.AreEqual("application/json; charset=utf-8", httpResponseMessage.Content.Headers.ContentType?.ToString()); - string result = await httpResponseMessage.Content.ReadAsStringAsync(); + string result = await httpClient.GetStringAsync($"api/{_ControllerName}"); Assert.IsNotNull(result); - httpClient.Dispose(); _Logger.Information($"{_TestContext?.TestName} completed"); }