Exemplo n.º 1
0
        public async Task <IActionResult> InitialCreate(Currency currency)
        {
            if (currency == null)
            {
                throw new ArgumentNullException(nameof(currency));
            }
            var currencies = await UtilityCurrencies.TakeData();

            UtilityCurrencies.AddingUah(currencies);
            UtilityCurrencies.Recalculate(currencies);
            foreach (var apiData in currencies)
            {
                currency = apiData;
                _context.Add(currency);
            }
            await _context.SaveChangesAsync();

            return(RedirectToAction(nameof(Index)));
        }
Exemplo n.º 2
0
        public async Task <IActionResult> UpdateCurrencies()
        {
            if (!Enumerable.Any(_context.Currencies, currency => currency != null))
            {
                return(NotFound());
            }
            if (!Enumerable.Any(_context.Products, product => product != null))
            {
                return(NotFound());
            }
            var products = await _context.Products.ToListAsync();

            var currenciesInDatabase = await _context.Currencies.ToListAsync();

            var newData = await UtilityCurrencies.TakeData();

            UtilityCurrencies.AddingUah(newData);
            UtilityCurrencies.Recalculate(newData);
            UtilityProduct.CalculateBasicCurrencies(newData, products);
            foreach (var oldData in currenciesInDatabase)
            {
                foreach (var editedData in newData.Where(editedData => oldData.CC == editedData.CC))
                {
                    oldData.Rate         = editedData.Rate;
                    oldData.ExchangeDate = DateTime.Today.ToString("dd.MM.yyyy");
                    _context.Update(oldData);
                }
            }

            foreach (var product in products)
            {
                _context.Update(product);
            }
            await _context.SaveChangesAsync();

            return(RedirectToAction(nameof(Index)));
        }