public StockDataServiceTests()
        {
            _stockDataRepository = new Mock <IStockDataRepository>();

            var options = new Mock <IOptionsMonitor <CacheOptions> >();

            options.Setup(o => o.Get(SingleQuoteCache.CacheName)).Returns(new CacheOptions
            {
                CacheExpiryTimeMinutes = 10,
                CacheItemSize          = 1,
                CacheSize = 100
            });

            _cache   = new SingleQuoteCache(options.Object);
            _service = new StockDataService(_stockDataRepository.Object,
                                            _cache,
                                            new QuerySubscriptions(),
                                            new Mock <ILogger <StockDataService> >().Object);

            _singleQuoteData = new SingleQuoteData(
                symbolId: 1,
                ticker: "MSFT",
                high: 1,
                low: 2,
                open: 3,
                previousClose: 4,
                volume: 5,
                change: 6,
                price: 7,
                changePercent: 0.9m,
                lastUpdated: DateTime.UtcNow,
                lastTradingDay: DateTime.UtcNow
                );
        }
Beispiel #2
0
        public ActionResult GetAllStockActivities()
        {
            var stockDataService       = new StockDataService();
            var stockHistoryActivities = stockDataService.GetAllStockHistory();
            var stockHistoryViewModels = new List <StockHistoryViewModel>();

            foreach (var stockHistory in stockHistoryActivities)
            {
                stockHistoryViewModels.Add(new StockHistoryViewModel
                {
                    ProductCode  = stockDataService.GetProductCodeByStockId(stockHistory.StockId),
                    Change       = stockHistory.Change,
                    ChangeAmount = stockHistory.ChangeAmount,
                    ChangeTime   = stockHistory.ChangeTime
                });
            }


            var stockHistoryOrderedByProductCode = stockHistoryViewModels.OrderBy(x => x.ProductCode).ToList();

            return(new JsonResult()
            {
                Data = stockHistoryOrderedByProductCode,
                ContentType = "application/json",
                JsonRequestBehavior = JsonRequestBehavior.AllowGet,
                MaxJsonLength = Int32.MaxValue
            });
        }
Beispiel #3
0
        public ActionResult GetStock(string productCode)
        {
            var orkaDataService = new StockDataService();
            var stocks          = orkaDataService.GetStockByCodeWithWildCardFromOrka(productCode).Select(x => new StockViewModel {
                Code = x.Code, Price = x.Price, StockAmount = x.StockAmount
            }).ToList();

            var productIds = new ProductDataService().GetProductByCodeWithWildCard(productCode).Select(x => x.Id).ToList();

            foreach (var productId in productIds)
            {
                var code           = new ProductDataService().GetProductById(productId).Code;
                var reservedAmount = new SalesDataService().GetReservedCountyProductId(productId);
                var stock          = stocks.FirstOrDefault(x => x.Code == code);
                if (stock != null)
                {
                    stock.ReservedAmount = reservedAmount;
                    stock.StockAmount   -= reservedAmount;
                }
            }


            return(new JsonResult()
            {
                Data = stocks,
                ContentType = "application/json",
                JsonRequestBehavior = JsonRequestBehavior.AllowGet
            });
        }
Beispiel #4
0
        public void UpdateStockHistory()
        {
            var stockDataService        = new StockDataService();
            var existingProductsInStock = stockDataService.GetAllProductsInStock();

            var allStockInformation     = GetAllStocks();
            var previousStocks          = stockDataService.GetAllStocks();
            var stocksWillBeAdded       = new List <IStock>();
            var stockHistoryWillBeAdded = new List <StockHistory>();
            var stockWillBeUpdated      = new List <IStock>();

            foreach (var existingStockInformation in allStockInformation)
            {
                //stock does not exist, it should be added to stock
                if (existingProductsInStock.ContainsKey(existingStockInformation.Code) == false)
                {
                    stocksWillBeAdded.Add(new Stock
                    {
                        Code           = existingStockInformation.Code,
                        StockAmount    = existingStockInformation.StockAmount,
                        ReservedAmount = existingStockInformation.ReservedAmount
                    });
                }
                else
                {
                    //Compare previous and existing stock amounts
                    var time = DateTime.Now;
                    //if there is a change on stock amount
                    var previousStock = previousStocks.First(x => x.Code == existingStockInformation.Code);
                    if (previousStock != null && previousStock.StockAmount !=
                        existingStockInformation.StockAmount)
                    {
                        //insert to updated stock list
                        stockWillBeUpdated.Add(new Stock
                        {
                            Id          = existingProductsInStock[previousStock.Code.ToString()],
                            StockAmount = previousStock.StockAmount
                        });
                        //insert to history list;
                        var stockHistory = new StockHistory
                        {
                            StockId      = existingProductsInStock[previousStock.Code.ToString()],
                            ChangeAmount = Math.Abs(
                                existingStockInformation.StockAmount - previousStock.StockAmount),
                            Change = existingStockInformation.StockAmount > previousStock.StockAmount
                                ? StockChangeState.StokIncreased
                                : StockChangeState.StokDecreased,
                            ChangeTime = time
                        };
                        stockHistoryWillBeAdded.Add(stockHistory);
                    }
                }
            }

            stockDataService.InsertStockHistory(stockHistoryWillBeAdded);
            stockDataService.InsertStock(stocksWillBeAdded);
            stockDataService.UpdateStock(stockWillBeUpdated);
        }
Beispiel #5
0
        public int GetStockInformationByProductCode(string productCode)
        {
            var stock         = new StockDataService().GetStockByCodeFromOrka(productCode);
            var productId     = new ProductDataService().GetProductByCode(productCode).Id;
            var reservedStock = new SalesDataService().GetReservedCountyProductId(productId);

            var usableStock = stock.StockAmount - reservedStock;

            return(usableStock);
        }
Beispiel #6
0
        public IList <StockViewModel> GetAllStocks()
        {
            var orkaDataService = new StockDataService();
            var allStocks       = orkaDataService.GetAllStocksFromOrka().Select(x => new StockViewModel {
                Code = x.Code, Price = x.Price, StockAmount = x.StockAmount
            }).ToList();
            var reservedProductIds = new SalesDataService().GetAllProductsForActiveOrders();

            foreach (var reservedProductId in reservedProductIds.Distinct())
            {
                var productCode    = new ProductDataService().GetProductById(reservedProductId).Code;
                var reservedAmount = new SalesDataService().GetReservedCountyProductId(reservedProductId);
                var stock          = allStocks.FirstOrDefault(x => x.Code == productCode);
                if (stock != null)
                {
                    stock.ReservedAmount = reservedAmount;
                    stock.StockAmount   -= reservedAmount;
                }
            }

            return(allStocks);
        }
 public StockDataServiceTest()
 {
     service = new StockDataService();
 }
        public int GetStockInformationByProductCode(string productCode)
        {
            var stock = new StockDataService().GetStockByCodeFromOrka(productCode);

            return(stock.StockAmount);
        }