Example #1
0
        public HttpResponseMessage Post([FromBody] TransactionHistoryInfo transactionInfo)
        {
            if (transactionInfo == null)
            {
                return(Request.CreateResponse(new ArgumentException($"Transaction information is empty")));
            }
            var sharesService = clientsSharesService.GetAllClientsShares().Where(
                x => x.ClientID == transactionInfo.SellerClientID && x.ShareID == transactionInfo.ShareID).FirstOrDefault();

            if (sharesService == null)
            {
                return(Request.CreateResponse(new ArgumentException($"Combination of client {transactionInfo.SellerClientID} and {transactionInfo.ShareID} not exist")));
            }
            tradingOperationService.SellAndBuyShares(transactionInfo.BuyerClientID, transactionInfo.SellerClientID, sharesService, transactionInfo.Amount);
            return(Request.CreateResponse(HttpStatusCode.OK));
        }
        public void ClientsTrade()
        {
            var clients = clientService.GetAllClients();

            if (validator.ValidateClientList(clients, logger))
            {
                var tradingClients = clients.OrderBy(x => Guid.NewGuid()).Take(2).ToList();
                if (validator.ValidateTradingClient(tradingClients[0], logger))
                {
                    logger.WriteInfo($"Starting operation between {tradingClients[0].ClientID} and {tradingClients[1].ClientID}");
                    ClientsSharesEntity shareType = tradingClients[0].ClientsShares.Where(x => x.Amount > 0).OrderBy(x => Guid.NewGuid()).First();
                    int numberOfSoldShares        = uniformRandomiser.Next(1, (int)shareType.Amount);
                    tradingOperationService.SellAndBuyShares(tradingClients[0].ClientID, tradingClients[1].ClientID, shareType, numberOfSoldShares);
                    logger.WriteInfo($"Client {tradingClients[0].ClientID} sold {numberOfSoldShares} shares of {shareType.ShareID} to {tradingClients[1].ClientID}");
                    logger.WriteInfo($"Operation successfully ended");
                }
            }
        }
Example #3
0
        public void ShouldNotBuyAndSellSharesMoreThanHave()
        {
            //Arrange
            TradingOperationService operationService = new TradingOperationService(
                clientService, shareService, clientsSharesService);
            int firstClientID  = 1;
            int secondClientID = 2;
            ClientsSharesEntity clientsShares = new ClientsSharesEntity()
            {
                ClientID = 1,
                ShareID  = 1,
                Amount   = 10
            };
            int numberOfSoldShares = 100;

            //Act
            operationService.SellAndBuyShares(firstClientID, secondClientID, clientsShares, numberOfSoldShares);
            //Assert
            clientsSharesService.DidNotReceive().ChangeClientsSharesAmount(Arg.Any <ClientsSharesInfo>());
            clientService.DidNotReceive().ChangeMoney(Arg.Any <int>(), Arg.Any <decimal>());
        }