From 83c697372b445b5ca6f2b89e46d6fa177ddf25e6 Mon Sep 17 00:00:00 2001 From: Bill Wagner Date: Thu, 23 Mar 2017 14:51:13 -0400 Subject: [PATCH] edit using the mock service works --- .../Services/Catalog/CatalogMockService.cs | 29 +++++++++++++++---- .../Services/Catalog/ICatalogService.cs | 4 ++- .../Catalog.WebForms/Default.aspx.cs | 2 +- .../Catalog.WebForms/EditCatalogItem.aspx | 2 +- .../Catalog.WebForms/EditCatalogItem.aspx.cs | 10 +++---- .../Services/CatalogService.cs | 16 ++++++++-- 6 files changed, 47 insertions(+), 16 deletions(-) diff --git a/src/Mobile/eShopOnContainers/eShopOnContainers.Core/Services/Catalog/CatalogMockService.cs b/src/Mobile/eShopOnContainers/eShopOnContainers.Core/Services/Catalog/CatalogMockService.cs index f9c2cc9c0..82eac3105 100644 --- a/src/Mobile/eShopOnContainers/eShopOnContainers.Core/Services/Catalog/CatalogMockService.cs +++ b/src/Mobile/eShopOnContainers/eShopOnContainers.Core/Services/Catalog/CatalogMockService.cs @@ -10,19 +10,21 @@ namespace eShopOnContainers.Core.Services.Catalog { public class CatalogMockService : ICatalogService { - private ObservableCollection MockCatalogBrand = new ObservableCollection + // Change to static so these remain + // in scope between requests: + private static ObservableCollection MockCatalogBrand = new ObservableCollection { new CatalogBrand { Id = 1, Brand = "Azure" }, new CatalogBrand { Id = 2, Brand = "Visual Studio" } }; - private ObservableCollection MockCatalogType = new ObservableCollection + private static ObservableCollection MockCatalogType = new ObservableCollection { new CatalogType { Id = 1, Type = "Mug" }, new CatalogType { Id = 2, Type = "T-Shirt" } }; - private ObservableCollection MockCatalog = new ObservableCollection + private static ObservableCollection MockCatalog = new ObservableCollection { new CatalogItem { Id = Common.Common.MockCatalogItemId01, PictureUri = Device.OS != TargetPlatform.Windows ? "fake_product_01.png" : "Assets/fake_product_01.png", Name = ".NET Bot Blue Sweatshirt (M)", Price = 19.50M, CatalogBrandId = 2, CatalogBrand = "Visual Studio", CatalogTypeId = 2, CatalogType = "T-Shirt" }, new CatalogItem { Id = Common.Common.MockCatalogItemId02, PictureUri = Device.OS != TargetPlatform.Windows ? "fake_product_02.png" : "Assets/fake_product_02.png", Name = ".NET Bot Purple Sweatshirt (M)", Price = 19.50M, CatalogBrandId = 2, CatalogBrand = "Visual Studio", CatalogTypeId = 2, CatalogType = "T-Shirt" }, @@ -35,7 +37,8 @@ namespace eShopOnContainers.Core.Services.Catalog { await Task.Delay(500); - return MockCatalog; + // copy so that edits must be changed here to take affect: + return new ObservableCollection(MockCatalog); } public async Task> FilterAsync(int catalogBrandId, int catalogTypeId) @@ -68,7 +71,7 @@ namespace eShopOnContainers.Core.Services.Catalog return MockCatalog.FirstOrDefault(c => c.Id == id); } - public async Task DeleteCatalogItem(string catalogItemId) + public async Task DeleteCatalogItemAsync(string catalogItemId) { var itemToRemove = MockCatalog.FirstOrDefault(c => c.Id == catalogItemId); await Task.Delay(500); @@ -77,5 +80,21 @@ namespace eShopOnContainers.Core.Services.Catalog else throw new ArgumentException(message: "item could not be found", paramName: nameof(catalogItemId)); } + + public async Task UpdateCatalogItemAsync(CatalogItem item) + { + var itemToChange = MockCatalog.FirstOrDefault(c => item.Id == c.Id); + await Task.Delay(500); + if (itemToChange != null) + { + itemToChange.CatalogBrandId = item.CatalogBrandId; + itemToChange.CatalogTypeId = item.CatalogTypeId; + itemToChange.Description = item.Description; + itemToChange.Name = item.Name; + itemToChange.Price = item.Price; + } + return itemToChange; + } + } } \ No newline at end of file diff --git a/src/Mobile/eShopOnContainers/eShopOnContainers.Core/Services/Catalog/ICatalogService.cs b/src/Mobile/eShopOnContainers/eShopOnContainers.Core/Services/Catalog/ICatalogService.cs index 746d5e267..70de68c0f 100644 --- a/src/Mobile/eShopOnContainers/eShopOnContainers.Core/Services/Catalog/ICatalogService.cs +++ b/src/Mobile/eShopOnContainers/eShopOnContainers.Core/Services/Catalog/ICatalogService.cs @@ -11,6 +11,8 @@ namespace eShopOnContainers.Core.Services.Catalog Task> GetCatalogTypeAsync(); Task> GetCatalogAsync(); Task GetCatalogItemAsync(string id); - Task DeleteCatalogItem(string catalogItemId); + Task DeleteCatalogItemAsync(string catalogItemId); + Task UpdateCatalogItemAsync(CatalogItem item); + } } diff --git a/src/Web/Catalog.WebForms/Catalog.WebForms/Default.aspx.cs b/src/Web/Catalog.WebForms/Catalog.WebForms/Default.aspx.cs index 2587b91f0..711437104 100644 --- a/src/Web/Catalog.WebForms/Catalog.WebForms/Default.aspx.cs +++ b/src/Web/Catalog.WebForms/Catalog.WebForms/Default.aspx.cs @@ -36,7 +36,7 @@ namespace eShopOnContainers.Catalog.WebForms public Task DeleteCatalogItemAsync(int id) { - return catalog?.DeleteCatalogItem(id.ToString()); + return catalog?.DeleteCatalogItemAsync(id.ToString()); } } } \ No newline at end of file diff --git a/src/Web/Catalog.WebForms/Catalog.WebForms/EditCatalogItem.aspx b/src/Web/Catalog.WebForms/Catalog.WebForms/EditCatalogItem.aspx index d827e1a56..af932d2d3 100644 --- a/src/Web/Catalog.WebForms/Catalog.WebForms/EditCatalogItem.aspx +++ b/src/Web/Catalog.WebForms/Catalog.WebForms/EditCatalogItem.aspx @@ -5,7 +5,7 @@ ItemType="eShopOnContainers.Core.Models.Catalog.CatalogItem" DataKeyNames="Id" SelectMethod="GetCatalogItemAsync" UpdateMethod="UpdateCatalogItemAsync" - InsertMethod="InsertCatalogItemAsync" + InsertMethod="InsertCatalogItemAsync" CssClass="table-compact table-full-width">
diff --git a/src/Web/Catalog.WebForms/Catalog.WebForms/EditCatalogItem.aspx.cs b/src/Web/Catalog.WebForms/Catalog.WebForms/EditCatalogItem.aspx.cs index 45fcd69d3..88382a4b7 100644 --- a/src/Web/Catalog.WebForms/Catalog.WebForms/EditCatalogItem.aspx.cs +++ b/src/Web/Catalog.WebForms/Catalog.WebForms/EditCatalogItem.aspx.cs @@ -47,21 +47,19 @@ namespace eShopOnContainers.Catalog.WebForms } // The id parameter name should match the DataKeyNames value set on the control - public void UpdateCatalogItemAsync(int id) + public async Task UpdateCatalogItemAsync(int id) { - CatalogItem item = null; - // Load the item here, e.g. item = MyDataLayer.Find(id); + CatalogItem item = await catalog?.GetCatalogItemAsync(id.ToString()); if (item == null) { // The item wasn't found ModelState.AddModelError("", String.Format("Item with id {0} was not found", id)); return; } - if (TryUpdateModel(item) && (ModelState.IsValid)) { - // Save changes here, e.g. MyDataLayer.SaveChanges(); - // Send the item to the Catalog Service. + await catalog?.UpdateCatalogItemAsync(item); + Response.Redirect("~"); } } diff --git a/src/Web/Catalog.WebForms/Catalog.WebForms/Services/CatalogService.cs b/src/Web/Catalog.WebForms/Catalog.WebForms/Services/CatalogService.cs index a1016f456..88e1cd97f 100644 --- a/src/Web/Catalog.WebForms/Catalog.WebForms/Services/CatalogService.cs +++ b/src/Web/Catalog.WebForms/Catalog.WebForms/Services/CatalogService.cs @@ -128,7 +128,7 @@ namespace eShopOnContainers.Core.Services.Catalog } } - public async Task DeleteCatalogItem(string catalogItemId) + public Task DeleteCatalogItemAsync(string catalogItemId) { // TODO: UriBuilder builder = new UriBuilder("" /* GlobalSetting.Instance.CatalogEndpoint */); @@ -137,7 +137,19 @@ namespace eShopOnContainers.Core.Services.Catalog string uri = builder.ToString(); - await _requestProvider.DeleteAsync(uri); + return _requestProvider.DeleteAsync(uri); + } + + public Task UpdateCatalogItemAsync(CatalogItem item) + { + // TODO: + UriBuilder builder = new UriBuilder("" /* GlobalSetting.Instance.CatalogEndpoint */); + + builder.Path = "api/v1/catalog/edit"; + + string uri = builder.ToString(); + + return _requestProvider.PostAsync(uri, item); } } }