public void FetchAndAddOrUpdateCoins() { //get all coins from external source var allcoinsFromResponse = WhatToMineService.GetAllCoinsFromCalculators(); using (GmiUnitOfWork unitOfWork = new GmiUnitOfWork()) { var coinsFromDB = Search(new SearchCriteria <Coin>(int.MaxValue, 1)).Result; var newCoinsToInsert = allcoinsFromResponse .Where(c => coinsFromDB.FirstOrDefault(dbc => dbc.Name.ToLower() == c.Name.ToLower()) == null).ToList(); //update existing foreach (var coin in coinsFromDB) { var newData = allcoinsFromResponse.FirstOrDefault(dbc => dbc.Name.ToLower() == coin.Name.ToLower()); if (newData == null) { continue; } coin.UpdateInfo(newData); } // add new ones unitOfWork.GenericRepository.Create(newCoinsToInsert); unitOfWork.Commit(); } }
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(); } }