Example #1
0
        public void UpdateCoinsInfo()
        {
            using (GmiUnitOfWork gmiUnitOfWork = new GmiUnitOfWork())
            {
                //Get Coins from DB,
                List <Coin> allCoinsInDB = gmiUnitOfWork.GenericRepository.GetAll <Coin>();
                //Read coin data from Coins.JSON, NOTE Not all coins are here
                List <Coin> allCoinsInfoFromWTM = WhatToMineService.GetCoinsInfoFromCoinsJson();
                //CoinsDB: Filter out the ones that dont have info from Coins.JSON.
                List <Coin> coinsWithInfoReturned = allCoinsInDB.Where(cdb => allCoinsInfoFromWTM.Any(c => c.WhatToMineId == cdb.WhatToMineId)).ToList();
                //CoinsDB: Filter out the ones that dont have info from Coins.JSON.
                List <Coin> coinsWithoutInfoReturned = allCoinsInDB.Where(cdb => allCoinsInfoFromWTM.All(c => c.WhatToMineId != cdb.WhatToMineId)).ToList();
                //Foreach one that no info returned, get its info
                foreach (var coin in coinsWithoutInfoReturned)
                {
                    Coin coinInfo = WhatToMineService.GetCoinInfo(coin.WhatToMineId);
                    if (coinInfo != null)
                    {
                        coin.UpdateInfo(coinInfo);
                    }
                }
                foreach (var coin in coinsWithInfoReturned)
                {
                    var coinInfo = allCoinsInfoFromWTM.FirstOrDefault(c => c.WhatToMineId == coin.WhatToMineId);
                    if (coinInfo == null)
                    {
                        throw new Exception("Someting is wrong, this coin should belong to the unknown list");
                    }
                    coin.UpdateInfo(coinInfo);
                }

                gmiUnitOfWork.Commit();
            }
        }