public async Task <bool> WriteExchangeRates(ExchangeRate rate) { try { using (var db = new ExchangeRateDBContext()) { foreach (var item in rate.Rates) { if (item.IsValid()) { var currency = await GetOrAddCountry(item.Key, db); db.Rates.Add(new Rates { Currency = currency, Rate = item.Value, RateDate = DateTime.Parse(rate.Date) }); } } await db.SaveChangesAsync(); return(true); } } catch (Exception e) { throw e; } }
private async Task <Currency> GetOrAddCountry(string code, ExchangeRateDBContext db) { var country = await db.Currency.FirstOrDefaultAsync(x => x.Code == code); if (country.IsNotNull()) { return(country); } return(new Currency { Code = code, Name = "Unknown currency" }); }
public ExchangeRateController(ExchangeRateDBContext context) { _context = context; }