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); }
public UserEntity GetSeller(int userId) { if (repo.Count() == 0) { throw new ArgumentException("Нет зарегестрированных пользователей"); } UserEntity seller = repo.GetUserById(userId); if (seller == null) { throw new ArgumentException($"Нет пользователя с ID {userId}"); } else if (seller.UsersShares == null) { throw new ArgumentException($"{seller.SurName} {seller.Name} не имеет акций для продажи"); } return(seller); }