public void ShouldChangeSharesNumber()
        {
            // Arrange
            SalesService salesService = new SalesService(
                this.operationTableRepository,
                this.balanceTableRepository,
                this.blockedMoneyTableRepository,
                this.sharesNumberTableRepository,
                this.blockedSharesNumberTableRepository);
            var sharesNumber = new SharesNumberEntity()
            {
                Id     = 4,
                Number = 34
            };
            int newNumber = 45;

            // Act
            bool flag = salesService.ChangeSharesNumber(sharesNumber, newNumber);

            // Assert
            this.sharesNumberTableRepository.Received(1).ChangeNumber(sharesNumber.Id, newNumber);
            this.sharesNumberTableRepository.Received(1).SaveChanges();
            if (!flag)
            {
                throw new ArgumentException("The flag is false");
            }
        }
        public void ShouldCreateSharesNumberForAdditionIfCantSearch()
        {
            // Arrange
            ShareEntity share = new ShareEntity()
            {
                Id = 55
            };
            ClientEntity client = new ClientEntity()
            {
                Id = 3
            };
            SharesNumberEntity nullSharesNumber = null;

            sharesNumberTableRepository.SearchSharesNumberForAddition(Arg.Is(client.Id), Arg.Is(share.Id)).Returns(nullSharesNumber);
            SalesService salesService = new SalesService(
                this.operationTableRepository,
                this.balanceTableRepository,
                this.blockedMoneyTableRepository,
                this.sharesNumberTableRepository,
                this.blockedSharesNumberTableRepository);

            // Act
            var sharesNumberResult = salesService.SearchOrCreateSharesNumberForAddition(client, share);

            // Assert
            this.sharesNumberTableRepository.Received(1).SearchSharesNumberForAddition(client.Id, share.Id);
            this.sharesNumberTableRepository.Received(1).Add(Arg.Any <SharesNumberEntity>());
            this.sharesNumberTableRepository.Received(1).SaveChanges();
        }
Example #3
0
        public bool ChangeSharesNumber(SharesNumberEntity sharesNumber, int newNumber)
        {
            this.sharesNumberTableRepository.ChangeNumber(sharesNumber.Id, newNumber);

            this.sharesNumberTableRepository.SaveChanges();

            return(true);
        }
Example #4
0
        public SharesNumberEntity SearchOrCreateSharesNumberForAddition(ClientEntity client, ShareEntity share)
        {
            SharesNumberEntity result = this.sharesNumberTableRepository.SearchSharesNumberForAddition(client.Id, share.Id);

            if (result == null)
            {
                result = CreateSharesNumber(new SharesNumberRegistrationInfo()
                {
                    Client = client,
                    Share  = share,
                    Number = 0
                });
            }
            return(result);
        }
Example #5
0
        /* 'Shares number' methods
         */
        public SharesNumberEntity CreateSharesNumber(SharesNumberRegistrationInfo args)
        {
            var entityToAdd = new SharesNumberEntity()
            {
                Client = args.Client,
                Share  = args.Share,
                Number = args.Number
            };

            this.sharesNumberTableRepository.Add(entityToAdd);

            this.sharesNumberTableRepository.SaveChanges();

            return(entityToAdd);
        }
        public void ShouldThrowExceptionIfCantFindSharesNumberForBuy()
        {
            // Arrange
            int shareId   = 55;
            int reqNumber = 5;
            SharesNumberEntity nullSharesNumber = null;

            sharesNumberTableRepository.SearchSharesNumberForBuy(Arg.Is(shareId), Arg.Is(reqNumber)).Returns(nullSharesNumber);
            SalesService salesService = new SalesService(
                this.operationTableRepository,
                this.balanceTableRepository,
                this.blockedMoneyTableRepository,
                this.sharesNumberTableRepository,
                this.blockedSharesNumberTableRepository);

            // Act
            var sharesNumberResult = salesService.SearchSharesNumberForBuy(shareId, reqNumber);

            // Assert
        }
        public void ShouldRemoveShareNumber()
        {
            // Arrange
            SalesService salesService = new SalesService(
                this.operationTableRepository,
                this.balanceTableRepository,
                this.blockedMoneyTableRepository,
                this.sharesNumberTableRepository,
                this.blockedSharesNumberTableRepository);
            SharesNumberEntity sharesNumber = new SharesNumberEntity()
            {
                Id = 68
            };

            // Act
            salesService.RemoveSharesNumber(sharesNumber);

            // Assert
            this.sharesNumberTableRepository.Received(1).Remove(sharesNumber.Id);
            this.sharesNumberTableRepository.Received(1).SaveChanges();
        }
 public void Add(SharesNumberEntity entity)
 {
     this.dBContext.SharesNumbers.Add(entity);
 }
