From 0716bff126234897ef51e7a3ec6504b6fb3b2c84 Mon Sep 17 00:00:00 2001 From: dsanz Date: Wed, 1 Mar 2017 17:57:29 +0100 Subject: [PATCH] Removed hardcoded addresses for persisted picture uris --- .../WebMonolithic/docker-compose.override.yml | 1 + .../WebMonolithic/eShopWeb/CatalogSettings.cs | 7 +++ .../Infrastructure/CatalogContextSeed.cs | 24 ++++----- .../eShopWeb/ScaffoldingReadMe.txt | 49 ------------------- .../eShopWeb/Services/CatalogService.cs | 20 +++++++- src/Web/WebMonolithic/eShopWeb/Startup.cs | 1 + .../WebMonolithic/eShopWeb/appsettings.json | 1 + 7 files changed, 41 insertions(+), 62 deletions(-) create mode 100644 src/Web/WebMonolithic/eShopWeb/CatalogSettings.cs delete mode 100644 src/Web/WebMonolithic/eShopWeb/ScaffoldingReadMe.txt diff --git a/src/Web/WebMonolithic/docker-compose.override.yml b/src/Web/WebMonolithic/docker-compose.override.yml index 8822a5f63..e908c1de9 100644 --- a/src/Web/WebMonolithic/docker-compose.override.yml +++ b/src/Web/WebMonolithic/docker-compose.override.yml @@ -5,6 +5,7 @@ services: environment: - ASPNETCORE_ENVIRONMENT=Development - ConnectionString=Server=sql.data;Database=Microsoft.eShopOnContainers.Services.CatalogDb;User Id=sa;Password=Pass@word + - CatalogBaseUrl=http://localhost:5106 ports: - "5106:5106" diff --git a/src/Web/WebMonolithic/eShopWeb/CatalogSettings.cs b/src/Web/WebMonolithic/eShopWeb/CatalogSettings.cs new file mode 100644 index 000000000..d11c487b5 --- /dev/null +++ b/src/Web/WebMonolithic/eShopWeb/CatalogSettings.cs @@ -0,0 +1,7 @@ +namespace eShopWeb +{ + public class CatalogSettings + { + public string CatalogBaseUrl { get; set; } + } +} diff --git a/src/Web/WebMonolithic/eShopWeb/Infrastructure/CatalogContextSeed.cs b/src/Web/WebMonolithic/eShopWeb/Infrastructure/CatalogContextSeed.cs index 212d52285..66b9e50e6 100644 --- a/src/Web/WebMonolithic/eShopWeb/Infrastructure/CatalogContextSeed.cs +++ b/src/Web/WebMonolithic/eShopWeb/Infrastructure/CatalogContextSeed.cs @@ -85,18 +85,18 @@ { return new List() { - new CatalogItem() { CatalogTypeId=2,CatalogBrandId=2, Description = ".NET Bot Black Sweatshirt", Name = ".NET Bot Black Sweatshirt", Price = 19.5M, PictureUri = "http://localhost:5106/catalog/pic/1" }, - new CatalogItem() { CatalogTypeId=1,CatalogBrandId=2, Description = ".NET Black & White Mug", Name = ".NET Black & White Mug", Price= 8.50M, PictureUri = "http://localhost:5106/catalog/pic/2" }, - new CatalogItem() { CatalogTypeId=2,CatalogBrandId=5, Description = "Prism White T-Shirt", Name = "Prism White T-Shirt", Price = 12, PictureUri = "http://localhost:5106/catalog/pic/3" }, - new CatalogItem() { CatalogTypeId=2,CatalogBrandId=2, Description = ".NET Foundation Sweatshirt", Name = ".NET Foundation Sweatshirt", Price = 12, PictureUri = "http://localhost:5106/catalog/pic/4" }, - new CatalogItem() { CatalogTypeId=3,CatalogBrandId=5, Description = "Roslyn Red Sheet", Name = "Roslyn Red Sheet", Price = 8.5M, PictureUri = "http://localhost:5106/catalog/pic/5" }, - new CatalogItem() { CatalogTypeId=2,CatalogBrandId=2, Description = ".NET Blue Sweatshirt", Name = ".NET Blue Sweatshirt", Price = 12, PictureUri = "http://localhost:5106/catalog/pic/6" }, - new CatalogItem() { CatalogTypeId=2,CatalogBrandId=5, Description = "Roslyn Red T-Shirt", Name = "Roslyn Red T-Shirt", Price = 12, PictureUri = "http://localhost:5106/catalog/pic/7" }, - new CatalogItem() { CatalogTypeId=2,CatalogBrandId=5, Description = "Kudu Purple Sweatshirt", Name = "Kudu Purple Sweatshirt", Price = 8.5M, PictureUri = "http://localhost:5106/catalog/pic/8" }, - new CatalogItem() { CatalogTypeId=1,CatalogBrandId=5, Description = "Cup White Mug", Name = "Cup White Mug", Price = 12, PictureUri = "http://localhost:5106/catalog/pic/9" }, - new CatalogItem() { CatalogTypeId=3,CatalogBrandId=2, Description = ".NET Foundation Sheet", Name = ".NET Foundation Sheet", Price = 12, PictureUri = "http://localhost:5106/catalog/pic/10" }, - new CatalogItem() { CatalogTypeId=3,CatalogBrandId=2, Description = "Cup Sheet", Name = "Cup Sheet", Price = 8.5M, PictureUri = "http://localhost:5106/catalog/pic/11" }, - new CatalogItem() { CatalogTypeId=2,CatalogBrandId=5, Description = "Prism White TShirt", Name = "Prism White TShirt", Price = 12, PictureUri = "http://localhost:5106/catalog/pic/12" } + new CatalogItem() { CatalogTypeId=2,CatalogBrandId=2, Description = ".NET Bot Black Sweatshirt", Name = ".NET Bot Black Sweatshirt", Price = 19.5M, PictureUri = "http://catalogbaseurltobereplaced/catalog/pic/1" }, + new CatalogItem() { CatalogTypeId=1,CatalogBrandId=2, Description = ".NET Black & White Mug", Name = ".NET Black & White Mug", Price= 8.50M, PictureUri = "http://catalogbaseurltobereplaced/catalog/pic/2" }, + new CatalogItem() { CatalogTypeId=2,CatalogBrandId=5, Description = "Prism White T-Shirt", Name = "Prism White T-Shirt", Price = 12, PictureUri = "http://catalogbaseurltobereplaced/catalog/pic/3" }, + new CatalogItem() { CatalogTypeId=2,CatalogBrandId=2, Description = ".NET Foundation Sweatshirt", Name = ".NET Foundation Sweatshirt", Price = 12, PictureUri = "http://catalogbaseurltobereplaced/catalog/pic/4" }, + new CatalogItem() { CatalogTypeId=3,CatalogBrandId=5, Description = "Roslyn Red Sheet", Name = "Roslyn Red Sheet", Price = 8.5M, PictureUri = "http://catalogbaseurltobereplaced/catalog/pic/5" }, + new CatalogItem() { CatalogTypeId=2,CatalogBrandId=2, Description = ".NET Blue Sweatshirt", Name = ".NET Blue Sweatshirt", Price = 12, PictureUri = "http://catalogbaseurltobereplaced/catalog/pic/6" }, + new CatalogItem() { CatalogTypeId=2,CatalogBrandId=5, Description = "Roslyn Red T-Shirt", Name = "Roslyn Red T-Shirt", Price = 12, PictureUri = "http://catalogbaseurltobereplaced/catalog/pic/7" }, + new CatalogItem() { CatalogTypeId=2,CatalogBrandId=5, Description = "Kudu Purple Sweatshirt", Name = "Kudu Purple Sweatshirt", Price = 8.5M, PictureUri = "http://catalogbaseurltobereplaced/catalog/pic/8" }, + new CatalogItem() { CatalogTypeId=1,CatalogBrandId=5, Description = "Cup White Mug", Name = "Cup White Mug", Price = 12, PictureUri = "http://catalogbaseurltobereplaced/catalog/pic/9" }, + new CatalogItem() { CatalogTypeId=3,CatalogBrandId=2, Description = ".NET Foundation Sheet", Name = ".NET Foundation Sheet", Price = 12, PictureUri = "http://catalogbaseurltobereplaced/catalog/pic/10" }, + new CatalogItem() { CatalogTypeId=3,CatalogBrandId=2, Description = "Cup Sheet", Name = "Cup Sheet", Price = 8.5M, PictureUri = "http://catalogbaseurltobereplaced/catalog/pic/11" }, + new CatalogItem() { CatalogTypeId=2,CatalogBrandId=5, Description = "Prism White TShirt", Name = "Prism White TShirt", Price = 12, PictureUri = "http://catalogbaseurltobereplaced/catalog/pic/12" } }; } } diff --git a/src/Web/WebMonolithic/eShopWeb/ScaffoldingReadMe.txt b/src/Web/WebMonolithic/eShopWeb/ScaffoldingReadMe.txt deleted file mode 100644 index 1bad8d29e..000000000 --- a/src/Web/WebMonolithic/eShopWeb/ScaffoldingReadMe.txt +++ /dev/null @@ -1,49 +0,0 @@ - -ASP.NET MVC core dependencies have been added to the project. -However you may still need to do make changes to your project. - -1. Suggested changes to Startup class: - 1.1 Add a constructor: - public IConfigurationRoot Configuration { get; } - - public Startup(IHostingEnvironment env) - { - var builder = new ConfigurationBuilder() - .SetBasePath(env.ContentRootPath) - .AddJsonFile("appsettings.json", optional: true, reloadOnChange: true) - .AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true) - .AddEnvironmentVariables(); - Configuration = builder.Build(); - } - 1.2 Add MVC services: - public void ConfigureServices(IServiceCollection services) - { - // Add framework services. - services.AddMvc(); - } - - 1.3 Configure web app to use use Configuration and use MVC routing: - - public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory) - { - loggerFactory.AddConsole(Configuration.GetSection("Logging")); - loggerFactory.AddDebug(); - - if (env.IsDevelopment()) - { - app.UseDeveloperExceptionPage(); - } - else - { - app.UseExceptionHandler("/Home/Error"); - } - - app.UseStaticFiles(); - - app.UseMvc(routes => - { - routes.MapRoute( - name: "default", - template: "{controller=Home}/{action=Index}/{id?}"); - }); - } diff --git a/src/Web/WebMonolithic/eShopWeb/Services/CatalogService.cs b/src/Web/WebMonolithic/eShopWeb/Services/CatalogService.cs index a5e9934a2..5d930b116 100644 --- a/src/Web/WebMonolithic/eShopWeb/Services/CatalogService.cs +++ b/src/Web/WebMonolithic/eShopWeb/Services/CatalogService.cs @@ -6,15 +6,20 @@ using eShopWeb.Models; using Microsoft.AspNetCore.Mvc.Rendering; using eShopWeb.Infrastructure; using Microsoft.EntityFrameworkCore; +using Microsoft.Extensions.Options; +using Microsoft.Extensions.Configuration; namespace eShopWeb.Services { public class CatalogService : ICatalogService { private readonly CatalogContext _context; - public CatalogService(CatalogContext context) + private readonly IOptionsSnapshot _settings; + + public CatalogService(CatalogContext context, IOptionsSnapshot settings) { _context = context; + _settings = settings; } public async Task GetCatalogItems(int pageIndex, int itemsPage, int? brandId, int? typeId) @@ -39,6 +44,8 @@ namespace eShopWeb.Services .Take(itemsPage) .ToListAsync(); + itemsOnPage = ComposePicUri(itemsOnPage); + return new Catalog() { Data = itemsOnPage, PageIndex = pageIndex, Count = (int)totalItems }; } @@ -67,5 +74,16 @@ namespace eShopWeb.Services return items; } + + private List ComposePicUri(List items) + { + var baseUri = _settings.Value.CatalogBaseUrl; + items.ForEach(x => + { + x.PictureUri = x.PictureUri.Replace("http://catalogbaseurltobereplaced", baseUri); + }); + + return items; + } } } diff --git a/src/Web/WebMonolithic/eShopWeb/Startup.cs b/src/Web/WebMonolithic/eShopWeb/Startup.cs index ad074f49d..dd05db55f 100644 --- a/src/Web/WebMonolithic/eShopWeb/Startup.cs +++ b/src/Web/WebMonolithic/eShopWeb/Startup.cs @@ -46,6 +46,7 @@ namespace eShopWeb }); services.AddTransient(); + services.Configure(Configuration); services.AddMvc(); } diff --git a/src/Web/WebMonolithic/eShopWeb/appsettings.json b/src/Web/WebMonolithic/eShopWeb/appsettings.json index f0ecd70d3..30708d497 100644 --- a/src/Web/WebMonolithic/eShopWeb/appsettings.json +++ b/src/Web/WebMonolithic/eShopWeb/appsettings.json @@ -1,5 +1,6 @@ { "ConnectionString": "Server=tcp:127.0.0.1,5433;Initial Catalog=Microsoft.eShopOnContainers.Services.CatalogDb;User Id=sa;Password=Pass@word", + "CatalogBaseUrl": "http://localhost:5106", "Logging": { "IncludeScopes": false, "LogLevel": {