Exemplo n.º 1
0
        // GET: Stocks/Stocks
        // Returns "Stocks" partial view
        public ActionResult Stocks()
        {
            // Catch connection errors, redirect to another view if something went wrong
            var ownedStocks     = GetUserOwnedStocks();
            var siteOwnedStocks = GetSiteOwnedStocks();
            var stockPrices     = new StocksJsonData();

            try
            {
                stockPrices = GetStockPrices();
            }
            catch (WebException exception)
            {
                return(View("ConnectionError", new ResponseFormat()
                {
                    Message = exception.ToString()
                }));
            }

            UpdateGraphData(stockPrices);

            var model = new StocksIndexViewModel
            {
                StockPrices     = stockPrices.Items,
                PublicationDate = TimeZoneInfo.ConvertTime(stockPrices.PublicationDate, TimeZoneInfo.Local),
                AccountBalance  = GetUserAccountBalance(),
                OwnedStocks     = ownedStocks,
                StocksTableData = GenerateTableData(stockPrices.Items, ownedStocks, siteOwnedStocks),
                SiteOwnedStocks = siteOwnedStocks
            };

            return(PartialView("Stocks", model));
        }
Exemplo n.º 2
0
        public void UpdateGraphData(StocksJsonData stockPrices)
        {
            var publicationDate = TimeZoneInfo.ConvertTime(stockPrices.PublicationDate, TimeZoneInfo.Local); // Timezone fix


            // Check if the List was created, do so if not.
            if (HttpContext.Session[StockExResr.Graph_Times_Var] == null)
            {
                HttpContext.Session[StockExResr.Graph_Times_Var] = new List <string>();
            }

            // Add the time of the reading
            var times = (List <string>)HttpContext.Session[StockExResr.Graph_Times_Var];

            if (times.Count != 0)
            {
                // Return the function if the last added time is equal to the new one, no point updating.
                if (times[times.Count - 1] == publicationDate.ToString("HH:mm:ss"))
                {
                    return;
                }
            }

            // Limit the readings to 20
            if (times.Count > 20)
            {
                times.RemoveAt(0);
            }
            times.Insert((times.Count), publicationDate.ToString("HH:mm:ss")); // Add new date at the end
            HttpContext.Session[StockExResr.Graph_Times_Var] = times;          // Save

            // Loop through companies
            for (var i = 0; i < stockPrices.Items.Count; i++)
            {
                // Check if the List was created, do so if not.
                if (HttpContext.Session[StockExResr.Graph_Prefix + i] == null)
                {
                    HttpContext.Session[StockExResr.Graph_Prefix + i] = new List <decimal>();
                }

                // Add prices for each company
                var prices = (List <decimal>)HttpContext.Session[StockExResr.Graph_Prefix + i];
                if (prices.Count > 20)
                {
                    prices.RemoveAt(0);
                }

                prices.Insert((prices.Count), (decimal)Math.Round((double)stockPrices.Items[i].GetType().GetProperty("Price").GetValue(stockPrices.Items[i]), 2));
                HttpContext.Session[StockExResr.Graph_Prefix + i] = prices;
            }
            ;
        }