From 9229a8d0fd245c5606dc7ee1c4fc0219212d96eb Mon Sep 17 00:00:00 2001 From: Shaun Walker Date: Wed, 21 Jun 2017 15:27:12 -0700 Subject: [PATCH] support for additional CatalogItem properties --- .../Infrastructure/CatalogContextSeed.cs | 97 +++++++++++++++++-- .../Catalog.API/Setup/CatalogItems.csv | 26 ++--- 2 files changed, 101 insertions(+), 22 deletions(-) diff --git a/src/Services/Catalog/Catalog.API/Infrastructure/CatalogContextSeed.cs b/src/Services/Catalog/Catalog.API/Infrastructure/CatalogContextSeed.cs index ff3625857..82e31eb68 100644 --- a/src/Services/Catalog/Catalog.API/Infrastructure/CatalogContextSeed.cs +++ b/src/Services/Catalog/Catalog.API/Infrastructure/CatalogContextSeed.cs @@ -81,7 +81,7 @@ try { string[] requiredHeaders = { "catalogbrand" }; - csvheaders = GetHeaders(requiredHeaders, csvFileCatalogBrands); + csvheaders = GetHeaders( csvFileCatalogBrands, requiredHeaders ); } catch (Exception ex) { @@ -136,7 +136,7 @@ try { string[] requiredHeaders = { "catalogtype" }; - csvheaders = GetHeaders(requiredHeaders, csvFileCatalogTypes); + csvheaders = GetHeaders( csvFileCatalogTypes, requiredHeaders ); } catch (Exception ex) { @@ -188,7 +188,8 @@ try { string[] requiredHeaders = { "catalogtypename", "catalogbrandname", "description", "name", "price", "pictureuri" }; - csvheaders = GetHeaders(requiredHeaders, csvFileCatalogItems); + string[] optionalheaders = { "availablestock", "restockThreshold", "maxStockThreshold", "onreorder" }; + csvheaders = GetHeaders(csvFileCatalogItems, requiredHeaders, optionalheaders ); } catch (Exception ex) { @@ -227,20 +228,90 @@ } string priceString = column[Array.IndexOf(headers, "price")]; - if (!Decimal.TryParse(priceString, NumberStyles.AllowDecimalPoint, CultureInfo.InvariantCulture, out Decimal price)) // TODO: number styles + if (!Decimal.TryParse(priceString, NumberStyles.AllowDecimalPoint, CultureInfo.InvariantCulture, out Decimal price)) { throw new Exception($"price={priceString}is not a valid decimal number"); } - return new CatalogItem() + var catalogItem = new CatalogItem() { CatalogTypeId = catalogTypeIdLookup[catalogTypeName], CatalogBrandId = catalogBrandIdLookup[catalogBrandName], Description = column[Array.IndexOf(headers, "description")], Name = column[Array.IndexOf(headers, "name")], Price = price, - PictureUri = column[Array.IndexOf(headers, "pictureuri")] + PictureUri = column[Array.IndexOf(headers, "pictureuri")], }; + + int availableStockIndex = Array.IndexOf(headers, "availablestock"); + if (availableStockIndex != -1) + { + string availableStockString = column[availableStockIndex]; + if (!String.IsNullOrEmpty(availableStockString)) + { + if ( int.TryParse(availableStockString, out int availableStock)) + { + catalogItem.AvailableStock = availableStock; + } + else + { + throw new Exception($"availableStock={availableStockString} is not a valid integer"); + } + } + } + + int restockThresholdIndex = Array.IndexOf(headers, "restockthreshold"); + if (restockThresholdIndex != -1) + { + string restockThresholdString = column[restockThresholdIndex]; + if (!String.IsNullOrEmpty(restockThresholdString)) + { + if (int.TryParse(restockThresholdString, out int restockThreshold)) + { + catalogItem.RestockThreshold = restockThreshold; + } + else + { + throw new Exception($"restockThreshold={restockThreshold} is not a valid integer"); + } + } + } + + int maxStockThresholdIndex = Array.IndexOf(headers, "maxstockthreshold"); + if (maxStockThresholdIndex != -1) + { + string maxStockThresholdString = column[maxStockThresholdIndex]; + if (!String.IsNullOrEmpty(maxStockThresholdString)) + { + if (int.TryParse(maxStockThresholdString, out int maxStockThreshold)) + { + catalogItem.MaxStockThreshold = maxStockThreshold; + } + else + { + throw new Exception($"maxStockThreshold={maxStockThreshold} is not a valid integer"); + } + } + } + + int onReorderIndex = Array.IndexOf(headers, "onreorder"); + if (onReorderIndex != -1) + { + string onReorderString = column[onReorderIndex]; + if (!String.IsNullOrEmpty(onReorderString)) + { + if (bool.TryParse(onReorderString, out bool onReorder)) + { + catalogItem.OnReorder = onReorder; + } + else + { + throw new Exception($"onReorder={onReorderString} is not a valid boolean"); + } + } + } + + return catalogItem; } static IEnumerable GetPreconfiguredItems() @@ -262,13 +333,21 @@ }; } - static string[] GetHeaders(string[] requiredHeaders, string csvfile) + static string[] GetHeaders(string csvfile, string[] requiredHeaders, string[] optionalHeaders = null) { string[] csvheaders = File.ReadLines(csvfile).First().ToLowerInvariant().Split(','); - if (csvheaders.Count() != requiredHeaders.Count()) + if (csvheaders.Count() < requiredHeaders.Count()) + { + throw new Exception($"requiredHeader count '{ requiredHeaders.Count()}' is bigger then csv header count '{csvheaders.Count()}' "); + } + + if (optionalHeaders != null) { - throw new Exception($"requiredHeader count '{ requiredHeaders.Count()}' is different then read header '{csvheaders.Count()}'"); + if (csvheaders.Count() > (requiredHeaders.Count() + optionalHeaders.Count())) + { + throw new Exception($"csv header count '{csvheaders.Count()}' is larger then required '{requiredHeaders.Count()}' and optional '{optionalHeaders.Count()}' headers count"); + } } foreach (var requiredHeader in requiredHeaders) diff --git a/src/Services/Catalog/Catalog.API/Setup/CatalogItems.csv b/src/Services/Catalog/Catalog.API/Setup/CatalogItems.csv index 28f5ef948..7f9d8e0bf 100644 --- a/src/Services/Catalog/Catalog.API/Setup/CatalogItems.csv +++ b/src/Services/Catalog/Catalog.API/Setup/CatalogItems.csv @@ -1,13 +1,13 @@ -CatalogTypeName,CatalogBrandName,Description,Name,Price,PictureUri -T-Shirt,.NET,".NET Bot Black Hoodie, and more",.NET Bot Black Hoodie,19.5,1.png -Mug,.NET,.NET Black & White Mug,.NET Black & White Mug,8.50,2.png -T-Shirt,Other,Prism White T-Shirt,Prism White T-Shirt,12,3.png -T-Shirt,.NET,.NET Foundation T-shirt,.NET Foundation T-shirt,12,4.png -Sheet,Other,Roslyn Red Sheet,Roslyn Red Sheet,8.5,5.png -T-Shirt,.NET,.NET Blue Hoodie,.NET Blue Hoodie,12,6.png -T-Shirt,Other,Roslyn Red T-Shirt,Roslyn Red T-Shirt",12,7.png -T-Shirt,Other,Kudu Purple Hoodie,Kudu Purple Hoodie,8.5,8.png -Mug,Other,Cup White Mug,Cup White Mug,12,9.png -Sheet,.NET,.NET Foundation Sheet,.NET Foundation Sheet,12,10.png -Sheet,.NET,Cup Sheet,Cup Sheet,8.5,11.png -T-Shirt,Other,Prism White TShirt,Prism White TShirt,12,12.png \ No newline at end of file +CatalogTypeName,CatalogBrandName,Description,Name,Price,PictureUri,availablestock,onreorder +T-Shirt,.NET,".NET Bot Black Hoodie, and more",.NET Bot Black Hoodie,19.5,1.png,100,false +Mug,.NET,.NET Black & White Mug,.NET Black & White Mug,8.50,2.png,89,true +T-Shirt,Other,Prism White T-Shirt,Prism White T-Shirt,12,3.png,56,false +T-Shirt,.NET,.NET Foundation T-shirt,.NET Foundation T-shirt,12,4.png,120,false +Sheet,Other,Roslyn Red Sheet,Roslyn Red Sheet,8.5,5.png,55,false +T-Shirt,.NET,.NET Blue Hoodie,.NET Blue Hoodie,12,6.png,17,false +T-Shirt,Other,Roslyn Red T-Shirt,Roslyn Red T-Shirt",12,7.png,8,false +T-Shirt,Other,Kudu Purple Hoodie,Kudu Purple Hoodie,8.5,8.png,34,false +Mug,Other,Cup White Mug,Cup White Mug,12,9.png,76,false +Sheet,.NET,.NET Foundation Sheet,.NET Foundation Sheet,12,10.png,11,false +Sheet,.NET,Cup Sheet,Cup Sheet,8.5,11.png,3,false +T-Shirt,Other,Prism White TShirt,Prism White TShirt,12,12.png,0,false \ No newline at end of file