Ejemplo n.º 1
0
        public async Task <ExchangeRates> Rate(string bank, DateTime date, string exchangeFrom)
        {
            var opt = mem.SqlOrMemory("DBRead");

            using (var cnt = new InfoValutarContext(opt.Options))
            {
                var data = cnt.RateQ(bank).Where(it =>
                                                 EF.Functions.DateDiffDay(it.Date, date) == 0 &&
                                                 it.ExchangeFrom == exchangeFrom)
                           .Select(it => new ExchangeRates()
                {
                    Bank          = bank,
                    Date          = it.Date,
                    ExchangeFrom  = it.ExchangeFrom,
                    ExchangeTo    = it.ExchangeTo,
                    ExchangeValue = it.ExchangeValue
                });
                return(await data.FirstOrDefaultAsync());
            }
        }
Ejemplo n.º 2
0
        public async Task <int> Save(params ExchangeRates[] er)
        {
            var data = er.Select(it => it.Bank?.ToLower()).Distinct();

            var opt = mem.SqlOrMemory("DBWrite");

            using (var cnt = new InfoValutarContext(opt.Options))
            {
                foreach (var item in data)
                {
                    var dataToSave = er
                                     .Where(it => it.Bank?.ToLower() == item);


                    switch (item)
                    {
                    case "ecb":
                        cnt.Ecb.AddRange(
                            dataToSave.Select(it =>
                                              new Ecb()
                        {
                            Date          = it.Date,
                            ExchangeFrom  = it.ExchangeFrom,
                            ExchangeTo    = it.ExchangeTo,
                            ExchangeValue = it.ExchangeValue
                        }).ToArray());
                        break;

                    case "bnr":
                        cnt.Nbr.AddRange(
                            dataToSave.Select(it =>
                                              new Nbr()
                        {
                            Date          = it.Date,
                            ExchangeFrom  = it.ExchangeFrom,
                            ExchangeTo    = it.ExchangeTo,
                            ExchangeValue = it.ExchangeValue
                        }).ToArray());
                        break;

                    default:
                        throw new ArgumentException($"cannot save bank {item}");
                    }
                }
                var nr = await cnt.SaveChangesAsync();

                return(nr);
            }
        }