コード例 #1
0
        public void Update(int clientId, int stockId, ClientStockInfo clientStockInfo)
        {
            var stockToUpdate = this.GetEntityByCompositeID(clientId, stockId);

            stockToUpdate.Quantity = clientStockInfo.Amount;
            this.unitOfWork.ClientStocks.Update(stockToUpdate);
            this.unitOfWork.Save();
        }
コード例 #2
0
 public IActionResult Update(int clientid, int stockid, [FromBody] ClientStockInfo info)
 {
     try
     {
         this.clientStockService.Update(clientid, stockid, info);
         return(Ok());
     }
     catch (Exception e)
     {
         var ex = e.Message;
         return(StatusCode(500));
     }
 }
コード例 #3
0
        public void AddClientStockToDB(ClientStockInfo args)
        {
            var clientstockToAdd = new ClientStock()
            {
                ClientID = args.ClientId,
                StockID  = args.StockId,
                Quantity = args.Amount
            };

            if (this.unitOfWork.ClientStocks.Get(c => c.ClientID == args.ClientId && c.StockID == args.StockId).Count() != 0)
            {
                throw new ArgumentException("This clientstock exists. Can't continue");
            }
            ;

            this.unitOfWork.ClientStocks.Add(clientstockToAdd);
            this.unitOfWork.Save();
        }
コード例 #4
0
        public IActionResult Add([FromBody] ClientStockInfo clientStock)
        {
            try
            {
                if (clientStock == null)
                {
                    return(BadRequest());
                }

                this.clientStockService.AddClientStockToDB(clientStock);
                return(Ok(clientStock));
            }
            catch (Exception e)
            {
                var ex = e.Message;
                return(StatusCode(500));
            }
        }
コード例 #5
0
        public void AddClientStock(ClientStockInfo args)
        {
            var clientstockToAdd = new ClientStock()
            {
                ClientID = args.ClientId,
                StockID  = args.StockId,
                Quantity = args.Amount
            };

            if (this.tableRepository.ContainsDTO(clientstockToAdd))
            {
                throw new ArgumentException("This clientstock exists. Can't continue");
            }
            ;

            this.tableRepository.Add(clientstockToAdd);
            this.tableRepository.SaveChanges();
        }
コード例 #6
0
        public void ShouldAddNewClientStock()
        {
            //Arrange
            ClientStockService clientStockService = new ClientStockService(clientStockTableRepository);
            ClientStockInfo    clientStockInfo    = new ClientStockInfo
            {
                ClientId = 1,
                StockId  = 1,
                Amount   = 10
            };

            //Act
            clientStockService.AddClientStock(clientStockInfo);
            //Assert
            clientStockTableRepository.Received(1).Add(Arg.Is <ClientStock>(
                                                           w => w.ClientID == 1 &&
                                                           w.StockID == 1 &&
                                                           w.Quantity == 10
                                                           ));
        }
コード例 #7
0
        public void ShouldNotAddNewClientStockIfItExists()
        {
            // Arrange

            ClientStockService clientStockService = new ClientStockService(clientStockTableRepository);
            ClientStockInfo    clientStockInfo    = new ClientStockInfo
            {
                ClientId = 1,
                StockId  = 1,
                Amount   = 10
            };

            // Act
            clientStockService.AddClientStock(clientStockInfo);

            clientStockTableRepository.ContainsDTO(Arg.Is <ClientStock>(
                                                       w => w.ClientID == 1 &&
                                                       w.StockID == 1 &&
                                                       w.Quantity == 10)).Returns(true);

            clientStockService.AddClientStock(clientStockInfo);
        }
