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 void AddShareInPortfolio(TransactionStoryInfo args) { var customer = usersService.GetUserById(args.customerId); var seller = usersService.GetUserById(args.sellerId); if (seller == null || customer == null) //тут залогировано все в прокси, но сами прокси возращают null { throw new ArgumentException("ОШИБКА ТРАНЗАКЦИИ: Неверный клиенты"); } if (seller.UsersShares.Where(us => us.ShareId == args.shareId).FirstOrDefault() == null) { throw new ArgumentException($"ОШИБКА ТРАНЗАКЦИИ: Акция с id {args.shareId} не существует"); } var customerPortfolio = customer.UsersShares.Where(us => us.ShareId == args.shareId).FirstOrDefault(); try { portfolio.ChangeAmountOfShares(seller.UsersShares.Where(us => us.ShareId == args.shareId).FirstOrDefault(), args.AmountOfShares * -1); } catch (ArgumentException ex) { throw ex; } if (customerPortfolio == null) { portfolio.AddNewUsersShares(new PortfolioInfo() { UserId = args.customerId, ShareId = args.shareId, Amount = args.AmountOfShares }); } else { portfolio.ChangeAmountOfShares(customerPortfolio, args.AmountOfShares); } usersService.ChangeUserBalance(args.customerId, args.TransactionCost * -1); // в данной программе нет ограничения на минусовой баланс usersService.ChangeUserBalance(args.sellerId, args.TransactionCost); }