public bool ValidateShareToClient(ClientsSharesInfo shareToClientInfo)
        {
            if (shareToClientInfo.ClientID < 0 && shareToClientInfo.ShareID < 0)
            {
                return(false);
            }

            if (clientsRepository.LoadClientByID(shareToClientInfo.ClientID) == null)
            {
                return(false);
            }

            var clientSharesInfo = new ClientsSharesEntity()
            {
                ClientID = shareToClientInfo.ClientID,
                ShareID  = shareToClientInfo.ShareID
            };

            var clientsSharesEntity = clientsSharesRepository.LoadClientsSharesByID(clientSharesInfo);

            if (clientsSharesEntity != null)
            {
                if (clientsSharesEntity.Amount + shareToClientInfo.Amount < 0)
                {
                    return(false);
                }
            }
            else if (shareToClientInfo.Amount < 0)
            {
                return(false);
            }
            return(true);
        }
Beispiel #2
0
        public void SellAndBuyShares(int firstClientID, int secondClientID, ClientsSharesEntity shareType, int numberOfSoldShares)
        {
            if (numberOfSoldShares > shareType.Amount)
            {
                throw new ArgumentException($"Cannot sell {numberOfSoldShares} that more than client have {shareType.Amount}");
            }
            if (firstClientID == secondClientID)
            {
                throw new ArgumentException($"Cannot sell shares to yourself");
            }
            ClientsSharesInfo sharesInfo = new ClientsSharesInfo()
            {
                ClientID = shareType.ClientID,
                ShareID  = shareType.ShareID,
                Amount   = -numberOfSoldShares
            };

            clientsSharesService.ChangeClientsSharesAmount(sharesInfo);
            sharesInfo.Amount  *= -1;
            sharesInfo.ClientID = secondClientID;
            clientsSharesService.ChangeClientsSharesAmount(sharesInfo);

            decimal shareCost = (decimal)shareService.GetAllShares().Where(x => x.ShareID == shareType.ShareID).Select(x => x.ShareCost).FirstOrDefault();

            clientService.ChangeMoney(firstClientID, shareCost * numberOfSoldShares);
            clientService.ChangeMoney(secondClientID, -(shareCost * numberOfSoldShares));
        }
Beispiel #3
0
        public void SellAndBuyShares(int firstClientID, int secondClientID, ClientsSharesEntity shareType, int numberOfSoldShares)
        {
            if (numberOfSoldShares > shareType.Amount)
            {
                throw new ArgumentException($"Cannot sell {numberOfSoldShares} that more than client have {shareType.Amount}");
            }
            if (firstClientID == secondClientID)
            {
                throw new ArgumentException($"Cannot sell shares to yourself");
            }
            ClientsSharesEntity sharesInfo = new ClientsSharesEntity()
            {
                ClientID       = shareType.ClientID,
                ShareID        = shareType.ShareID,
                Amount         = shareType.Amount - numberOfSoldShares,
                CostOfOneShare = shareType.CostOfOneShare
            };

            clientsSharesService.UpdateShares(sharesInfo);

            sharesInfo.ClientID = firstClientID;
            var secondClientShares = clientsSharesService.GetAllClientsShares().Where(x => x.ClientID == sharesInfo.ClientID && x.ShareID == sharesInfo.ShareID).FirstOrDefault();

            if (secondClientShares == null)
            {
                ClientsSharesInfo clientsSharesInfo = new ClientsSharesInfo()
                {
                    ClientID = sharesInfo.ClientID,
                    ShareID  = sharesInfo.ShareID,
                    Amount   = numberOfSoldShares
                };
                clientsSharesService.AddShares(clientsSharesInfo);
            }
            else
            {
                sharesInfo.Amount = secondClientShares.Amount + numberOfSoldShares;
                clientsSharesService.UpdateShares(sharesInfo);
            }

            balanceService.ChangeMoney(firstClientID, shareType.CostOfOneShare * numberOfSoldShares);
            balanceService.ChangeMoney(secondClientID, -(shareType.CostOfOneShare * numberOfSoldShares));

            TransactionHistoryInfo operationHistoryInfo = new TransactionHistoryInfo()
            {
                BuyerClientID  = firstClientID,
                SellerClientID = secondClientID,
                ShareID        = shareType.ShareID,
                Amount         = numberOfSoldShares,
                SumOfOperation = shareType.CostOfOneShare * numberOfSoldShares,
                DateTime       = DateTime.Now
            };

            operationHistoryService.Add(operationHistoryInfo);
        }
        public async Task <IHttpActionResult> Post(ClientsSharesEntity share)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }
            ClientsSharesInfo sharesInfo = new ClientsSharesInfo()
            {
                Amount = share.Amount, ClientID = share.ClientID, CostOfOneShare = share.CostOfOneShare, ShareID = share.ShareID
            };

            shareService.AddShares(sharesInfo);
            return(Created(share));
        }
