@@ -11,6 +11,7 @@ namespace Migration
1111 public class Migrator
1212 {
1313 private readonly string _dataDirectory ;
14+ private readonly ICollection < StockItem > _stock = new List < StockItem > ( ) ;
1415
1516 public Migrator ( string dataDirectory )
1617 {
@@ -20,15 +21,11 @@ public Migrator(string dataDirectory)
2021 public IEnumerable < Product > Migrate ( )
2122 {
2223 string catalogPath = Path . Combine ( _dataDirectory , "socks_catalog.csv" ) ;
23- string stockPath = Path . Combine ( _dataDirectory , "stock_inventory.xlsx" ) ;
24-
2524 if ( ! File . Exists ( catalogPath ) )
2625 throw new FileNotFoundException ( $ "Catalog file not found: { catalogPath } ") ;
27- if ( ! File . Exists ( stockPath ) )
28- throw new FileNotFoundException ( $ "Stock file not found: { stockPath } ") ;
2926
30- var stockData = ReadStockData ( stockPath ) ;
31- var combinedProducts = ReadAndCombineCatalogData ( catalogPath , stockData ) ;
27+ ReadStock ( ) ;
28+ var combinedProducts = ReadAndCombineCatalogData ( catalogPath ) ;
3229
3330 var products = combinedProducts
3431 . Where ( x => x . Active )
@@ -42,7 +39,7 @@ public IEnumerable<Product> Migrate()
4239 new ProductId ( index ) ,
4340 new Name ( prod . Name . Replace ( " Socks" , "" ) ) ,
4441 new Category ( "???" ) , // TODO: we don't have categories in the legacy system
45- new Price ( prod . Price ) ,
42+ new Price ( prod . Price , 0 ) ,
4643 new Stock ( group . Sum ( x => x . Stock ) )
4744 ) ;
4845 } )
@@ -51,29 +48,7 @@ public IEnumerable<Product> Migrate()
5148 return products ;
5249 }
5350
54- private static Dictionary < Guid , int > ReadStockData ( string stockPath )
55- {
56- var stockData = new Dictionary < Guid , int > ( ) ;
57-
58- using var workbook = new XLWorkbook ( stockPath ) ;
59- var worksheet = workbook . Worksheet ( 1 ) ;
60- var rowCount = worksheet . LastRowUsed ( ) ? . RowNumber ( ) ;
61-
62- for ( int row = 2 ; row <= rowCount ; row ++ )
63- {
64- var productIdCell = worksheet . Cell ( row , 1 ) . GetValue < string > ( ) ;
65- var stockQuantityCell = worksheet . Cell ( row , 2 ) . GetValue < int > ( ) ;
66-
67- if ( Guid . TryParse ( productIdCell , out var productId ) )
68- {
69- stockData [ productId ] = stockQuantityCell ;
70- }
71- }
72-
73- return stockData ;
74- }
75-
76- private static List < LegacyCombinedProduct > ReadAndCombineCatalogData ( string catalogPath , Dictionary < Guid , int > stockData )
51+ private List < LegacyCombinedProduct > ReadAndCombineCatalogData ( string catalogPath )
7752 {
7853 var products = new List < LegacyCombinedProduct > ( ) ;
7954
@@ -96,7 +71,7 @@ private static List<LegacyCombinedProduct> ReadAndCombineCatalogData(string cata
9671 Material = columns [ 8 ] ,
9772 Description = columns [ 9 ] . Trim ( '"' ) ,
9873 Brand = columns [ 10 ] ,
99- Stock = stockData . GetValueOrDefault ( productId , 0 )
74+ Stock = GetStock ( productId ) ,
10075 } ;
10176
10277 products . Add ( product ) ;
@@ -105,6 +80,63 @@ private static List<LegacyCombinedProduct> ReadAndCombineCatalogData(string cata
10580 return products ;
10681 }
10782
83+ private record StockItem ( Guid ProductId , int Stock ) ;
84+
85+ private void ReadStock ( )
86+ {
87+ string stockPath = Path . Combine ( _dataDirectory , "stock_inventory.xlsx" ) ;
88+
89+ if ( ! File . Exists ( stockPath ) )
90+ throw new FileNotFoundException ( $ "Stock file not found: { stockPath } ") ;
91+
92+ using var workbook = new XLWorkbook ( stockPath ) ;
93+ var worksheet = workbook . Worksheet ( 1 ) ;
94+ var rowCount = worksheet . LastRowUsed ( ) ? . RowNumber ( ) ;
95+
96+ for ( int row = 2 ; row <= rowCount ; row ++ )
97+ {
98+ var productIdCell = worksheet . Cell ( row , 1 ) . GetValue < string > ( ) ;
99+ var stockQuantity = worksheet . Cell ( row , 2 ) . GetValue < int > ( ) ;
100+
101+ if ( Guid . TryParse ( productIdCell , out var productId ) )
102+ {
103+ _stock . Add ( new StockItem ( productId , stockQuantity ) ) ;
104+ }
105+ }
106+ }
107+
108+ private int GetStock ( Guid productIdToFind )
109+ {
110+ return _stock . FirstOrDefault ( x => x . ProductId == productIdToFind ) ? . Stock ?? 0 ;
111+
112+ // ATTN: This code would read the Excel every time we need a Stock
113+ // But that just took way too long for a single test to run.
114+ //string stockPath = Path.Combine(_dataDirectory, "stock_inventory.xlsx");
115+
116+ //if (!File.Exists(stockPath))
117+ // throw new FileNotFoundException($"Stock file not found: {stockPath}");
118+
119+ //using var workbook = new XLWorkbook(stockPath);
120+ //var worksheet = workbook.Worksheet(1);
121+ //var rowCount = worksheet.LastRowUsed()?.RowNumber();
122+
123+ //for (int row = 2; row <= rowCount; row++)
124+ //{
125+ // var productIdCell = worksheet.Cell(row, 1).GetValue<string>();
126+ // var stockQuantityCell = worksheet.Cell(row, 2).GetValue<int>();
127+
128+ // if (Guid.TryParse(productIdCell, out var productId))
129+ // {
130+ // if (productId == productIdToFind)
131+ // {
132+ // return stockQuantityCell;
133+ // }
134+ // }
135+ //}
136+
137+ //return 0;
138+ }
139+
108140 private static string [ ] ParseCsvLine ( string line )
109141 {
110142 var result = new List < string > ( ) ;
0 commit comments