public Task LoadCurrencyRateInfo() { return Task.Run(() => { var objXml = new XmlDocument(); objXml.Load("http://www.obmennik.by/xml/kurs.xml"); XmlNodeList objNodes = objXml.GetElementsByTagName("bank-id"); List<Bank> objBanks = GetAllBanks(); if(objBanks.Count == 0) { foreach(XmlNode node in objNodes) { var objBank = new Bank { Xmld = Convert.ToInt32(node["idbank"].InnerText) }; AddBank(objBank); } } SaveChanges(); foreach(XmlNode node in objNodes) { Bank objBank = GetBankByXmlId(Convert.ToInt32(node["idbank"].InnerText)); if(objBank == null) { break; } DateTime objDateTime = DateParser(node["date"].InnerText, node["time"].InnerText); //по сути очень кривая проверка, //если в базе уже лежат данные хотя бы одного курса валют за данный день, то прерываем все ExchangeRate objExchange = GetRateByDate(objBank, "USD", objDateTime); if(objExchange != null) { return; } var objUsdRate = new ExchangeRate() { Bank = objBank, CurrencyName = "USD", Buy = Convert.ToDouble(node["usd"]["buy"].InnerText), Sale = Convert.ToDouble(node["usd"]["sell"].InnerText), Date = objDateTime }; var objEurRate = new ExchangeRate() { Bank = objBank, CurrencyName = "EUR", Buy = Convert.ToDouble(node["eur"]["buy"].InnerText), Sale = Convert.ToDouble(node["eur"]["sell"].InnerText), Date = objDateTime }; var objRurRate = new ExchangeRate() { Bank = objBank, CurrencyName = "RUR", Buy = Convert.ToDouble(node["rur"]["buy"].InnerText), Sale = Convert.ToDouble(node["rur"]["sell"].InnerText), Date = objDateTime }; AddExchangeRate(objUsdRate); AddExchangeRate(objEurRate); AddExchangeRate(objRurRate); } }); }
public ExchangeRate GetRateByBank(string strCurrencyName, Bank objBank) { return _ObjBankDataBase.ExchangeRates. Where(r => r.Bank.Id == objBank.Id && r.CurrencyName == strCurrencyName). OrderBy(e => e.Date).FirstOrDefault(); }
public void AddBank(Bank objBank) { _ObjBankDataBase.Banks.Add(objBank); }
public ExchangeRate GetRateByDate(Bank objBank, string strName, DateTime objDateTime) { return _ObjBankDataBase.ExchangeRates.FirstOrDefault(r => r.Date == objDateTime && r.CurrencyName == strName && r.Bank.Id == objBank.Id); }