public IActionResult PostNewTransaction([FromBody] NewTransactionAPIModel apiModel)
        {
            if (ModelState.IsValid)
            {
                TransactionModel newTransaction = new TransactionModel();
                newTransaction.UserId                = userDao.GetUser(apiModel.UserName).Id;
                newTransaction.GameId                = apiModel.GameId;
                newTransaction.StockSymbol           = apiModel.StockSymbol;
                newTransaction.NumberOfShares        = apiModel.NumberOfShares;
                newTransaction.TransactionSharePrice = stockDao.GetStockBySymbol(apiModel.StockSymbol).CurrentPrice;
                newTransaction.IsPurchase            = apiModel.IsPurchase;

                if (newTransaction.IsPurchase)
                {
                    newTransaction.NetTransactionChange = -(newTransaction.NumberOfShares * newTransaction.TransactionSharePrice);
                }
                else
                {
                    newTransaction.NetTransactionChange = newTransaction.NumberOfShares * newTransaction.TransactionSharePrice;
                }

                int newId = transactionDao.AddNewTransaction(newTransaction);
                AddCommissionFee(newTransaction.UserId, newTransaction.GameId);

                // TODO 09a (Controller): Return CreatedAtRoute to return 201
                return(CreatedAtRoute("GetGame", new { id = newId }, newTransaction));
            }
            else
            {
                return(new BadRequestObjectResult(ModelState));
            }
        }
Exemple #2
0
        public IList <OwnedStocksModel> GetOwnedStocksByUserAndGame(int userId, int gameId)
        {
            IList <StockTransaction> transactions = transactDao.GetTransactionsByGameAndUser(gameId, userId);

            Dictionary <string, List <StockTransaction> > transactionDict = new Dictionary <string, List <StockTransaction> >();

            foreach (StockTransaction transaction in transactions)
            {
                if (!transactionDict.ContainsKey(transaction.StockSymbol))
                {
                    transactionDict.Add(transaction.StockSymbol, new List <StockTransaction>());
                    transactionDict[transaction.StockSymbol].Add(transaction);
                }
                else
                {
                    transactionDict[transaction.StockSymbol].Add(transaction);
                }
            }

            IList <OwnedStocksModel> ownedStocks = new List <OwnedStocksModel>();

            foreach (KeyValuePair <string, List <StockTransaction> > kvp in transactionDict)
            {
                if (kvp.Value.Count > 0)
                {
                    OwnedStocksModel ownedStock = new OwnedStocksModel();
                    ownedStock.StockSymbol       = kvp.Value[0].StockSymbol;
                    ownedStock.CompanyName       = kvp.Value[0].CompanyName;
                    ownedStock.CurrentSharePrice = stockDao.GetStockBySymbol(kvp.Key).CurrentPrice;
                    ownedStock.PercentChange     = stockDao.GetStockBySymbol(kvp.Key).PercentChange;

                    int     numShares           = 0;
                    decimal netTotalPriceBought = 0.0M;
                    int     netNumSharesBought  = 0;

                    foreach (StockTransaction transaction in kvp.Value)
                    {
                        if (transaction.IsPurchase)
                        {
                            numShares           += transaction.NumberOfShares;
                            netNumSharesBought  += transaction.NumberOfShares;
                            netTotalPriceBought -= transaction.NetValue;
                        }
                        else
                        {
                            numShares -= transaction.NumberOfShares;
                        }
                    }
                    ownedStock.NumberOfShares = numShares;

                    if (netNumSharesBought != 0)
                    {
                        ownedStock.AvgPurchasedPrice = netTotalPriceBought / netNumSharesBought;
                    }


                    if (ownedStock.NumberOfShares > 0)
                    {
                        ownedStocks.Add(ownedStock);
                    }
                }
            }

            return(ownedStocks);
        }
 public IActionResult GetStockDetail(string symbol)
 {
     return(new JsonResult(stockDao.GetStockBySymbol(symbol)));
 }