diff --git a/src/Mobile/eShopOnContainers/eShopOnContainers.Core/Services/Catalog/CatalogMockService.cs b/src/Mobile/eShopOnContainers/eShopOnContainers.Core/Services/Catalog/CatalogMockService.cs index d58b2b698..f9c2cc9c0 100644 --- a/src/Mobile/eShopOnContainers/eShopOnContainers.Core/Services/Catalog/CatalogMockService.cs +++ b/src/Mobile/eShopOnContainers/eShopOnContainers.Core/Services/Catalog/CatalogMockService.cs @@ -61,5 +61,21 @@ namespace eShopOnContainers.Core.Services.Catalog return MockCatalogType; } + public async Task GetCatalogItemAsync(string id) + { + await Task.Delay(500); + + return MockCatalog.FirstOrDefault(c => c.Id == id); + } + + public async Task DeleteCatalogItem(string catalogItemId) + { + var itemToRemove = MockCatalog.FirstOrDefault(c => c.Id == catalogItemId); + await Task.Delay(500); + if (itemToRemove != null) + MockCatalog.Remove(itemToRemove); + else + throw new ArgumentException(message: "item could not be found", paramName: nameof(catalogItemId)); + } } } \ 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 74d6aa91b..746d5e267 100644 --- a/src/Mobile/eShopOnContainers/eShopOnContainers.Core/Services/Catalog/ICatalogService.cs +++ b/src/Mobile/eShopOnContainers/eShopOnContainers.Core/Services/Catalog/ICatalogService.cs @@ -10,5 +10,7 @@ namespace eShopOnContainers.Core.Services.Catalog Task> FilterAsync(int catalogBrandId, int catalogTypeId); Task> GetCatalogTypeAsync(); Task> GetCatalogAsync(); + Task GetCatalogItemAsync(string id); + Task DeleteCatalogItem(string catalogItemId); } } diff --git a/src/Web/Catalog.WebForms/Catalog.WebForms/Default.aspx.cs b/src/Web/Catalog.WebForms/Catalog.WebForms/Default.aspx.cs index a74ed8e82..2587b91f0 100644 --- a/src/Web/Catalog.WebForms/Catalog.WebForms/Default.aspx.cs +++ b/src/Web/Catalog.WebForms/Catalog.WebForms/Default.aspx.cs @@ -17,10 +17,8 @@ namespace eShopOnContainers.Catalog.WebForms protected _Default() { } - public _Default(ICatalogService catalog) - { + public _Default(ICatalogService catalog) => this.catalog = catalog; - } protected void Page_Load(object sender, EventArgs e) { @@ -33,15 +31,12 @@ namespace eShopOnContainers.Catalog.WebForms // int startRowIndex // out int totalRowCount // string sortByExpression - public async Task> GetCatalogDataAsync() - { - return await catalog?.GetCatalogAsync(); - } + public async Task> GetCatalogDataAsync() => + (await catalog?.GetCatalogAsync()).AsEnumerable(); - // The id parameter name should match the DataKeyNames value set on the control - public async Task DeleteCatalogItemAsync(int id) + public Task DeleteCatalogItemAsync(int id) { - //TODO: Call the service. + return catalog?.DeleteCatalogItem(id.ToString()); } } } \ No newline at end of file diff --git a/src/Web/Catalog.WebForms/Catalog.WebForms/Services/CatalogService.cs b/src/Web/Catalog.WebForms/Catalog.WebForms/Services/CatalogService.cs new file mode 100644 index 000000000..a1016f456 --- /dev/null +++ b/src/Web/Catalog.WebForms/Catalog.WebForms/Services/CatalogService.cs @@ -0,0 +1,143 @@ +using System; +using System.Collections.ObjectModel; +using System.Threading.Tasks; +using eShopOnContainers.Core.Models.Catalog; +using eShopOnContainers.Core.Services.RequestProvider; +using eShopOnContainers.Core.Extensions; +using System.Collections.Generic; + +// from https://github.com/dotnet/eShopOnContainers/blob/vs2017/src/Mobile/eShopOnContainers/eShopOnContainers.Core/Services/Catalog/CatalogService.cs +// TODO: DRY this stuff. +namespace eShopOnContainers.Core.Services.Catalog +{ + public class CatalogService : ICatalogService + { + private readonly IRequestProvider _requestProvider; + + public CatalogService(IRequestProvider requestProvider) + { + _requestProvider = requestProvider; + } + + public async Task> FilterAsync(int catalogBrandId, int catalogTypeId) + { + try + { + // TODO: + UriBuilder builder = new UriBuilder("" /* GlobalSetting.Instance.CatalogEndpoint */); + + builder.Path = string.Format("api/v1/catalog/items/type/{0}/brand/{1}", catalogTypeId, catalogBrandId); + + string uri = builder.ToString(); + + CatalogRoot catalog = + await _requestProvider.GetAsync(uri); + + if (catalog?.Data != null) + return catalog?.Data.ToObservableCollection(); + else + return new ObservableCollection(); + } + catch + { + return new ObservableCollection(); + } + } + + public async Task> GetCatalogAsync() + { + try + { + // TODO: + UriBuilder builder = new UriBuilder("" /* GlobalSetting.Instance.CatalogEndpoint */); + + builder.Path = "api/v1/catalog/items"; + + string uri = builder.ToString(); + + CatalogRoot catalog = + await _requestProvider.GetAsync(uri); + + if (catalog?.Data != null) + { + // TODO: ServicesHelper.FixCatalogItemPictureUri(catalog?.Data); + + return catalog?.Data.ToObservableCollection(); + } + else + return new ObservableCollection(); + } + catch + { + return new ObservableCollection(); + } + } + + public Task GetCatalogItemAsync(string id) + { + throw new NotImplementedException(); + } + + public async Task> GetCatalogBrandAsync() + { + try + { + // TODO: + UriBuilder builder = new UriBuilder("" /* GlobalSetting.Instance.CatalogEndpoint */); + + builder.Path = "api/v1/catalog/catalogbrands"; + + string uri = builder.ToString(); + + IEnumerable brands = + await _requestProvider.GetAsync>(uri); + + if (brands != null) + return brands?.ToObservableCollection(); + else + return new ObservableCollection(); + } + catch + { + return new ObservableCollection(); + } + } + + public async Task> GetCatalogTypeAsync() + { + try + { + // TODO: + UriBuilder builder = new UriBuilder("" /* GlobalSetting.Instance.CatalogEndpoint */); + + builder.Path = "api/v1/catalog/catalogtypes"; + + string uri = builder.ToString(); + + IEnumerable types = + await _requestProvider.GetAsync>(uri); + + if (types != null) + return types.ToObservableCollection(); + else + return new ObservableCollection(); + } + catch + { + return new ObservableCollection(); + } + } + + public async Task DeleteCatalogItem(string catalogItemId) + { + // TODO: + UriBuilder builder = new UriBuilder("" /* GlobalSetting.Instance.CatalogEndpoint */); + + builder.Path = $"api/v1/catalog/{catalogItemId}"; + + string uri = builder.ToString(); + + await _requestProvider.DeleteAsync(uri); + } + } +}