Beispiel #1
0
        public void FillPrice(object state)
        {
            var client   = new HttpClient();
            var response = client.GetAsync(BTCPriceUrl).Result;

            if (response.StatusCode == HttpStatusCode.OK)
            {
                var content   = response.Content.ReadAsStringAsync();
                var jsonData  = JObject.Parse(content.Result);
                var buyPrice  = jsonData.GetValue("ask").Value <decimal>();
                var sellPrice = jsonData.GetValue("bid").Value <decimal>();

                var price = new CurrencyPrice()
                {
                    Name         = CurrencyName,
                    BuyPrice     = buyPrice,
                    BuyPriceDiff = _latestPrice != null?GetDiff(_latestPrice.BuyPrice, buyPrice) : 0,
                                       SellPrice     = sellPrice,
                                       SellPriceDiff = _latestPrice != null?GetDiff(_latestPrice.SellPrice, sellPrice) : 0,
                                                           Timestamp = DateTime.UtcNow
                };

                _messageBus.Publish(price);
                Task.Factory.StartNew(() => BroadcastPriceAsync(price));
            }
            else
            {
                _logger.LogError(3, $"CoinTree response status: {response.StatusCode}; message: {response.Content.ReadAsStringAsync().Result}");
            }
        }
        public async Task <IActionResult> PutCurrencyPrice([FromRoute] int id, [FromBody] CurrencyPrice currencyPrice)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            if (id != currencyPrice.currencyPriceID)
            {
                return(BadRequest());
            }

            _context.Entry(currencyPrice).State = EntityState.Modified;

            try
            {
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!CurrencyPriceExists(id))
                {
                    return(NotFound());
                }
                else
                {
                    throw;
                }
            }

            return(NoContent());
        }
        public async Task UpdateCurrency(string code, DateTime date)
        {
            var nbpResponse = await _nbpService.GetCurrency(code, date);

            if (nbpResponse == null)
            {
                return;
            }

            foreach (var currencyRate in nbpResponse.Rates)
            {
                var currencyId = _currenciesRepository.Query().Where(c => c.Code == code).Select(c => c.Id).Single();

                if (_currencyPricesRepository.Query().Any(cp => cp.CurrencyId == currencyId && cp.Day == currencyRate.EffectiveDate))
                {
                    continue;
                }

                var currencyPrice = new CurrencyPrice
                {
                    Day        = currencyRate.EffectiveDate,
                    Price      = currencyRate.Mid,
                    CurrencyId = currencyId
                };

                _currencyPricesRepository.Add(currencyPrice);
            }

            _contextScope.Commit();
        }
        public async Task <IActionResult> PostCurrencyPrice([FromBody] CurrencyPrice currencyPrice)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            _context.CurrencyPrice.Add(currencyPrice);
            await _context.SaveChangesAsync();

            return(CreatedAtAction("GetCurrencyPrice", new { id = currencyPrice.currencyPriceID }, currencyPrice));
        }
Beispiel #5
0
        /// <summary>
        /// Starts endless watcher in background thread
        /// </summary>
        public void Start(CurrencyPrice latestPrice = null)
        {
            _latestPrice = latestPrice;

            if (latestPrice == null)
            {
                // for very first launch when there is no initial value from DB.
                FillPrice(null);
            }

            if (_timer != null)
            {
                _timer.Dispose();
            }
            _timer = new Timer(FillPrice, null, _updateInterval, _updateInterval);
        }
Beispiel #6
0
        public async Task <IActionResult> OnPostAsync(int?id)
        {
            if (id == null)
            {
                return(NotFound());
            }

            CurrencyPrice = await _context.CurrencyPrice.FindAsync(id);

            if (CurrencyPrice != null)
            {
                _context.CurrencyPrice.Remove(CurrencyPrice);
                await _context.SaveChangesAsync();
            }

            return(RedirectToPage("./Index"));
        }
Beispiel #7
0
        public async Task <IActionResult> OnGetAsync(int?id)
        {
            if (id == null)
            {
                return(NotFound());
            }

            CurrencyPrice = await _context.CurrencyPrice
                            .Include(c => c.currency)
                            .Include(c => c.provider).FirstOrDefaultAsync(m => m.currencyPriceID == id);

            if (CurrencyPrice == null)
            {
                return(NotFound());
            }
            return(Page());
        }
        private async Task <decimal> FetchPrice(Currency from, Currency to)
        {
            decimal       price         = 0;
            CurrencyPrice currencyPrice = FetchCachedPrice(from, to);

            if (currencyPrice != null)
            {
                price = currencyPrice.Price;
            }
            else
            {
                CurrencyPriceProxy currencyPriceProxy = new CurrencyPriceProxy();
                price = await currencyPriceProxy.Convert(from, to);

                UpateCache(from, to, price);
            }
            return(price);
        }
        public async Task <IActionResult> OnGetAsync(int?id)
        {
            if (id == null)
            {
                return(NotFound());
            }

            CurrencyPrice = await _context.CurrencyPrice
                            .Include(c => c.currency)
                            .Include(c => c.provider).FirstOrDefaultAsync(m => m.currencyPriceID == id);

            if (CurrencyPrice == null)
            {
                return(NotFound());
            }
            ViewData["currencyID"] = new SelectList(_context.Currency, "currencyID", "currencyCode");
            ViewData["providerID"] = new SelectList(_context.Provider, "providerID", "apiUrl");
            return(Page());
        }
Beispiel #10
0
 private async Task BroadcastPriceAsync(CurrencyPrice price)
 {
     await _clients.Clients.All.InvokeAsync("updateCurrencyPrice", price);
 }