Example #9
0
        /* Sale
         * 0.  Get info about purchase from program (Customer, Number of Shares, Total (money))
         * 1.  Create empty operation
         * 2.1 Get Customer balance info
         * 2.2 - Customer balance amount
         * 3.  Create blocked money
         * 4.1 Get Seller shares number info
         * 4.2 - Seller shares number
         * 5.  Create blocked shares number // after that action purchase can't cancel
         * 6.1 Get Seller balance info
         * 6.2 + Seller balance amount
         * 7.1 Get Customer shares number info
         * 7.2 + Customer shares number
         * 8.  Fill operation columns
         * 9.  Remove blocked money
         * 10. Remove blocked shares number
         */
        public void Deal(int customerId, int sellerId, int shareId, int requiredSharesNumber)
        {
            OperationEntity    operation            = null;
            SharesNumberEntity customerSharesNumber = null;
            SharesNumberEntity sellerSharesNumber   = null;
            ClientEntity       customer             = null;
            //ClientEntity seller = null;
            BalanceEntity      customerBalance = null;
            BalanceEntity      sellerBalance   = null;
            ShareEntity        share           = null;
            BlockedMoneyEntity blockedMoney    = null;
            bool blockedMoneyFlag = false;
            BlockedSharesNumberEntity blockedSharesNumber = null;
            bool    blockedSharesNumberFlag = false;
            decimal customerBalanceAmount   = 0M;
            decimal total = 0M;
            int     sellerSharesNumberNumber = 0;

            operation = CreateOperation();
            try
            {
                sellerSharesNumber = SearchSharesNumberForBuy(shareId, requiredSharesNumber); // search required shares on stock exchange
                //seller = sellerSharesNumber.Client;
                //share = sellerSharesNumber.Share;
                share           = GetShare(shareId);
                customerBalance = SearchBalanceByClientId(customerId);
                customer        = customerBalance.Client;
                //sellerBalance = SearchBalanceByClientId(seller.Id);
                sellerBalance = SearchBalanceByClientId(sellerId);

                // get total
                CheckShareAndShareTypeStatuses(share);
                total = share.Type.Cost * requiredSharesNumber;

                // Blocked money
                customerBalanceAmount = customerBalance.Amount;
                if (customerBalanceAmount < total)
                {
                    throw new ArgumentException("Customer don't have enough money.");
                }
                blockedMoneyFlag = ChangeBalance(customerBalance, customerBalanceAmount - total);
                blockedMoney     = CreateBlockedMoney(new BlockedMoneyRegistrationInfo()
                {
                    Operation     = operation,
                    ClientBalance = customerBalance,
                    Total         = total
                });

                // Blocked shares Number
                sellerSharesNumberNumber = sellerSharesNumber.Number;
                blockedSharesNumberFlag  = ChangeSharesNumber(sellerSharesNumber, sellerSharesNumberNumber - requiredSharesNumber);
                blockedSharesNumber      = CreateBlockedSharesNumber(new BlockedSharesNumberRegistrationInfo()
                {
                    ClientSharesNumber = sellerSharesNumber,
                    Operation          = operation,
                    //Share = sellerSharesNumber.Share,
                    //ShareTypeName = sellerSharesNumber.Share.Type.Name,
                    //Cost = sellerSharesNumber.Share.Type.Cost,
                    Number = requiredSharesNumber
                });
            }
            catch
            {
                RemoveOperation(operation);

                if (blockedMoneyFlag)
                {
                    ChangeBalance(customerBalance, customerBalanceAmount + total);
                    if (blockedMoney != null)
                    {
                        RemoveBlockedMoney(blockedMoney);
                    }
                }

                if (blockedSharesNumberFlag)
                {
                    ChangeSharesNumber(sellerSharesNumber, sellerSharesNumberNumber + requiredSharesNumber);
                    if (blockedSharesNumber != null)
                    {
                        RemoveBlockedSharesNumber(blockedSharesNumber);
                    }
                }

                // throw new ArgumentException($"Deal was broken cause: {e.Message}");
                throw;
            }

            if (sellerSharesNumber.Number == 0)
            {
                RemoveSharesNumber(sellerSharesNumber);
            }

            ChangeBalance(sellerBalance, sellerBalance.Amount + total);

            customerSharesNumber = SearchOrCreateSharesNumberForAddition(customer, share);
            ChangeSharesNumber(customerSharesNumber, customerSharesNumber.Number + requiredSharesNumber);

            FillOperationColumns(blockedMoney, blockedSharesNumber);

            RemoveBlockedMoney(blockedMoney);
            RemoveBlockedSharesNumber(blockedSharesNumber);
        }
Example #10
0
        public void RemoveSharesNumber(SharesNumberEntity sharesNumber)
        {
            this.sharesNumberTableRepository.Remove(sharesNumber.Id);

            this.sharesNumberTableRepository.SaveChanges();
        }