Ejemplo n.º 1
0
        public void StartTrading()
        {
            Random random   = new Random();
            var    seller   = usersService.GetSeller(random.Next(1, userRepo.Count()));
            var    customer = usersService.GetCustomer(random.Next(1, userRepo.Count()), seller.Id);

            if (seller == null || customer == null) //тут проблема что исключение обрабатывается в прокси, а сам прокси в таком случае кидает null, знаю что костыль
            {
                return;
            }

            var     sellerPortfolioForTrade = seller.UsersShares.ToList()[random.Next(0, seller.UsersShares.Count())];
            var     amountSharesForTrade    = random.Next(1, sellerPortfolioForTrade.Amount / 5);
            var     shareForTrade           = sellerPortfolioForTrade.Share;
            decimal CostOfTransaction       = amountSharesForTrade * shareForTrade.Price;

            usersService.ChangeUserBalance(seller.Id, CostOfTransaction);
            usersService.ChangeUserBalance(customer.Id, CostOfTransaction * -1);

            PortfolioEntity customerPortfolio;

            if ((customerPortfolio = customer.UsersShares.Where(us => us.ShareId == shareForTrade.Id).FirstOrDefault()) == null)
            {
                PortfolioInfo customerPortfolioToAdd = new PortfolioInfo()
                {
                    UserId  = customer.Id,
                    ShareId = shareForTrade.Id,
                    Amount  = amountSharesForTrade
                };
                portfolioService.AddNewUsersShares(customerPortfolioToAdd);
            }
            else
            {
                portfolioService.ChangeAmountOfShares(customerPortfolio, amountSharesForTrade);
            }
            portfolioService.ChangeAmountOfShares(sellerPortfolioForTrade, amountSharesForTrade * -1);

            TransactionStoryInfo transactionOperation = new TransactionStoryInfo()
            {
                customerId      = customer.Id,
                sellerId        = seller.Id,
                shareId         = shareForTrade.Id,
                Share           = shareForTrade,
                AmountOfShares  = amountSharesForTrade,
                DateTime        = DateTime.Now,
                TransactionCost = CostOfTransaction
            };

            transaction.AddNewTransaction(transactionOperation);
        }
Ejemplo n.º 2
0
        public void PostMake(JObject json)
        {
            var args  = JsonConvert.DeserializeObject <TransactionStoryInfo>(json.ToString());
            var share = shareServices.GetShareById(args.shareId);

            if (share == null)
            {
                return;
            }
            //args.Share = share; // из-за этого добавляется в табл с акциями новая акция
            args.TransactionCost = share.Price * args.AmountOfShares;
            try
            {
                transaction.AddShareInPortfolio(args);
            }
            catch (ArgumentException)
            {
                return;
            }

            transaction.AddNewTransaction(args);
        }