Exemple #1
0
        public void SellStock(int userId, int stockId, int quantity)
        {
            try
            {
                var currentEntry = _factory.PortfolioRepository.GetPortfolioByUserId(userId).Where(p => p.StockId == stockId).First();
                if (currentEntry.Quantity >= quantity)
                {
                    var stock    = _factory.StockRepository.GetStockById(stockId);
                    var proceeds = stock.LastPrice * quantity;

                    Portfolio entry = new Portfolio()
                    {
                        Quantity    = quantity,
                        StockId     = stockId,
                        UserId      = userId,
                        StockSymbol = stock.Symbol
                    };

                    _factory.PortfolioRepository.SellPortfolioEntry(entry);

                    _factory.UserRepository.AddCashValue(userId, proceeds);

                    _factory.TransactionLogRepository.Create(entry, StockAction.Sold);
                }
                else
                {
                    throw new NotEnoughSharesException();
                }
            }
            catch
            {
                throw new NotEnoughSharesException();
            }
        }
        public void BuyStock(int userId, int stockId, int quantity)
        {
            var money = _factory.UserRepository.GetCashValueByUserId(userId);
            var stock = _factory.StockRepository.GetStockById(stockId);
            var cost = stock.LastPrice * quantity;

            if (money >= cost)
            {
                Portfolio portfolio = new Portfolio()
                {
                    Quantity = quantity,
                    StockId = stockId,
                    UserId = userId,
                    StockSymbol = stock.Symbol
                };
                _factory.PortfolioRepository.BuyPortfolioEntry(portfolio);

                _factory.UserRepository.AddCashValue(userId, -1 * cost);

                _factory.TransactionLogRepository.Create(portfolio, StockAction.Bought);
            }
            else
            {
                throw new NotEnoughCashException();
            }
        }
Exemple #3
0
        public void BuyStock(int userId, int stockId, int quantity)
        {
            var money = _factory.UserRepository.GetCashValueByUserId(userId);
            var stock = _factory.StockRepository.GetStockById(stockId);
            var cost  = stock.LastPrice * quantity;


            if (money >= cost)
            {
                Portfolio portfolio = new Portfolio()
                {
                    Quantity    = quantity,
                    StockId     = stockId,
                    UserId      = userId,
                    StockSymbol = stock.Symbol
                };
                _factory.PortfolioRepository.BuyPortfolioEntry(portfolio);

                _factory.UserRepository.AddCashValue(userId, -1 * cost);

                _factory.TransactionLogRepository.Create(portfolio, StockAction.Bought);
            }
            else
            {
                throw new NotEnoughCashException();
            }
        }
        public void SellStock(int userId, int stockId, int quantity)
        {
            try
            {
                var currentEntry = _factory.PortfolioRepository.GetPortfolioByUserId(userId).Where(p => p.StockId == stockId).First();
                if (currentEntry.Quantity >= quantity)
                {
                    var stock = _factory.StockRepository.GetStockById(stockId);
                    var proceeds = stock.LastPrice * quantity;

                    Portfolio entry = new Portfolio()
                    {
                        Quantity = quantity,
                        StockId = stockId,
                        UserId = userId,
                        StockSymbol = stock.Symbol
                    };

                    _factory.PortfolioRepository.SellPortfolioEntry(entry);

                    _factory.UserRepository.AddCashValue(userId, proceeds);

                    _factory.TransactionLogRepository.Create(entry, StockAction.Sold);
                }
                else
                {
                    throw new NotEnoughSharesException();
                }
            }
            catch
            {
                throw new NotEnoughSharesException();
            }
        }