コード例 #8
0
        public void RunTraiding()
        {
            int loopcount = 10;

            for (int i = 0; i < loopcount; i++)
            {
                int amountInLotForSale = 10;


                //Select random saler
                Client saler = GetRandomClient();
                //Select random stock for saler
                var clstock = GetRandomClientStock(saler.ClientID);
                if (clstock == null || clstock.Quantity < 10)
                {
                    continue;
                }

                //determine amount for sale
                int    lotsAmount    = clstock.Quantity / amountInLotForSale;
                Random random        = new Random();
                int    amountForSale = random.Next(1, lotsAmount) * amountInLotForSale;



                Task addsalerOrder = Task.Run(
                    () => this.AddOrder(new OrderInfo()
                {
                    ClientId  = clstock.ClientID,
                    StockId   = clstock.StockID,
                    Quantity  = amountForSale,
                    OrderType = OrderInfo.OrdType.Sale
                }));

                addsalerOrder.Wait();

                this.logger.Info($"Order for sale stock {clstock.StockID} for client {clstock.ClientID} has been added to DB");

                int salerorderId = this.GetLastOrder().OrderID;

                Client customer;
                do
                {
                    customer = GetRandomClient();
                }while (customer.ClientID == saler.ClientID);



                Task addcustomerOrder = Task.Run(() => this.AddOrder(new OrderInfo()
                {
                    ClientId  = customer.ClientID,
                    StockId   = clstock.StockID,
                    Quantity  = amountForSale,
                    OrderType = OrderInfo.OrdType.Purchase
                }));

                addcustomerOrder.Wait();
                this.logger.Info($"Order for purchasing stock {clstock.StockID} for client {customer.ClientID} has been added to DB");

                int      customerorderId = this.GetLastOrder().OrderID;
                DateTime dealDateTime    = DateTime.Now;
                decimal  dealPrice       = this.GetStockById(clstock.StockID).Price;

                ClientInfo salerInfo = new ClientInfo()
                {
                    FirstName = saler.FirstName,
                    LastName  = saler.LastName,
                    Phone     = saler.Phone,
                    Balance   = saler.Balance + (dealPrice * amountForSale)
                };

                this.EditClient(saler.ClientID, salerInfo);
                this.logger.Info($"Client {saler.ClientID} balance has been increased by {(dealPrice * amountForSale)}");

                ClientInfo customerInfo = new ClientInfo()
                {
                    FirstName = customer.FirstName,
                    LastName  = customer.LastName,
                    Phone     = customer.Phone,
                    Balance   = customer.Balance - (dealPrice * amountForSale)
                };

                this.EditClient(customer.ClientID, customerInfo);
                this.logger.Info($"Client {customer.ClientID} balance has been reduced by {(dealPrice * amountForSale)}");


                ClientStockInfo salerStockInfo = new ClientStockInfo()
                {
                    ClientId = saler.ClientID,
                    StockId  = clstock.StockID,
                    Amount   = clstock.Quantity - amountForSale
                };
                this.EditClientStockAmount(saler.ClientID, clstock.StockID, salerStockInfo);


                this.logger.Info($"Client {saler.ClientID} stock {clstock.StockID} amount has been reduced on {amountForSale}");

                var customerStock = this.GetAllClientStocks(customer.ClientID).Where(s => s.StockID == clstock.StockID).SingleOrDefault();
                if (customerStock == null)
                {
                    ClientStockInfo clientStockInfo = new ClientStockInfo()
                    {
                        ClientId = customer.ClientID,
                        StockId  = clstock.StockID,
                        Amount   = amountForSale
                    };
                    Task addStock = Task.Run(() => AddClientStock(clientStockInfo));
                    addStock.Wait();
                }

                else
                {
                    ClientStockInfo clientStockInfo = new ClientStockInfo()
                    {
                        ClientId = customer.ClientID,
                        StockId  = clstock.StockID,
                        Amount   = customerStock.Quantity + amountForSale
                    };
                    this.EditClientStockAmount(customer.ClientID, clstock.StockID, clientStockInfo);
                }
                this.logger.Info($"Client {customer.ClientID} stock {clstock.StockID} amount has been increased on {amountForSale}");


                transactionHistoryService.AddTransactionInfo(new TransactionInfo()
                {
                    TrDateTime = dealDateTime
                });
                var lastTransaction = transactionHistoryService.GetLastTransaction();
                this.logger.Info($"Transaction {lastTransaction.TransactionHistoryID} has been added to DB");

                orderService.SetIsExecuted(salerorderId, lastTransaction.TransactionHistoryID);
                this.logger.Info($"Saler's order {salerorderId} status has been set as 'IsExecuted'");
                orderService.SetIsExecuted(customerorderId, lastTransaction.TransactionHistoryID);
                this.logger.Info($"Customer's order {customerorderId} status has been set as 'IsExecuted'");
                this.logger.Info($"Deal is finished");


                Thread.Sleep(10000);
            }
        }
コード例 #9
0
 public void EditClientStockAmount(int clientid, int stockid, ClientStockInfo clientStockInfo)
 {
     httpclient.PostAsJsonAsync($"http://localhost:5000/shares/update?clientid={clientid}&stockid={stockid}", clientStockInfo);
 }
コード例 #10
0
 public Task AddClientStock(ClientStockInfo clientStockInfo)
 {
     return(httpclient.PostAsJsonAsync($"http://localhost:5000/shares/add", clientStockInfo));
 }