Пример #1
0
        public CurrencyPrice GetLatestPrice()
        {
            DbPrice dbPrice = null;

            try
            {
                var prices = _dbContext.Prices
                             .OrderByDescending(price => price.Timestamp)
                             .Take(2);
                dbPrice = prices.FirstOrDefault();
                if (dbPrice != null)
                {
                    var previousPrice = prices.LastOrDefault();
                    if (previousPrice != dbPrice)
                    {
                        var resultPrice = _mapper.Map <CurrencyPrice>(dbPrice);
                        // TODO: difference should be stored in DB, this calculations should not be in that class.
                        resultPrice.BuyPriceDiff  = (dbPrice.BuyPrice - previousPrice.BuyPrice) / previousPrice.BuyPrice;
                        resultPrice.SellPriceDiff = (dbPrice.SellPrice - previousPrice.SellPrice) / previousPrice.SellPrice;

                        return(resultPrice);
                    }
                }
            }
            catch (SqliteException ex)
            {
                // in case bd not created yet.
                _logger.LogError(ex, "Can be expeceted cause db is completely emtpy");
            }

            return(_mapper.Map <CurrencyPrice>(dbPrice));
        }
Пример #2
0
        public void DbPriceTest()
        {
            DbPrice price = new DbPrice()
            {
                GroupId = 1, ProductId = 1, Price = 1m
            };
            var result = price.Equals(null);

            Assert.False(result);

            DbPrice differentPrice = new DbPrice()
            {
                GroupId = 1, ProductId = 2, Price = 1m
            };

            result = price.Equals(differentPrice);
            Assert.False(result);

            differentPrice.ProductId = price.ProductId;
            differentPrice.Price     = price.Price - (price.Delta * 2);

            result = price.Equals(differentPrice);
            Assert.False(result);

            differentPrice.Price = price.Price - (price.Delta / 2);

            result = price.Equals(differentPrice);
            Assert.True(result);

            var priceHash     = price.GetHashCode();
            var diffPriceHash = differentPrice.GetHashCode();

            Assert.NotEqual <int>(priceHash, diffPriceHash);
        }