private void RefreshCurrencies(DateTime refreshDate) { refreshDate = refreshDate.Date; List <NBPCurrencyRate> nbpCurrencies = LoadCurrenciesFromNBP(refreshDate, true, true); CurrencyRate[] existingRates = this.repo.Currencies.GetAllRatesForDay(refreshDate).ToArray(); IEnumerable <Currency> systemCurrencies = this.repo.Dictionaries.GetCurrencies(); //List<Currency> test = systemCurrencies.ToList(); List <CurrencyRate> ratesToBeAdded = new List <CurrencyRate>(); foreach (Currency curr in systemCurrencies) { NBPCurrencyRate nbp = nbpCurrencies.FirstOrDefault(c => string.Equals(c.CurrencyCode, curr.Code, StringComparison.InvariantCultureIgnoreCase)); if (nbp != null) { if (!existingRates.Any(cr => cr.Currency.Code == nbp.CurrencyCode)) { CurrencyRate rate = new CurrencyRate() { Currency = curr, DateRefreshed = refreshDate, ExchangeRate = nbp.CurrencyRate / nbp.Converter }; ratesToBeAdded.Add(rate); } //curr.ExchangeRate = nbp.CurrencyRate; //curr.DateRefreshed = nbp.CurrencyDate; } } //List<NBPCurrencyRate> missing = nbpCurrencies.Where(nbpc => !systemCurrencies.Any(sc => // string.Equals(nbpc.CurrencyCode, sc.Code, StringComparison.InvariantCultureIgnoreCase))).ToList(); //if (missing.Count > 0) //{ // List<Currency> toBeAdded = missing.Select(nbpc => new Currency() // { // Name = nbpc.CurrencyName, // Code = nbpc.CurrencyCode, // ExchangeRate = nbpc.CurrencyRate, // DateRefreshed = nbpc.CurrencyDate // }).ToList(); // this.repo.Dictionaries.AddCurrencies(toBeAdded); //} this.repo.Currencies.AddCurrencyRates(ratesToBeAdded); this.repo.SaveChanges(); }
private List <NBPCurrencyRate> ParseCurrencies(string rawCurrencies) { XmlDocument xmlDoc = new XmlDocument(); xmlDoc.LoadXml(rawCurrencies); XmlNodeList currencyListXml = xmlDoc.GetElementsByTagName(Constraints.POSITION); var date = xmlDoc.GetElementsByTagName(Constraints.PUBLICATION_DATE); DateTime currencyDate = Convert.ToDateTime(date[0].FirstChild.Value); List <NBPCurrencyRate> currencyList = new List <NBPCurrencyRate>(); foreach (XmlNode currencyXml in currencyListXml) { //XmlNodeList nodes = currencyXml.ChildNodes; NBPCurrencyRate currency = new NBPCurrencyRate(); currency.CurrencyName = currencyXml.ChildNodes[0].FirstChild.Value; currency.Converter = Convert.ToDouble(currencyXml.ChildNodes[1].FirstChild.Value); currency.CurrencyCode = currencyXml.ChildNodes[2].FirstChild.Value; currency.CurrencyRate = Convert.ToDouble(currencyXml.ChildNodes[3].FirstChild.Value, CultureInfo.GetCultureInfo("pl-PL")); currency.CurrencyDate = currencyDate; currencyList.Add(currency); } return(currencyList); }