Esempio n. 1
0
        public void RegisterStockForNewClient(StockOfClientInfo args)
        {
            if (!CheckIfStockPriseConteinStockOfClientByTypeOfStock(args.TypeOfStocks))
            {
                throw new ArgumentException("StockPrice Table doesnt contain this type of stock");
            }

            var entityToAdd = new StockOfClientEntity()
            {
                AccountId        = args.AccountId,
                TypeOfStocks     = args.TypeOfStocks.ToUpper(),
                quantityOfStocks = args.quantityOfStocks
            };

            stockOfClientTableRepository.Add(entityToAdd);
            stockOfClientTableRepository.SaveChanges();
            logger.Info($"Stock {entityToAdd.Id} of type {entityToAdd.TypeOfStocks} in quantity {entityToAdd.quantityOfStocks} for account {entityToAdd.AccountId} has been added");
        }
 public void Remove(StockOfClientEntity entity)
 {
     dbContext.StockOfClient.Remove(entity);
 }
 public void Add(StockOfClientEntity entity)
 {
     dbContext.StockOfClient.Add(entity);
 }
Esempio n. 4
0
        public void ShouldMakeATrade()
        {
            //Arrange
            TradingService  tradingService  = new TradingService(accountTableRepository, stockPriceTableRepository, historyTableRepository, stockOfClientTableRepository, logger);
            TransactionInfo transactionInfo = new TransactionInfo();

            transactionInfo.BuyerId          = 1;
            transactionInfo.SellerId         = 2;
            transactionInfo.TypeOfStock      = "AAA";
            transactionInfo.QuantityOfStocks = 20;
            StockPriceEntity stockPriceEntity = new StockPriceEntity()
            {
                TypeOfStock  = transactionInfo.TypeOfStock,
                PriceOfStock = 4
            };
            AccountEntity buyer = new AccountEntity()
            {
                AccountId = 11,
                ClientId  = 1,
                Balance   = 100,
                Stocks    = new List <StockOfClientEntity>()
            };
            AccountEntity seller = new AccountEntity()
            {
                AccountId = 22,
                ClientId  = 2,
                Balance   = 100,
                Stocks    = new List <StockOfClientEntity>()
            };
            StockOfClientEntity stockForSell = new StockOfClientEntity()
            {
                Id               = 222,
                TypeOfStocks     = "AAA",
                AccountId        = 22,
                AccountForStock  = seller,
                quantityOfStocks = 30
            };

            stockPriceTableRepository.GetStockPriceEntityByStockType(Arg.Is <string>(
                                                                         w => w == transactionInfo.TypeOfStock)).Returns(stockPriceEntity);

            accountTableRepository.GetAccountByClientId(Arg.Is <int>(
                                                            w => w == transactionInfo.BuyerId)).Returns(buyer);

            accountTableRepository.GetAccountByClientId(Arg.Is <int>(
                                                            w => w == transactionInfo.SellerId)).Returns(seller);

            stockOfClientTableRepository.GetStockOfClientEntityByAccountIdAndType(Arg.Is <int>(
                                                                                      w => w == seller.AccountId), Arg.Is <string>(
                                                                                      w => w == transactionInfo.TypeOfStock)).Returns(stockForSell);

            stockOfClientTableRepository.GetStockOfClientEntityByAccountIdAndType(Arg.Is <int>(
                                                                                      w => w == buyer.AccountId), Arg.Is <string>(
                                                                                      w => w == transactionInfo.TypeOfStock)).Returns(stockForSell);
            //Act

            seller.Stocks.Add(stockForSell);
            tradingService.MakeATrade(transactionInfo);

            //Assert
            stockOfClientTableRepository.Received(1).Add(Arg.Is <StockOfClientEntity>(
                                                             w => w.AccountForStock == buyer &&
                                                             w.quantityOfStocks == transactionInfo.QuantityOfStocks &&
                                                             w.TypeOfStocks == transactionInfo.TypeOfStock)
                                                         );

            stockOfClientTableRepository.Received(2).SaveChanges();

            accountTableRepository.Received(1).Change(Arg.Is <AccountEntity>(buyer));
            accountTableRepository.Received(1).Change(Arg.Is <AccountEntity>(seller));
            accountTableRepository.Received(2).SaveChanges();

            historyTableRepository.Received(1).Add(Arg.Is <HistoryEntity>
                                                       (w => w.BuyerId == transactionInfo.BuyerId &&
                                                       w.SellerId == transactionInfo.SellerId &&
                                                       w.QuantityOfStocks == transactionInfo.QuantityOfStocks &&
                                                       w.TypeOfStock == transactionInfo.TypeOfStock &&
                                                       w.FullPrice == 80
                                                       ));
            historyTableRepository.Received(1).SaveChanges();
        }