示例#1
0
文件: Config.cs 项目: w1r2p1/Core-2
        /// <summary>
        /// Appends the configuration file.
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <typeparam name="I"></typeparam>
        /// <param name="data">The data.</param>
        /// <param name="item">The item.</param>
        /// <returns></returns>
        public static T AppendConfigFile <T, I>(T data, I item)
            where T : class
        {
            if (typeof(T) == typeof(CurrencyRatesConfig[]))
            {
                //get input
                IEnumerable <CurrencyRatesConfig> rates = data as CurrencyRatesConfig[];
                CurrencyRatesConfig added = item as CurrencyRatesConfig;

                //add item
                var tolist = rates.ToList();
                tolist.Add(added);
                rates = tolist;

                //save
                string newJson = JsonConvert.SerializeObject(rates);
                File.WriteAllText(ConfigCache.GetConfigPath(ConfigFileType.CurrencyRates), newJson);

                //Return what we currently have
                Cachedconfig.Put(ConfigFileType.CurrencyRates, rates);
                return(CurrencyConfig as T);
            }
            else
            {
                throw new Exception($"Append config does not have an implementation for config of type {data.GetType().FullName}");
            }
        }
 public CurrencyViewerController(ICurrencyRatesQueryService currencyRatesQueryService,
                                 ICurrencyRatesBetweenDatesReceiver currencyRatesBetweenDatesReceiver,
                                 ICurrencyRatesCommandService commandService,
                                 IOptions <CurrencyRatesConfig> config)
 {
     _currencyRatesQueryService         = currencyRatesQueryService;
     _currencyRatesBetweenDatesReceiver = currencyRatesBetweenDatesReceiver;
     _commandService = commandService;
     _config         = config.Value;
 }
        public static IOptions <CurrencyRatesConfig> GetConfig(CurrencyRatesConfig expectedResult = null)
        {
            var options = new Mock <IOptions <CurrencyRatesConfig> >();

            var returnValue = expectedResult ?? new CurrencyRatesConfig()
            {
                BaseUrl = "http://api.nbp.pl", CurrencyCodes = new string[] { "usd", "eur" }
            };

            options.SetupGet(x => x.Value).Returns(returnValue);

            return(options.Object);
        }
示例#4
0
        /// <summary>
        /// Update current config file with new information
        /// </summary>
        /// <param name="date"></param>
        /// <param name="base"></param>
        /// <param name="rate"></param>
        private void UpdateConfig(DateTime date, CurrencyType @base, CurrencyRatesConfig rate)
        {
            //Set proper date
            date = date.Date;

            //Get current config
            var currentconfig = GetFromConfig();

            //Check if we need to add
            var current = currentconfig[@base.ToString()];

            if (!current.ContainsKey(date))
            {
                Config.AppendConfigFile(currentconfig, rate);
            }
        }
示例#5
0
        public void Config_AppendConfig()
        {
            //Arrange
            var currentitems = Config.CurrencyConfig.ToArray();
            var addeditem    = new CurrencyRatesConfig
            {
                Base  = "EUR",
                date  = DateTime.UtcNow.Date.ToString("yyyy-MM-dd"),
                Date  = DateTime.UtcNow.Date,
                Rates = Enum.GetValues(typeof(CurrencyType)).Cast <CurrencyType>().ToDictionary(x => x.ToString(), x => 0.91m)
            };

            //Act
            Config.AppendConfigFile(Config.CurrencyConfig, addeditem);

            //Assert
            Config.CurrencyConfig.Length.Should().BeGreaterThan(currentitems.Length);
        }
示例#6
0
        /// <summary>
        /// Updates the specified date.
        /// </summary>
        /// <param name="date">The date.</param>
        /// <param name="base">The base.</param>
        /// <param name="rate">The rate.</param>
        protected void Update(DateTime date, CurrencyType @base, CurrencyRatesConfig rate)
        {
            //Update locally
            string basecurrency = @base.ToString();

            if (!History.TryGetValue(basecurrency, out Dictionary <DateTime, CurrencyRatesConfig> rates))
            {
                rates[date.Date] = rate;
            }
            else
            {
                History.Add(basecurrency, new Dictionary <DateTime, CurrencyRatesConfig>
                {
                    { date.Date, rate }
                });
            }

            //Update config file
            UpdateConfig(date, @base, rate);
            UpdateRange();
        }
 public CurrencyRatesReceiver(IOptions <CurrencyRatesConfig> options, IHttpClientFactory clientFactory)
 {
     _config        = options.Value;
     _clientFactory = clientFactory;
 }