示例#1
0
        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;
            }
        }
示例#2
0
        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;
 }