diff --git a/ASP.Net Core API Gateway - 01/Catalog.ViewModelComposition/Catalog.ViewModelComposition.csproj b/ASP.Net Core API Gateway - 01/Catalog.ViewModelComposition/Catalog.ViewModelComposition.csproj index 40317540..f42773a4 100644 --- a/ASP.Net Core API Gateway - 01/Catalog.ViewModelComposition/Catalog.ViewModelComposition.csproj +++ b/ASP.Net Core API Gateway - 01/Catalog.ViewModelComposition/Catalog.ViewModelComposition.csproj @@ -6,7 +6,7 @@ - + diff --git a/ASP.Net Core API Gateway - 01/Composition.Tests/When_calling_composition_gateway.cs b/ASP.Net Core API Gateway - 01/Composition.Tests/When_calling_composition_gateway.cs index e2e005ee..1b9bc2fc 100644 --- a/ASP.Net Core API Gateway - 01/Composition.Tests/When_calling_composition_gateway.cs +++ b/ASP.Net Core API Gateway - 01/Composition.Tests/When_calling_composition_gateway.cs @@ -54,8 +54,8 @@ public async Task Get_composed_product_should_return_expected_values() HttpClient ClientProvider(string name) => name switch { - var val when val == typeof(Shipping.ViewModelComposition.ProductDetailsGetHandler).FullName => _shippingApiClient, - var val when val == typeof(Warehouse.ViewModelComposition.ProductDetailsGetHandler).FullName => _warehouseApiClient, + var val when val == typeof(Shipping.ViewModelComposition.CompositionHandlers.ProductDetailsCompositionHandler).FullName => _shippingApiClient, + var val when val == typeof(Warehouse.ViewModelComposition.CompositionHandlers.ProductDetailsCompositionHandler).FullName => _warehouseApiClient, var val when val == typeof(Sales.ViewModelComposition.ProductDetailsGetHandler).FullName => _salesApiClient, var val when val == typeof(Catalog.ViewModelComposition.ProductDetailsGetHandler).FullName => _catalogApiClient, _ => throw new NotSupportedException($"Missing HTTP client for {name}") @@ -72,6 +72,7 @@ HttpClient ClientProvider(string name) => // Act var composedResponse = await compositionClient.GetAsync("/products/details/1"); + var stringContent = await composedResponse.Content.ReadAsStringAsync(); dynamic composedViewModel = await composedResponse.Content.AsExpando(); // Assert @@ -80,9 +81,10 @@ HttpClient ClientProvider(string name) => Assert.Equal("Banana Holder", composedViewModel.ProductName); Assert.Equal("Outdoor travel cute banana protector storage box", composedViewModel.ProductDescription); Assert.Equal(10, composedViewModel.ProductPrice); - Assert.Equal(4, composedViewModel.ProductInventory); - Assert.Equal(false, composedViewModel.ProductOutOfStock); + Assert.Equal("Express Delivery, Regular mail", composedViewModel.ProductShippingOptions); + Assert.Equal(false, composedViewModel.ProductOutOfStock); + Assert.Equal(4, composedViewModel.ProductInventory); } } } \ No newline at end of file diff --git a/ASP.Net Core API Gateway - 01/CompositionGateway/CompositionGateway.csproj b/ASP.Net Core API Gateway - 01/CompositionGateway/CompositionGateway.csproj index a563855d..d3487a62 100644 --- a/ASP.Net Core API Gateway - 01/CompositionGateway/CompositionGateway.csproj +++ b/ASP.Net Core API Gateway - 01/CompositionGateway/CompositionGateway.csproj @@ -4,7 +4,7 @@ net8.0 - + diff --git a/ASP.Net Core API Gateway - 01/CompositionGateway/Startup.cs b/ASP.Net Core API Gateway - 01/CompositionGateway/Startup.cs index 42cb9d8d..197d72de 100644 --- a/ASP.Net Core API Gateway - 01/CompositionGateway/Startup.cs +++ b/ASP.Net Core API Gateway - 01/CompositionGateway/Startup.cs @@ -9,9 +9,11 @@ public class Startup { public void ConfigureServices(IServiceCollection services) { + services.AddHttpContextAccessor(); services.AddHttpClient(); services.AddRouting(); services.AddViewModelComposition(); + services.AddControllers(); } public void Configure(IApplicationBuilder app, ILoggerFactory loggerFactory) diff --git a/ASP.Net Core API Gateway - 01/ConfigurationUtils/ConfigurationUtils.csproj b/ASP.Net Core API Gateway - 01/ConfigurationUtils/ConfigurationUtils.csproj index c8f39510..17c4f55f 100644 --- a/ASP.Net Core API Gateway - 01/ConfigurationUtils/ConfigurationUtils.csproj +++ b/ASP.Net Core API Gateway - 01/ConfigurationUtils/ConfigurationUtils.csproj @@ -5,7 +5,7 @@ - + diff --git a/ASP.Net Core API Gateway - 01/Sales.ViewModelComposition/Sales.ViewModelComposition.csproj b/ASP.Net Core API Gateway - 01/Sales.ViewModelComposition/Sales.ViewModelComposition.csproj index 40317540..f42773a4 100644 --- a/ASP.Net Core API Gateway - 01/Sales.ViewModelComposition/Sales.ViewModelComposition.csproj +++ b/ASP.Net Core API Gateway - 01/Sales.ViewModelComposition/Sales.ViewModelComposition.csproj @@ -6,7 +6,7 @@ - + diff --git a/ASP.Net Core API Gateway - 01/Shipping.ViewModelComposition/CompositionHandlers/ProductDetailsCompositionHandler.cs b/ASP.Net Core API Gateway - 01/Shipping.ViewModelComposition/CompositionHandlers/ProductDetailsCompositionHandler.cs new file mode 100644 index 00000000..ef872a1d --- /dev/null +++ b/ASP.Net Core API Gateway - 01/Shipping.ViewModelComposition/CompositionHandlers/ProductDetailsCompositionHandler.cs @@ -0,0 +1,29 @@ +using System.Collections.Generic; +using System.Linq; +using System.Net.Http; +using System.Threading.Tasks; +using JsonUtils; +using Microsoft.AspNetCore.Http; +using Microsoft.AspNetCore.Mvc; +using ServiceComposer.AspNetCore; + +namespace Shipping.ViewModelComposition.CompositionHandlers; + +public class ProductDetailsCompositionHandler(HttpClient client, IHttpContextAccessor httpContextAccessor) +{ + [HttpGet("/products/details/{id}")] + public async Task Get(string id) + { + var url = $"/api/shipping-options/product/{id}"; + var response = await client.GetAsync(url); + + dynamic productShippingOptions = await response.Content.AsExpando(); + + var options = ((IEnumerable)productShippingOptions.Options) + .Select(o => o.Option) + .ToArray(); + + var vm = httpContextAccessor.HttpContext.Request.GetComposedResponseModel(); + vm.ProductShippingOptions = string.Join(", ", options); + } +} \ No newline at end of file diff --git a/ASP.Net Core API Gateway - 01/Shipping.ViewModelComposition/ProductDetailsGetHandler.cs b/ASP.Net Core API Gateway - 01/Shipping.ViewModelComposition/ProductDetailsGetHandler.cs deleted file mode 100644 index 5462b0a8..00000000 --- a/ASP.Net Core API Gateway - 01/Shipping.ViewModelComposition/ProductDetailsGetHandler.cs +++ /dev/null @@ -1,40 +0,0 @@ -using Microsoft.AspNetCore.Http; -using Microsoft.AspNetCore.Routing; -using ServiceComposer.AspNetCore; -using JsonUtils; -using System.Collections.Generic; -using System.Linq; -using System.Net.Http; -using System.Threading.Tasks; -using Microsoft.AspNetCore.Mvc; - -namespace Shipping.ViewModelComposition -{ - class ProductDetailsGetHandler : ICompositionRequestsHandler - { - private readonly HttpClient _client; - - public ProductDetailsGetHandler(HttpClient client) - { - _client = client; - } - - [HttpGet("/products/details/{id}")] - public async Task Handle(HttpRequest request) - { - var id = (string)request.HttpContext.GetRouteData().Values["id"]; - - var url = $"/api/shipping-options/product/{id}"; - var response = await _client.GetAsync(url); - - dynamic productShippingOptions = await response.Content.AsExpando(); - - var options = ((IEnumerable)productShippingOptions.Options) - .Select(o => o.Option) - .ToArray(); - - var vm = request.GetComposedResponseModel(); - vm.ProductShippingOptions = string.Join(", ", options); - } - } -} diff --git a/ASP.Net Core API Gateway - 01/Shipping.ViewModelComposition/Shipping.ViewModelComposition.csproj b/ASP.Net Core API Gateway - 01/Shipping.ViewModelComposition/Shipping.ViewModelComposition.csproj index 19c8bb9f..daa19355 100644 --- a/ASP.Net Core API Gateway - 01/Shipping.ViewModelComposition/Shipping.ViewModelComposition.csproj +++ b/ASP.Net Core API Gateway - 01/Shipping.ViewModelComposition/Shipping.ViewModelComposition.csproj @@ -6,7 +6,7 @@ - + diff --git a/ASP.Net Core API Gateway - 01/Shipping.ViewModelComposition/ViewModelCompositionOptionsCustomization.cs b/ASP.Net Core API Gateway - 01/Shipping.ViewModelComposition/ViewModelCompositionOptionsCustomization.cs index 94e6bedc..457b4735 100644 --- a/ASP.Net Core API Gateway - 01/Shipping.ViewModelComposition/ViewModelCompositionOptionsCustomization.cs +++ b/ASP.Net Core API Gateway - 01/Shipping.ViewModelComposition/ViewModelCompositionOptionsCustomization.cs @@ -3,6 +3,7 @@ using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; using ServiceComposer.AspNetCore; +using Shipping.ViewModelComposition.CompositionHandlers; namespace Shipping.ViewModelComposition { @@ -10,7 +11,7 @@ public class ViewModelCompositionOptionsCustomization : IViewModelCompositionOpt { public void Customize(ViewModelCompositionOptions options) { - options.RegisterHttpClient((serviceProvider, httpClient) => + options.RegisterHttpClient((serviceProvider, httpClient) => { var configuration = serviceProvider.GetService(); var baseAddress = configuration?.GetSection("Shipping:BaseAddress").Value ?? "http://localhost:5004"; diff --git a/ASP.Net Core API Gateway - 01/Warehouse.ViewModelComposition/CompositionHandlers/ProductDetailsCompositionHandler.cs b/ASP.Net Core API Gateway - 01/Warehouse.ViewModelComposition/CompositionHandlers/ProductDetailsCompositionHandler.cs new file mode 100644 index 00000000..5509f3f9 --- /dev/null +++ b/ASP.Net Core API Gateway - 01/Warehouse.ViewModelComposition/CompositionHandlers/ProductDetailsCompositionHandler.cs @@ -0,0 +1,24 @@ +using System.Net.Http; +using System.Threading.Tasks; +using JsonUtils; +using Microsoft.AspNetCore.Http; +using Microsoft.AspNetCore.Mvc; +using ServiceComposer.AspNetCore; + +namespace Warehouse.ViewModelComposition.CompositionHandlers; + +public class ProductDetailsCompositionHandler(HttpClient client, IHttpContextAccessor httpContextAccessor) +{ + [HttpGet("/products/details/{id}")] + public async Task Get(string id) + { + var url = $"/api/inventory/product/{id}"; + var response = await client.GetAsync(url); + + dynamic stockItem = await response.Content.AsExpando(); + + dynamic vm = httpContextAccessor.HttpContext.Request.GetComposedResponseModel(); + vm.ProductInventory = stockItem.Inventory; + vm.ProductOutOfStock = stockItem.Inventory == 0; + } +} \ No newline at end of file diff --git a/ASP.Net Core API Gateway - 01/Warehouse.ViewModelComposition/ProductDetailsGetHandler.cs b/ASP.Net Core API Gateway - 01/Warehouse.ViewModelComposition/ProductDetailsGetHandler.cs deleted file mode 100644 index e8c050b6..00000000 --- a/ASP.Net Core API Gateway - 01/Warehouse.ViewModelComposition/ProductDetailsGetHandler.cs +++ /dev/null @@ -1,35 +0,0 @@ -using Microsoft.AspNetCore.Http; -using Microsoft.AspNetCore.Routing; -using ServiceComposer.AspNetCore; -using JsonUtils; -using System.Net.Http; -using System.Threading.Tasks; -using Microsoft.AspNetCore.Mvc; - -namespace Warehouse.ViewModelComposition -{ - class ProductDetailsGetHandler : ICompositionRequestsHandler - { - readonly HttpClient _client; - - public ProductDetailsGetHandler(HttpClient client) - { - _client = client; - } - - [HttpGet("/products/details/{id}")] - public async Task Handle(HttpRequest request) - { - var id = (string)request.HttpContext.GetRouteData().Values["id"]; - - var url = $"/api/inventory/product/{id}"; - var response = await _client.GetAsync(url); - - dynamic stockItem = await response.Content.AsExpando(); - - dynamic vm = request.GetComposedResponseModel(); - vm.ProductInventory = stockItem.Inventory; - vm.ProductOutOfStock = stockItem.Inventory == 0; - } - } -} diff --git a/ASP.Net Core API Gateway - 01/Warehouse.ViewModelComposition/ViewModelCompositionOptionsCustomization.cs b/ASP.Net Core API Gateway - 01/Warehouse.ViewModelComposition/ViewModelCompositionOptionsCustomization.cs index ffe9607f..96acc704 100644 --- a/ASP.Net Core API Gateway - 01/Warehouse.ViewModelComposition/ViewModelCompositionOptionsCustomization.cs +++ b/ASP.Net Core API Gateway - 01/Warehouse.ViewModelComposition/ViewModelCompositionOptionsCustomization.cs @@ -1,7 +1,6 @@ -using System; -using ConfigurationUtils; -using Microsoft.Extensions.DependencyInjection; +using ConfigurationUtils; using ServiceComposer.AspNetCore; +using Warehouse.ViewModelComposition.CompositionHandlers; namespace Warehouse.ViewModelComposition { @@ -9,7 +8,7 @@ public class ViewModelCompositionOptionsCustomization : IViewModelCompositionOpt { public void Customize(ViewModelCompositionOptions options) { - options.RegisterHttpClient("http://localhost:5003"); + options.RegisterHttpClient("http://localhost:5003"); } } } \ No newline at end of file diff --git a/ASP.Net Core API Gateway - 01/Warehouse.ViewModelComposition/Warehouse.ViewModelComposition.csproj b/ASP.Net Core API Gateway - 01/Warehouse.ViewModelComposition/Warehouse.ViewModelComposition.csproj index 40317540..f42773a4 100644 --- a/ASP.Net Core API Gateway - 01/Warehouse.ViewModelComposition/Warehouse.ViewModelComposition.csproj +++ b/ASP.Net Core API Gateway - 01/Warehouse.ViewModelComposition/Warehouse.ViewModelComposition.csproj @@ -6,7 +6,7 @@ - + diff --git a/ASP.Net Core API Gateway - 01/nuget.config b/ASP.Net Core API Gateway - 01/nuget.config index 319bc405..c6e68375 100644 --- a/ASP.Net Core API Gateway - 01/nuget.config +++ b/ASP.Net Core API Gateway - 01/nuget.config @@ -15,5 +15,8 @@ + + + \ No newline at end of file