Beispiel #5
0
        public void ShouldAddSharesForClient()
        {
            //Arrange
            ClientsSharesService clientsSharesService = new ClientsSharesService(clientsSharesRepository);
            ClientsSharesInfo    clientsSharesInfo    = new ClientsSharesInfo()
            {
                ClientID = 1,
                ShareID  = 2,
                Amount   = 20
            };
            //Act
            var amount = clientsSharesService.ChangeClientsSharesAmount(clientsSharesInfo);

            //Assert
            clientsSharesRepository.Received(1).SaveChanges();
            Assert.AreEqual(35, amount);
        }
Beispiel #6
0
        public string Run(string[] splittedUserInpit, RequestSender requestSender)
        {
            string answer = "";

            if (splittedUserInpit.Length < 5)
            {
                return("Not enough parameters");
            }
            int clientID;

            if (!int.TryParse(splittedUserInpit[1], out clientID))
            {
                return("Cannot parse ID");
            }
            int shareID;

            if (!int.TryParse(splittedUserInpit[2], out shareID))
            {
                return("Cannot parse ID");
            }
            decimal cost;

            if (!decimal.TryParse(splittedUserInpit[3], out cost))
            {
                return("Cannot parse cost");
            }
            int amount;

            if (!int.TryParse(splittedUserInpit[4], out amount))
            {
                return("Cannot parse amount");
            }
            ClientsSharesInfo sharesInfo = new ClientsSharesInfo
            {
                ClientID       = clientID,
                ShareID        = shareID,
                CostOfOneShare = cost,
                Amount         = amount
            };

            requestSender.PostAddShare(sharesInfo, out answer);
            return(answer);
        }
        public void ShouldNotValidateNegativeShareToClientInfo()
        {
            //Arrange
            TradeValidator    tradeValidator  = new TradeValidator(clientsRepository, shareRepository, clientsSharesRepository);
            ClientsSharesInfo clientShareInfo = new ClientsSharesInfo()
            {
                ClientID = 1,
                ShareID  = 2,
                Amount   = -30
            };
            //Act
            var isValid = tradeValidator.ValidateShareToClient(clientShareInfo, logger);

            //Assert
            Assert.AreEqual(false, isValid);
            clientsRepository.Received(1).LoadClientByID(clientShareInfo.ClientID);
            shareRepository.Received(1).LoadShareByID(clientShareInfo.ShareID);
            clientsSharesRepository.Received(1).LoadClientsSharesByID(clientShareInfo);
        }
Beispiel #8
0
 public void AddShares(ClientsSharesInfo clientsSharesInfo)
 {
     if (validator.ValidateShareToClient(clientsSharesInfo))
     {
         var clientsShares = new ClientsSharesEntity()
         {
             ClientID       = clientsSharesInfo.ClientID,
             ShareID        = clientsSharesInfo.ShareID,
             Amount         = clientsSharesInfo.Amount,
             CostOfOneShare = clientsSharesInfo.CostOfOneShare
         };
         var clientSharesToAdd = clientsSharesRepository.LoadClientsSharesByID(clientsShares);
         if (clientSharesToAdd != null)
         {
             return;
         }
         clientsSharesRepository.Add(clientsShares);
         clientsSharesRepository.SaveChanges();
     }
 }
