public void AddOrUpdate() { Dictionary <string, double> pricesDictionary = ApiLayerService.LoadCurrencies(); using (GmiUnitOfWork unitOfWork = new GmiUnitOfWork()) { var allCurrenciesInDB = unitOfWork.GenericRepository.GetAll <FiatCurrency>(); List <FiatCurrency> newFiatCurrencies = new List <FiatCurrency>(); foreach (var key in pricesDictionary.Keys) { var fiatCurrencyInDB = allCurrenciesInDB.FirstOrDefault(c => c.Code == key); //not null means already exist, we will update only if (fiatCurrencyInDB != null) { fiatCurrencyInDB.UpdateExchangeRate(pricesDictionary[key]); } else { newFiatCurrencies.Add(new FiatCurrency() { Code = key, ExchangeRateUSD = pricesDictionary[key] }); } } unitOfWork.GenericRepository.Create(newFiatCurrencies); unitOfWork.Commit(); } }
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(); } }
public void UpdateUsdExchangeRate(int id, double exchangeRate) { using (GmiUnitOfWork gmiUnitOfWork = new GmiUnitOfWork()) { Coin coin = gmiUnitOfWork.GenericRepository.GetByID <Coin>(id); Guard.AgainstNull(coin); coin.ExchangeRateUsd = exchangeRate; gmiUnitOfWork.Commit(); } }
public void UpdateUsdExchangeRates() { using (GmiUnitOfWork gmiUnitOfWork = new GmiUnitOfWork()) { //Get Coins from DB, int allCoinsInDBCount = gmiUnitOfWork.GenericRepository.Count(new SearchCriteria <Coin>(int.MaxValue, 1) { FilterExpression = c => c.Difficulty > 0 && c.BlockReward > 0 && c.LastBlock > 0 }); int pageNumber = 1; int pageSize = 30; int lastPage = (int)Math.Ceiling((double)allCoinsInDBCount / pageSize); string usdCode = "USD"; while (pageNumber <= lastPage) { var searchCriteria = new SearchCriteria <Coin>(pageSize, pageNumber) { FilterExpression = c => c.Difficulty > 0 && c.BlockReward > 0 && c.LastBlock > 0 }; var partialResult = gmiUnitOfWork.GenericRepository.Search(searchCriteria).Result; var coinNames = partialResult.Select(s => s.Tag.ToUpper()).ToList(); List <CryptoComparePriceResult> exchangeRatesPartial = CryptoCompareService.GetCryptoComparePriceExchangeRateMultiSource(coinNames, usdCode); foreach (var cryptoComparePriceResult in exchangeRatesPartial) { var coin = partialResult.FirstOrDefault(c => c.Tag.ToUpper() == cryptoComparePriceResult.SourceCurrenceCode.ToUpper()); if (coin == null) { continue; } coin.ExchangeRateUsd = cryptoComparePriceResult.ExchangeRates[usdCode]; } pageNumber++; } gmiUnitOfWork.Commit(); } }