Пример #1
0
        public async Task <Object> retrievePortfolio(int userId)
        {
            // Get user's stocks.
            var res = from Stock in _context.Stocks
                      where Stock.UserId == userId
                      select new Stock
            {
                StockId       = Stock.StockId,
                UserId        = Stock.UserId,
                StockQuantity = Stock.StockQuantity
            };

            // Create new list for unique Stock IDs.
            var listOfUniqueStocks = new List <string>();

            foreach (var Stock in res)
            {
                var tickerSymbol = Stock.StockId;
                listOfUniqueStocks.Add(tickerSymbol);
            }

            // Create new StockInfoPrice array, to match the values in Res with those from the Batch result.
            StockInfoPrice[] stockInfoPrice = new StockInfoPrice[listOfUniqueStocks.Count];

            // Prevent it from moving forward with 0 Unique Stocks.  Returns an empty Array.
            if (listOfUniqueStocks.Count == 0)
            {
                decimal currency = (from User in _context.Users
                                    where User.UserId == userId
                                    select User.UserCurrency).SingleOrDefault();
                var ret = new {
                    UserCurrency = currency,
                    Stock        = stockInfoPrice
                };
                return(ret);
            }

            // Send Stock IDs to stockService for a price check.
            List <StockBatch> batch = await _stockService.FetchBatch(listOfUniqueStocks);

            int i = 0;

            // Put together information from Res and Batch to form an array of a new object.
            foreach (var Stock in res)
            {
                stockInfoPrice[i]               = new StockInfoPrice();
                stockInfoPrice[i].StockId       = Stock.StockId;
                stockInfoPrice[i].UserId        = Stock.UserId;
                stockInfoPrice[i].StockQuantity = Stock.StockQuantity;
                decimal price = Convert.ToDecimal(batch.Find(x => x.symbol.Equals(Stock.StockId.ToUpper())).price);
                stockInfoPrice[i].StockPrice = price;
                decimal change = Convert.ToDecimal(batch.Find(x => x.symbol.Equals(Stock.StockId.ToUpper())).changePercent);
                stockInfoPrice[i].ChangePercent = change;
                i++;
            }

            // Retrieve the User's available currency.
            decimal userCurrency = (from User in _context.Users
                                    where User.UserId == userId
                                    select User.UserCurrency).SingleOrDefault();

            // Package the userCurrency with the new Stock Object, for a single return.
            var retVal = new {
                UserCurrency = userCurrency,
                Stock        = stockInfoPrice
            };

            return(retVal);
        }
Пример #2
0
        public async Task <List <User> > viewLeaderboard(string leagueID)
        {
            // Get userIDs from the LeagueID
            var res = from leagueUsers in _context.LeagueUsers
                      join user in _context.Users on leagueUsers.UserId equals user.UserId
                      where leagueUsers.LeagueId == leagueID && leagueUsers.UserId == user.UserId
                      orderby user.UserCurrency
                      select leagueUsers.UserId;

            var userIds = await res.ToArrayAsync();

            // Get user objects using the userIds
            var users = await _context.Users
                        .Where(x => userIds.Contains(x.UserId))
                        .Include(x => x.Stocks)
                        .ToListAsync();

            // Obtain a full list of unique stocks.
            var listOfUniqueStocks = new List <string>();

            foreach (var user in users)
            {
                var stocksForUser = user.Stocks;
                foreach (var stock in stocksForUser)
                {
                    var tickerSymbol = stock.StockId;
                    // Ensure it does not repeat the same stock twice in the List of Strings.
                    if (!listOfUniqueStocks.Contains(tickerSymbol))
                    {
                        listOfUniqueStocks.Add(tickerSymbol);
                    }
                }
            }

            // Prevent it from moving forward with 0 Unique Stocks.  Returns an empty List.
            List <User> leaderBoard = new List <User>();

            // Use Stock Service and have it compute the current price of every stock.
            List <StockBatch> batch = null; //= await _stockService.FetchBatch(listOfUniqueStocks);

            if (listOfUniqueStocks.Count != 0)
            {
                batch = await _stockService.FetchBatch(listOfUniqueStocks);
            }

            // For every user; have it calculate their current available cash with the amount they hold in stocks.
            decimal funds;

            foreach (var user in users)
            {
                funds = user.UserCurrency;
                var stocksForUser = user.Stocks;
                foreach (var stock in stocksForUser)
                {
                    // Multiply the quantity owned of each stock with the price, and add it to a sum total.
                    decimal price = Convert.ToDecimal(batch.Find(x => x.symbol.Equals(stock.StockId.ToUpper())).price);
                    funds = funds + (stock.StockQuantity * price);
                }
                leaderBoard.Add(new User()
                {
                    UserName = user.UserName, UserId = user.UserId, UserCurrency = funds
                });
            }

            // Sort the LeaderBoard from the best first, worst last and then return the List.
            List <User> sortedLeaderBoard = leaderBoard.OrderByDescending(x => x.UserCurrency).ToList();

            return(sortedLeaderBoard);
        }