Skip to content

Commit 9229a8d

Browse files
author
Shaun Walker
committed
support for additional CatalogItem properties
1 parent 445101b commit 9229a8d

2 files changed

Lines changed: 101 additions & 22 deletions

File tree

src/Services/Catalog/Catalog.API/Infrastructure/CatalogContextSeed.cs

Lines changed: 88 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ static IEnumerable<CatalogBrand> GetCatalogBrandsFromFile(string contentRootPath
8181
try
8282
{
8383
string[] requiredHeaders = { "catalogbrand" };
84-
csvheaders = GetHeaders(requiredHeaders, csvFileCatalogBrands);
84+
csvheaders = GetHeaders( csvFileCatalogBrands, requiredHeaders );
8585
}
8686
catch (Exception ex)
8787
{
@@ -136,7 +136,7 @@ static IEnumerable<CatalogType> GetCatalogTypesFromFile(string contentRootPath,
136136
try
137137
{
138138
string[] requiredHeaders = { "catalogtype" };
139-
csvheaders = GetHeaders(requiredHeaders, csvFileCatalogTypes);
139+
csvheaders = GetHeaders( csvFileCatalogTypes, requiredHeaders );
140140
}
141141
catch (Exception ex)
142142
{
@@ -188,7 +188,8 @@ static IEnumerable<CatalogItem> GetCatalogItemsFromFile(string contentRootPath,
188188
try
189189
{
190190
string[] requiredHeaders = { "catalogtypename", "catalogbrandname", "description", "name", "price", "pictureuri" };
191-
csvheaders = GetHeaders(requiredHeaders, csvFileCatalogItems);
191+
string[] optionalheaders = { "availablestock", "restockThreshold", "maxStockThreshold", "onreorder" };
192+
csvheaders = GetHeaders(csvFileCatalogItems, requiredHeaders, optionalheaders );
192193
}
193194
catch (Exception ex)
194195
{
@@ -227,20 +228,90 @@ static CatalogItem CreateCatalogItem(string[] column, string[] headers, Dictiona
227228
}
228229

229230
string priceString = column[Array.IndexOf(headers, "price")];
230-
if (!Decimal.TryParse(priceString, NumberStyles.AllowDecimalPoint, CultureInfo.InvariantCulture, out Decimal price)) // TODO: number styles
231+
if (!Decimal.TryParse(priceString, NumberStyles.AllowDecimalPoint, CultureInfo.InvariantCulture, out Decimal price))
231232
{
232233
throw new Exception($"price={priceString}is not a valid decimal number");
233234
}
234235

235-
return new CatalogItem()
236+
var catalogItem = new CatalogItem()
236237
{
237238
CatalogTypeId = catalogTypeIdLookup[catalogTypeName],
238239
CatalogBrandId = catalogBrandIdLookup[catalogBrandName],
239240
Description = column[Array.IndexOf(headers, "description")],
240241
Name = column[Array.IndexOf(headers, "name")],
241242
Price = price,
242-
PictureUri = column[Array.IndexOf(headers, "pictureuri")]
243+
PictureUri = column[Array.IndexOf(headers, "pictureuri")],
243244
};
245+
246+
int availableStockIndex = Array.IndexOf(headers, "availablestock");
247+
if (availableStockIndex != -1)
248+
{
249+
string availableStockString = column[availableStockIndex];
250+
if (!String.IsNullOrEmpty(availableStockString))
251+
{
252+
if ( int.TryParse(availableStockString, out int availableStock))
253+
{
254+
catalogItem.AvailableStock = availableStock;
255+
}
256+
else
257+
{
258+
throw new Exception($"availableStock={availableStockString} is not a valid integer");
259+
}
260+
}
261+
}
262+
263+
int restockThresholdIndex = Array.IndexOf(headers, "restockthreshold");
264+
if (restockThresholdIndex != -1)
265+
{
266+
string restockThresholdString = column[restockThresholdIndex];
267+
if (!String.IsNullOrEmpty(restockThresholdString))
268+
{
269+
if (int.TryParse(restockThresholdString, out int restockThreshold))
270+
{
271+
catalogItem.RestockThreshold = restockThreshold;
272+
}
273+
else
274+
{
275+
throw new Exception($"restockThreshold={restockThreshold} is not a valid integer");
276+
}
277+
}
278+
}
279+
280+
int maxStockThresholdIndex = Array.IndexOf(headers, "maxstockthreshold");
281+
if (maxStockThresholdIndex != -1)
282+
{
283+
string maxStockThresholdString = column[maxStockThresholdIndex];
284+
if (!String.IsNullOrEmpty(maxStockThresholdString))
285+
{
286+
if (int.TryParse(maxStockThresholdString, out int maxStockThreshold))
287+
{
288+
catalogItem.MaxStockThreshold = maxStockThreshold;
289+
}
290+
else
291+
{
292+
throw new Exception($"maxStockThreshold={maxStockThreshold} is not a valid integer");
293+
}
294+
}
295+
}
296+
297+
int onReorderIndex = Array.IndexOf(headers, "onreorder");
298+
if (onReorderIndex != -1)
299+
{
300+
string onReorderString = column[onReorderIndex];
301+
if (!String.IsNullOrEmpty(onReorderString))
302+
{
303+
if (bool.TryParse(onReorderString, out bool onReorder))
304+
{
305+
catalogItem.OnReorder = onReorder;
306+
}
307+
else
308+
{
309+
throw new Exception($"onReorder={onReorderString} is not a valid boolean");
310+
}
311+
}
312+
}
313+
314+
return catalogItem;
244315
}
245316

246317
static IEnumerable<CatalogItem> GetPreconfiguredItems()
@@ -262,13 +333,21 @@ static IEnumerable<CatalogItem> GetPreconfiguredItems()
262333
};
263334
}
264335

265-
static string[] GetHeaders(string[] requiredHeaders, string csvfile)
336+
static string[] GetHeaders(string csvfile, string[] requiredHeaders, string[] optionalHeaders = null)
266337
{
267338
string[] csvheaders = File.ReadLines(csvfile).First().ToLowerInvariant().Split(',');
268339

269-
if (csvheaders.Count() != requiredHeaders.Count())
340+
if (csvheaders.Count() < requiredHeaders.Count())
341+
{
342+
throw new Exception($"requiredHeader count '{ requiredHeaders.Count()}' is bigger then csv header count '{csvheaders.Count()}' ");
343+
}
344+
345+
if (optionalHeaders != null)
270346
{
271-
throw new Exception($"requiredHeader count '{ requiredHeaders.Count()}' is different then read header '{csvheaders.Count()}'");
347+
if (csvheaders.Count() > (requiredHeaders.Count() + optionalHeaders.Count()))
348+
{
349+
throw new Exception($"csv header count '{csvheaders.Count()}' is larger then required '{requiredHeaders.Count()}' and optional '{optionalHeaders.Count()}' headers count");
350+
}
272351
}
273352

274353
foreach (var requiredHeader in requiredHeaders)
Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
1-
CatalogTypeName,CatalogBrandName,Description,Name,Price,PictureUri
2-
T-Shirt,.NET,".NET Bot Black Hoodie, and more",.NET Bot Black Hoodie,19.5,1.png
3-
Mug,.NET,.NET Black & White Mug,.NET Black & White Mug,8.50,2.png
4-
T-Shirt,Other,Prism White T-Shirt,Prism White T-Shirt,12,3.png
5-
T-Shirt,.NET,.NET Foundation T-shirt,.NET Foundation T-shirt,12,4.png
6-
Sheet,Other,Roslyn Red Sheet,Roslyn Red Sheet,8.5,5.png
7-
T-Shirt,.NET,.NET Blue Hoodie,.NET Blue Hoodie,12,6.png
8-
T-Shirt,Other,Roslyn Red T-Shirt,Roslyn Red T-Shirt",12,7.png
9-
T-Shirt,Other,Kudu Purple Hoodie,Kudu Purple Hoodie,8.5,8.png
10-
Mug,Other,Cup<T> White Mug,Cup<T> White Mug,12,9.png
11-
Sheet,.NET,.NET Foundation Sheet,.NET Foundation Sheet,12,10.png
12-
Sheet,.NET,Cup<T> Sheet,Cup<T> Sheet,8.5,11.png
13-
T-Shirt,Other,Prism White TShirt,Prism White TShirt,12,12.png
1+
CatalogTypeName,CatalogBrandName,Description,Name,Price,PictureUri,availablestock,onreorder
2+
T-Shirt,.NET,".NET Bot Black Hoodie, and more",.NET Bot Black Hoodie,19.5,1.png,100,false
3+
Mug,.NET,.NET Black & White Mug,.NET Black & White Mug,8.50,2.png,89,true
4+
T-Shirt,Other,Prism White T-Shirt,Prism White T-Shirt,12,3.png,56,false
5+
T-Shirt,.NET,.NET Foundation T-shirt,.NET Foundation T-shirt,12,4.png,120,false
6+
Sheet,Other,Roslyn Red Sheet,Roslyn Red Sheet,8.5,5.png,55,false
7+
T-Shirt,.NET,.NET Blue Hoodie,.NET Blue Hoodie,12,6.png,17,false
8+
T-Shirt,Other,Roslyn Red T-Shirt,Roslyn Red T-Shirt",12,7.png,8,false
9+
T-Shirt,Other,Kudu Purple Hoodie,Kudu Purple Hoodie,8.5,8.png,34,false
10+
Mug,Other,Cup<T> White Mug,Cup<T> White Mug,12,9.png,76,false
11+
Sheet,.NET,.NET Foundation Sheet,.NET Foundation Sheet,12,10.png,11,false
12+
Sheet,.NET,Cup<T> Sheet,Cup<T> Sheet,8.5,11.png,3,false
13+
T-Shirt,Other,Prism White TShirt,Prism White TShirt,12,12.png,0,false

0 commit comments

Comments
 (0)