Beispiel #9
0
        public void ShouldRegisterNewSharesForClient()
        {
            //Arrange
            ClientsSharesService clientsSharesService = new ClientsSharesService(clientsSharesRepository);
            ClientsSharesInfo    clientsSharesInfo    = new ClientsSharesInfo()
            {
                ClientID = 2,
                ShareID  = 1,
                Amount   = 20
            };
            //Act
            var amount = clientsSharesService.ChangeClientsSharesAmount(clientsSharesInfo);

            //Assert
            clientsSharesRepository.Received(1).Add(Arg.Is <ClientsSharesEntity>(
                                                        w => w.ClientID == clientsSharesInfo.ClientID &&
                                                        w.ShareID == clientsSharesInfo.ShareID &&
                                                        w.Amount == clientsSharesInfo.Amount));
            clientsSharesRepository.Received(1).SaveChanges();
            Assert.AreEqual(20, amount);
        }
        public int ChangeClientsSharesAmount(ClientsSharesInfo clientsSharesInfo)
        {
            var clientSharesToChange = clientsSharesRepository.LoadClientsSharesByID(clientsSharesInfo);

            if (clientSharesToChange != null)
            {
                clientSharesToChange.Amount += clientsSharesInfo.Amount;
            }
            else
            {
                clientSharesToChange = new ClientsSharesEntity()
                {
                    ShareID  = clientsSharesInfo.ShareID,
                    ClientID = clientsSharesInfo.ClientID,
                    Amount   = clientsSharesInfo.Amount,
                };
                clientsSharesRepository.Add(clientSharesToChange);
            }

            clientsSharesRepository.SaveChanges();
            return((int)clientSharesToChange.Amount);
        }
        public bool ValidateShareToClient(ClientsSharesInfo shareToClientInfo, ILogger logger)
        {
            if (shareToClientInfo.ClientID < 0 && shareToClientInfo.ShareID < 0)
            {
                logger.WriteWarn("ID cannot be less than 0");
                return(false);
            }

            if (clientsRepository.LoadClientByID(shareToClientInfo.ClientID) == null)
            {
                logger.WriteWarn($"Client with ID {shareToClientInfo.ClientID} not exist");
                return(false);
            }

            if (shareRepository.LoadShareByID(shareToClientInfo.ShareID) == null)
            {
                logger.WriteWarn($"Share with ID {shareToClientInfo.ShareID} not exist");
                return(false);
            }

            var clientsSharesEntity = clientsSharesRepository.LoadClientsSharesByID(shareToClientInfo);

            if (clientsSharesEntity != null)
            {
                if (clientsSharesEntity.Amount + shareToClientInfo.Amount < 0)
                {
                    logger.WriteWarn("Amount of shares cannot be less than 0");
                    return(false);
                }
            }
            else if (shareToClientInfo.Amount < 0)
            {
                logger.WriteWarn("Amount of shares cannot be less than 0");
                return(false);
            }
            return(true);
        }
        private void processUserInput(string userInput)
        {
            string[] splitedUserInput = userInput.Split(' ', '\t', ';');
            switch (splitedUserInput[0].ToLower())
            {
            case "addclient":
                ClientRegistrationInfo clientInfo = new ClientRegistrationInfo()
                {
                    FirstName   = splitedUserInput[1],
                    LastName    = splitedUserInput[2],
                    PhoneNumber = splitedUserInput[3]
                };
                registerEntity(validator.ValidateClientInfo, clientService.RegisterClient, clientInfo);
                break;

            case "addshare":
                ShareRegistrationInfo shareInfo = new ShareRegistrationInfo()
                {
                    Name = splitedUserInput[1],
                    Cost = decimal.Parse(splitedUserInput[2])
                };
                registerEntity(validator.ValidateShareInfo, shareService.RegisterShare, shareInfo);
                break;

            case "changesharestoclient":
                ClientsSharesInfo clientsShareInfo = new ClientsSharesInfo()
                {
                    ClientID = int.Parse(splitedUserInput[1]),
                    ShareID  = int.Parse(splitedUserInput[2]),
                    Amount   = int.Parse(splitedUserInput[3])
                };
                registerEntity(validator.ValidateShareToClient, clientsSharesService.ChangeClientsSharesAmount, clientsShareInfo);
                break;

            case "changeclientmoney":
                changeClientsMoney(splitedUserInput);
                break;

            case "showorange":
                showClientsList(clientService.GetClientsFromOrangeZone());
                break;

            case "showblack":
                showClientsList(clientService.GetClientsFromBlackZone());
                break;

            case "showfullclients":
                showClientsList(clientService.GetAllClients());
                break;

            case "showfullshares":
                var fullList = shareService.GetAllShares();
                logger.WriteInfo(phraseProvider.GetPhrase("ShareHeader"));
                foreach (ShareEntity share in fullList)
                {
                    string sharesInfo = $"{share.ShareID.ToString()} {share.ShareName} {share.ShareCost.ToString()}";
                    logger.WriteInfo(sharesInfo);
                }
                logger.WriteInfo("Successfully showed full share list");
                break;

            case "help":
                logger.WriteInfo(phraseProvider.GetPhrase("Help"));
                break;

            case "e":
                break;

            default:
                logger.WriteWarn("Unknown command");
                return;
            }
        }
 public HttpResponseMessage Post([FromBody] ClientsSharesInfo sharesInfo)
 {
     shareService.AddShares(sharesInfo);
     return(Request.CreateResponse(HttpStatusCode.OK));
 }
 public ClientsSharesEntity LoadClientsSharesByID(ClientsSharesInfo clientsSharesInfo)
 {
     return(dbContext.ClientsShares.Where(x => x.ClientID == clientsSharesInfo.ClientID && x.ShareID == clientsSharesInfo.ShareID).FirstOrDefault());
 }
Beispiel #15
0
        public ClientsSharesEntity PostAddShare(ClientsSharesInfo sharesInfo, out string answer)
        {
            string request = "shares/add";

            return(PostCommandsWithReturn <ClientsSharesInfo, ClientsSharesEntity>(request, sharesInfo, out answer));
        }