Exemplo n.º 1
0
        public async Task <IEnumerable <PortfolioListItem> > GetPortfolio()
        {
            using (var ctx = new ApplicationDbContext())
            {
                var service = new ShareAPIService();

                var query = await ctx.Portfolios
                            .Where(port => port.AuthorId == _userId)
                            .Select(port => new PortfolioListItem
                {
                    Name = port.Name,

                    StocksInPortfolio = port.StocksInPortfolio.Select(stockport => new StockPortfolioListItem
                    {
                        StockId   = stockport.Stock.StockId,
                        StockName = stockport.Stock.StockName,
                        Ticker    = stockport.Stock.Ticker
                    }).ToList(),

                    ETFsInPortfolio = port.ETFInPortfolio.Select(etfPort => new ETFPortfolioListItem
                    {
                        ETFId   = etfPort.ETF.ETFId,
                        ETFName = etfPort.ETF.Name,
                        Ticker  = etfPort.ETF.Ticker
                    }).ToList()
                }).ToArrayAsync();

                return(query);
            }
        }
Exemplo n.º 2
0
        public async Task <List <StockWithNewData> > GetStockByTicker(string ticker)
        {
            using (var ctx = new ApplicationDbContext())
            {
                var service = new ShareAPIService();

                var stockNewData = await service.GetStockInfo(ticker);

                var stockNewPrice = await service.GetStockPrice(ticker);

                DateTime datetime  = DateTime.Parse(stockNewData.values[0].datetime);
                double   open      = double.Parse(stockNewData.values[0].open);
                double   high      = double.Parse(stockNewData.values[0].high);
                double   low       = double.Parse(stockNewData.values[0].low);
                double   close     = double.Parse(stockNewData.values[0].close);
                double   volume    = double.Parse(stockNewData.values[0].volume);
                double   price     = double.Parse(stockNewPrice.price);
                string   buyOrHold = (Convert.ToDouble(low) < Convert.ToDouble(open)) ? "Buy it" : "Hold it";


                var query = await ctx.Stocks
                            .Where(Stock => Stock.Ticker == ticker)
                            .Select(n => new StockWithNewData
                {
                    StockName = n.StockName,
                    Ticker    = n.Ticker,
                    Price     = price,
                    Datetime  = datetime,
                    Open      = open,
                    High      = high,
                    Low       = low,
                    Close     = close,
                    Volume    = volume,
                    BuyOrHold = buyOrHold
                }).ToListAsync();

                return(query);
            }
        }