예제 #1
0
        public async Task <TotalHoldings> GetHoldings(string userId)
        {
            var transactions = _transactionQueryService.GetTransactions(userId);

            if (transactions.Count > 0)
            {
                var latestPrice = await _stockService.LatestPrice(transactions);

                var processedHoldings = _holdingsProcessor.HoldingsCombiner(transactions, latestPrice);

                var holdingsValue = _holdingsProcessor.HoldingsValue(processedHoldings);

                var cash = _walletQueryService.GetWallet(userId);

                var holdings = new TotalHoldings()
                {
                    Cash     = cash.Cash,
                    Value    = holdingsValue,
                    Holdings = processedHoldings
                };

                return(holdings);
            }
            else
            {
                var cash = _walletQueryService.GetWallet(userId);

                var holdings = new TotalHoldings()
                {
                    Cash = cash.Cash,
                };

                return(holdings);
            }
        }
        public void CreateWallet_UserDoesNotExist_ReturnsUserId()
        {
            WalletEntity user = null;

            _walletQueryService.GetWallet(Arg.Any <string>()).Returns(user);

            _walletQueryService.Save(Arg.Any <WalletEntity>());

            var result = _sut.CreateWallet("9fefa208-5c52-4435-a3ca-70d1e9cee692");

            Assert.That(result, Is.EqualTo("9fefa208-5c52-4435-a3ca-70d1e9cee692"));
        }
예제 #3
0
        public void Buy_CashIsGreaterThanTotalPrice_ReturnsWallet()
        {
            var wallet = new WalletEntity()
            {
                Cash = 100.00M
            };

            _walletQueryService.GetWallet(Arg.Any <string>()).Returns(wallet);

            var result = _sut.Buy("9fefa208-5c52-4435-a3ca-70d1e9cee692", "V", 10.00M, 1);

            Assert.That(result.Cash, Is.EqualTo(100.00M));
        }
예제 #4
0
        public WalletEntity Buy(string userId, string stock, decimal price, int quantity)
        {
            var isPurchase = true;

            decimal totalPrice = price * quantity;

            var wallet = _walletQueryService.GetWallet(userId);

            if (wallet.Cash < totalPrice)
            {
                throw new InsufficientAvailableFundsException("There are insufficient funds to complete this transaction.");
            }

            _walletQueryService.Update(wallet.WalletId, totalPrice, isPurchase);

            _transactionQueryService.AddTransaction(userId, stock, price, quantity);

            return(wallet);
        }
예제 #5
0
        public string CreateWallet(string userId)
        {
            var wallet = new WalletEntity()
            {
                UserId = userId,
                Cash   = 100000.00M
            };

            var user = _walletQueryService.GetWallet(userId);

            if (user == null)
            {
                _walletQueryService.Save(wallet);
                return(userId);
            }
            else
            {
                throw new UserAlreadyExistsException($"Error in creating wallet");
            }
        }
예제 #6
0
        public async Task GetHoldings_TransactionCountIsGreaterThanZero_ReturnsHoldings()
        {
            var transactions = new List <TransactionEntity>();

            transactions.Add(new TransactionEntity()
            {
                Price = 1.00M, Quantity = 1, Stock = "v"
            });
            transactions.Add(new TransactionEntity()
            {
                Price = 5.00M, Quantity = 1, Stock = "tsla"
            });
            transactions.Add(new TransactionEntity()
            {
                Price = 10.00M, Quantity = 1, Stock = "goog"
            });

            var latestPrice = new Dictionary <string, LatestPriceModel>();

            latestPrice.Add("v", new LatestPriceModel()
            {
                Quote = new QuoteModel()
                {
                    LatestPrice = 10.00M
                }
            });
            latestPrice.Add("tsla", new LatestPriceModel()
            {
                Quote = new QuoteModel()
                {
                    LatestPrice = 50.00M
                }
            });
            latestPrice.Add("goog", new LatestPriceModel()
            {
                Quote = new QuoteModel()
                {
                    LatestPrice = 100.00M
                }
            });

            var processedHoldings = new List <Holding>();

            processedHoldings.Add(new Holding()
            {
                Stock = "v", LatestPrice = 10.00M, Quantity = 1
            });
            processedHoldings.Add(new Holding()
            {
                Stock = "tsla", LatestPrice = 50.00M, Quantity = 1
            });
            processedHoldings.Add(new Holding()
            {
                Stock = "goog", LatestPrice = 100.00M, Quantity = 1
            });

            var holdingsValue = 160.00M;

            var cash = new WalletEntity()
            {
                Cash = 100.00M
            };

            _transactionQueryService.GetTransactions(Arg.Any <string>()).Returns(transactions);

            _stockService.LatestPrice(Arg.Any <List <TransactionEntity> >()).Returns(latestPrice);

            _holdingsProcessor.HoldingsCombiner(Arg.Any <List <TransactionEntity> >(),
                                                Arg.Any <Dictionary <string, LatestPriceModel> >()).Returns(processedHoldings);

            _holdingsProcessor.HoldingsValue(Arg.Any <List <Holding> >()).Returns(holdingsValue);

            _walletQueryService.GetWallet(Arg.Any <string>()).Returns(cash);

            var result = await _sut.GetHoldings("9fefa208-5c52-4435-a3ca-70d1e9cee692");

            Assert.That(result.Holdings.Count, Is.EqualTo(3));
            Assert.That(result.Cash, Is.EqualTo(100.00M));
            Assert.That(result.Value, Is.EqualTo(160.00M));
        }