public Transaction PerformNewDeal(TransactionToAdd transaction) { if (transaction == null || transaction.SellerId == 0 || transaction.BuyerId == 0 || transaction.ShareId == 0 || transaction.Quantity == 0 || transaction.SellerId == transaction.BuyerId) { return(null); } var sellerToChange = tradersRepository.GetTrader(transaction.SellerId); var buyerToChange = tradersRepository.GetTrader(transaction.BuyerId); var sellerShareRecordToChange = portfoliosRepository.GetPortfolio(transaction.SellerId, transaction.ShareId); var sharePrice = sharesRepository.GetPrice(transaction.ShareId); if (sellerToChange == null || buyerToChange == null || sellerShareRecordToChange == null) { return(null); } tradersRepository.AddToBalance(sellerToChange.TraderId, sharePrice * transaction.Quantity); tradersRepository.SubtractFromBalance(buyerToChange.TraderId, sharePrice * transaction.Quantity); portfoliosRepository.RemoveShares(sellerShareRecordToChange, transaction.Quantity); if (sellerShareRecordToChange.Quantity == 0) { portfoliosRepository.RemovePortfolio(sellerShareRecordToChange); } if (portfoliosRepository.GetPortfoliosCount(transaction.BuyerId, transaction.ShareId) > 0) { var buyerShareRecordToChange = portfoliosRepository.GetPortfolio(transaction.BuyerId, transaction.ShareId); if (buyerShareRecordToChange != null) { portfoliosRepository.AddShares(buyerShareRecordToChange, transaction.Quantity); } } else { portfoliosRepository.AddPortfolio(transaction.BuyerId, transaction.ShareId, transaction.Quantity); } var newTransaction = transactionsRepository.AddTransaction(transaction.BuyerId, transaction.SellerId, transaction.ShareId, sharePrice, transaction.Quantity); return(newTransaction); }