Exemplo n.º 1
0
        public async Task <IQueryable <DtoRate> > GetRatesAsync()
        {
            var res = await SoapHelper.CallWebService(
                CBR_DAILY_SERIVCE_URL,
                NAMESPACE,
                VERB,
                "GetCursOnDate",
                new Dictionary <string, string>
            {
                { "On_date", DateTime.Now.ToString("yyyy-MM-dd") },
            });

            var doc = new XmlDocument();

            doc.LoadXml(res);

            var datas = doc.SelectNodes("//ValuteCursOnDate");

            _context.Rates.RemoveRange(_context.Rates);
            _context.SaveChanges();

            foreach (XmlElement data in datas)
            {
                try
                {
                    _context.Rates.Add(new CbrRate
                    {
                        Code  = data.SelectSingleNode("VchCode").InnerText.Trim(),
                        Title = data.SelectSingleNode("Vname").InnerText.Trim(),
                        Count = short.Parse(data.SelectSingleNode("Vnom").InnerText),
                        Rate  = double.Parse(data.SelectSingleNode("Vcurs").InnerText, CultureInfo.InvariantCulture),
                    });
                }
                catch (Exception ex)
                {
                    Debug.WriteLine(ex.Message);
                }
            }
            _context.SaveChanges();

            var query = _context.Rates.Select(i => new DtoRate
            {
                Code  = i.Code,
                Title = i.Title,
                Count = i.Count,
                Rate  = i.Rate
            });

            return(query);
            //return query.ToList().AsQueryable();
        }
        public override async Task <ResponseInfo> GetRates(RequestInfo request, ServerCallContext context)
        {
            var response = new ResponseInfo();

            var res = await SoapHelper.CallWebService(
                CBR_DAILY_SERIVCE_URL,
                NAMESPACE,
                VERB,
                "GetCursOnDate",
                new Dictionary <string, string>
            {
                { "On_date", DateTime.Now.ToString("yyyy-MM-dd") },
            });

            var doc = new XmlDocument();

            doc.LoadXml(res);

            var datas = doc.SelectNodes("//ValuteCursOnDate");

            _context.Rates.RemoveRange(_context.Rates);
            _context.SaveChanges();

            foreach (XmlElement data in datas)
            {
                try
                {
                    _context.Rates.Add(new CbrRate
                    {
                        Code  = data.SelectSingleNode("VchCode").InnerText.Trim(),
                        Title = data.SelectSingleNode("Vname").InnerText.Trim(),
                        Count = short.Parse(data.SelectSingleNode("Vnom").InnerText),
                        Rate  = double.Parse(data.SelectSingleNode("Vcurs").InnerText, CultureInfo.InvariantCulture),
                    });
                }
                catch (Exception ex)
                {
                    Debug.WriteLine(ex.Message);
                }
            }
            _context.SaveChanges();

            var rates = await GetDataFromContextByODataFilterAsync <DataContext, IEnumerable <DtoRate> >(request.Query);

            foreach (var rate in rates)
            {
                var eRate = new ExchangeRate
                {
                    Code  = new StringValue(),
                    Title = new StringValue(),
                    Count = new Int32Value(),
                    Rate  = new DoubleValue()
                };

                if (!string.IsNullOrEmpty(rate.Code))
                {
                    eRate.Code.Value = rate.Code;
                }

                if (!string.IsNullOrEmpty(rate.Title))
                {
                    eRate.Title.Value = rate.Title;
                }

                if (rate.Count.HasValue)
                {
                    eRate.Count.Value = rate.Count.Value;
                }

                if (rate.Rate.HasValue)
                {
                    eRate.Rate.Value = rate.Rate.Value;
                }

                response.Rates.Add(eRate);
            }

            return(response);
        }