public void MakeTransaction(TransactionArguments args) { logger.Log.Info($"Starting transaction with following parameters:" + $"SellerId: {args.SellerId}, BuyerId: {args.BuyerId}, SharesId:{args.SharesId}, Quantity: {args.Quantity}"); try { var seller = clientTableRepository.GetById(args.SellerId); var buyer = clientTableRepository.GetById(args.BuyerId); var sellersItem = seller.Portfolio.FirstOrDefault(p => p.Shares.Id == args.SharesId); var buyersItem = buyer.Portfolio.FirstOrDefault(p => p.Shares.Id == args.SharesId); decimal sum = sharesTableRepository.GetById(args.SharesId).Price *args.Quantity; logger.Log.Info( $"Seller {seller.Name} has {seller.Balance}$ " + $"and {sellersItem.Quantity} {sellersItem.Shares.SharesType} shares that cost" + $"{sellersItem.Shares.Price}before the deal"); logger.Log.Info( $"Buyer {buyer.Name} has {buyer.Balance}$ " + $"and {buyersItem.Quantity} {buyersItem.Shares.SharesType} before the deal"); transactionService.MakeTransaction(args); logger.Log.Info($"Transaction succed."); logger.Log.Info($"Deal amount is : {sum} $"); logger.Log.Info( $"Seller {seller.Name} has {seller.Balance}$ " + $"and {sellersItem.Quantity} {sellersItem.Shares.SharesType} after the deal"); logger.Log.Info( $"Buyer {buyer.Name} has {buyer.Balance}$ " + $"and {buyersItem.Quantity} {buyersItem.Shares.SharesType} after the deal"); } catch (Exception ex) { logger.Log.Error(ex.Message); } }
public void PutMoneyToBalance(ArgumentsForPutMoneyToBalance args) { if (!clientTableRepository.ContainsById(args.ClientId)) { throw new ArgumentException($"Client with Id {args.ClientId} doesn't exist"); } ClientEntity clientToChangeBalance = clientTableRepository.GetById(args.ClientId); clientToChangeBalance.Balance += args.AmountToPut; clientTableRepository.Change(clientToChangeBalance); clientTableRepository.SaveChanges(); }
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 void Initialize() { clientTableRepository = Substitute.For <IClientTableRepository>(); clientTableRepository.GetById(1).Returns(new ClientEntity() { Id = 1, Balance = 0M, Name = "Vlad Blood" }); clientTableRepository.GetClientsInOrangeZone() .Returns(new List <ClientEntity>()); clientTableRepository.GetClientsInBlackZone() .Returns(new List <ClientEntity>()); clientsService = new ClientsService(clientTableRepository); }
public void ShouldPutMoneyToBalance() { //Arrange ArgumentsForPutMoneyToBalance args = new ArgumentsForPutMoneyToBalance() { AmountToPut = 100M, ClientId = 1 }; var balanceBeforeChange = clientTableRepository.GetById(1).Balance; //Act clientTableRepository.ContainsById(args.ClientId).Returns(true); clientsService.PutMoneyToBalance(args); //Assert clientTableRepository.Received(1).Change(Arg.Is <ClientEntity>(s => s.Id == args.ClientId && s.Balance == balanceBeforeChange + args.AmountToPut)); clientTableRepository.Received(1).SaveChanges(); }
public void ShouldChangeSellersBalance() { //Arrange TransactionArguments args = new TransactionArguments(); args.SellerId = 1; args.BuyerId = 2; args.SharesId = 1; args.Quantity = 5; var balanceBeforeChange = clientTableRepository.GetById(1).Balance; var sum = sharesTableRepository.GetById(args.SharesId).Price *args.Quantity; //Act transactionService.MakeTransaction(args); //Assert clientTableRepository.Received(1).Change(Arg.Is <ClientEntity>(s => s.Name == "Seller" && s.Balance == balanceBeforeChange + sum)); clientTableRepository.Received(1).SaveChanges(); }
public void Initialize() { clientTableRepository = Substitute.For <IClientTableRepository>(); sharesTableRepository = Substitute.For <ISharesTableRepository>(); clientSharesTableRepository = Substitute.For <IClientSharesTableRepository>(); transactionHistoryTableRepository = Substitute.For <ITransactionHistoryTableRepository>(); clientTableRepository.ContainsById(1).Returns(true); clientTableRepository.ContainsById(2).Returns(true); clientTableRepository.ContainsById(3).Returns(false); sharesTableRepository.ContainsById(1).Returns(true); sharesTableRepository.ContainsById(2).Returns(true); sharesTableRepository.ContainsById(3).Returns(true); sharesTableRepository.ContainsById(4).Returns(false); clientTableRepository.GetById(1).Returns(new ClientEntity() { Id = 1, Balance = 0M, Name = "Seller", Portfolio = new List <ClientSharesEntity>() { new ClientSharesEntity() { Id = 1, Quantity = 50, Shares = new SharesEntity { Id = 1, Price = 10M, SharesType = "TypeA" } }, new ClientSharesEntity() { Id = 2, Quantity = 10, Shares = new SharesEntity { Id = 3, Price = 15M, SharesType = "TypeC" } } } }); clientTableRepository.GetById(2).Returns(new ClientEntity() { Id = 2, Balance = 100M, Name = "Buyer", Portfolio = new List <ClientSharesEntity>() { new ClientSharesEntity() { Id = 2, Quantity = 0, Shares = new SharesEntity { Id = 2, Price = 5M, SharesType = "TypeB" } }, new ClientSharesEntity() { Id = 3, Quantity = 15, Shares = new SharesEntity { Id = 1, Price = 10M, SharesType = "TypeA" } } } }); sharesTableRepository.GetById(1).Returns(new SharesEntity() { Id = 1, Price = 10M, SharesType = "TypeA" }); sharesTableRepository.GetById(2).Returns(new SharesEntity() { Id = 2, Price = 5M, SharesType = "TypeB" }); sharesTableRepository.GetById(3).Returns(new SharesEntity() { Id = 3, Price = 15M, SharesType = "TypeC" }); transactionService = new TransactionService( clientTableRepository, clientSharesTableRepository, sharesTableRepository, transactionHistoryTableRepository); }