@@ -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 )
0 commit comments