public void MakeTransaction(TransactionArguments args) { try { ValidateTransaction(args); var seller = clientTableRepository.GetById(args.SellerId); var buyer = clientTableRepository.GetById(args.BuyerId); var sellersItem = GetItemFromPortfolio(seller, args.SharesId); var buyersItem = GetItemFromPortfolio(buyer, args.SharesId); decimal sum = sharesTableRepository.GetById(args.SharesId).Price *args.Quantity; seller.Balance += sum; sellersItem.Quantity -= args.Quantity; buyer.Balance -= sum; buyersItem.Quantity += args.Quantity; clientTableRepository.Change(seller); clientTableRepository.Change(buyer); clientSharesTableRepository.Change(sellersItem); clientSharesTableRepository.Change(buyersItem); clientTableRepository.SaveChanges(); clientSharesTableRepository.SaveChanges(); WriteTransactionHistory(args, sum); } catch (Exception ex) { throw new Exception($"Transaction failed : {ex.Message}"); } }
public int RegisterNewClient(ClientRegistrationInfo args) { var entityToAdd = (new ClientEntity() { RegistationDateTime = DateTime.Now, Name = args.Name, Phone = args.Phone, Balance = 0M }); if (clientTableRepository.Contains(entityToAdd)) { throw new ArgumentException("This client has been already registered"); } clientTableRepository.Add(entityToAdd); clientTableRepository.SaveChanges(); return(entityToAdd.Id); }
private void BuyPartOfTrasaction(TradeInfo tradeInfo) { decimal stockCost = stockTableRepository.GetCost(tradeInfo.Stock_ID); int customerStocks = stockClientTableRepository.GetAmount(tradeInfo.Customer_ID, tradeInfo.Stock_ID); decimal customerMoney = clientTableRepository.GetBalance(tradeInfo.Customer_ID); decimal transactionPrice = stockCost * tradeInfo.Amount; decimal newCustomerBalance = customerMoney - transactionPrice; int newCustomerStockAmount = customerStocks + tradeInfo.Amount; clientTableRepository.UpdateBalance(tradeInfo.Customer_ID, newCustomerBalance); editCleintStockService.Edit(new EditStockOfClientInfo { Client_ID = tradeInfo.Customer_ID, Stock_ID = tradeInfo.Stock_ID, AmountOfStocks = newCustomerStockAmount }); clientTableRepository.SaveChanges(); }