public JsonResult GetExchangeRate(string baseId, string quoteId) { var assets = _dbContext.Set <Asset>(); var baseAsset = assets.FirstOrDefault(x => x.AssetId == baseId); var quoteAsset = assets.FirstOrDefault(x => x.AssetId == quoteId); if (baseAsset == null) { return(Json(new { Message = "Base asset not found" })); } if (quoteAsset == null) { return(Json(new { Message = "Quote asset not found" })); } ExchangeRate rate = _currencyApi.GetSpecificRate(baseAsset, quoteAsset); return(Json(new { Message = "Success", ExchangeRate = rate })); }
public override void Execute() { List <Asset> assets = _dbContext.Set <Asset>().ToList(); foreach (var baseAsset in assets) { if (baseAsset.AssetId == null) { continue; } var dbRates = _dbContext.Set <ExchangeRate>() .Include(x => x.AssetBase) .Include(x => x.AssetQuote); foreach (var quoteAsset in assets.Where(x => x.AssetId != baseAsset.AssetId)) { var apiRate = _currencyApi.GetSpecificRate(baseAsset, quoteAsset); if (apiRate == null || apiRate.Rate == 0) { return; } var dbRate = dbRates.FirstOrDefault(x => x.AssetBase.AssetId == baseAsset.AssetId && x.AssetQuote.AssetId == quoteAsset.AssetId); if (dbRate == null) { _dbContext.Set <ExchangeRate>().Add( new ExchangeRate { AssetBase = baseAsset, AssetQuote = quoteAsset, Rate = apiRate.Rate, Time = apiRate.Time }); } else { dbRate.Rate = apiRate.Rate; dbRate.Time = apiRate.Time; } } _dbContext.SaveChanges(); } _dbContext.SaveChanges(); }