public string Post([FromBody] TransactionInfo value) { string StockName = stockService.Get(value.StockID).Type; decimal transactionQuantity = stockService.Get(value.StockID).Price *value.StockAmount; TransactionHistoryEntity deal = new TransactionHistoryEntity() { BuyerBalanceID = value.BuyerBalanceID, SellerBalanceID = value.SellerBalanceID, StockAmount = value.StockAmount, StockName = StockName, TimeOfTransaction = DateTime.Now, TransactionQuantity = transactionQuantity }; transactionsService.Add(deal); transactionsService.SaveChanges(); int id = transactionsService.GetId(value); return(Newtonsoft.Json.JsonConvert.SerializeObject(transactionsService.Get(id))); }
// GET api/stocks //[HttpGet] public string Get() { string stringToreturn = String.Empty; String currurl = Request.RequestUri.ToString(); String querystring; // Check to make sure some query string variables // exist and if not return full list of users. int iqs = currurl.IndexOf('?'); if (iqs == -1) { var users = stocksService.GetAll(); stringToreturn = Newtonsoft.Json.JsonConvert.SerializeObject(users); } // If query string variables exist, put them in // a string. else if (iqs >= 0) { querystring = (iqs < currurl.Length - 1) ? currurl.Substring(iqs + 1) : String.Empty; // Parse the query string variables into a NameValueCollection. NameValueCollection qscoll = HttpUtility.ParseQueryString(querystring); if (qscoll.AllKeys.Contains("clientId")) { int clientID; bool ifPageIsInt = int.TryParse(qscoll.Get("clientId"), out clientID); var obj = balancesService.GetAll(clientID); Dictionary <string, decimal> objToReturn = new Dictionary <string, decimal>(); foreach (BalanceEntity balance in obj) { string nameOfStockType = stocksService.Get(balance.StockID).Type; decimal price = stocksService.Get(balance.StockID).Price; objToReturn.Add(nameOfStockType, price); } stringToreturn = Newtonsoft.Json.JsonConvert.SerializeObject(objToReturn); } } return(stringToreturn); }
public void Initialize() { this.clientTableRepository = Substitute.For <IClientTableRepository>(); this.stockTableRepository = Substitute.For <IStockTableRepository>(); this.stockOfClientsTableRepository = Substitute.For <IStockOfClientsTableRepository>(); this.transactionHistoryTableRepository = Substitute.For <ITransactionHistoryTableRepository>(); this.editClientStockService = new EditCleintStockService(this.stockOfClientsTableRepository); clientTableRepository.Get(5).Returns(new ClientEntity() { ID = 5, Name = "Serj", Surname = "Tankian", PhoneNumber = "+7228133705", AccountBalance = 100, }); clientTableRepository.Get(32).Returns(new ClientEntity() { ID = 32, Name = "Chester", Surname = "Bennington", PhoneNumber = "+7228133705", AccountBalance = 50 }); stockTableRepository.Get(1).Returns(new StockEntity() { ID = 1, Name = "Yandex", Type = "P", Cost = 10 }); stockOfClientsTableRepository.Get(2).Returns(new StockOfClientsEntity() { ID = 2, ClientID = 32, StockID = 1, Amount = 5 }); stockTableRepository.GetCost(Arg.Is <int>(1)).Returns(10); stockOfClientsTableRepository.GetAmount(Arg.Is <int>(5), Arg.Is <int>(1)).Returns(0); stockOfClientsTableRepository.GetAmount(Arg.Is <int>(32), Arg.Is <int>(1)).Returns(10); clientTableRepository.GetBalance(Arg.Is <int>(5)).Returns(100); clientTableRepository.GetBalance(Arg.Is <int>(32)).Returns(50); stockTableRepository.GetType(Arg.Is <int>(1)).Returns("P"); }
public void ShouldChangeBalanceOfTheSeller() { //Arrange historyTableRepository = Substitute.For <IHistoryTableRepository>(); balanceTableRepository = Substitute.For <IBalanceTableRepository>(); stockTableRepository = Substitute.For <IStockTableRepository>(); TransactionService transactionService = new TransactionService(this.historyTableRepository, this.balanceTableRepository, this.stockTableRepository); var args = new TransactionInfo(); args.SellerID = 22; args.SellerBalanceID = "2201"; args.BuyerID = 7; args.BuyerBalanceID = "701"; args.StockAmount = 2; args.StockID = 5; args.dateTime = DateTime.Now; var stock = new StockEntity(); stock.ID = 5; stock.Price = 200; stock.Type = "StockTypeA"; stockTableRepository.Get(5).Returns(stock); var sellerBalance = new BalanceEntity(); sellerBalance.BalanceID = "2201"; sellerBalance.Balance = 1000; sellerBalance.StockID = 3; sellerBalance.StockAmount = 2; sellerBalance.UserID = 22; sellerBalance.CreatedAt = DateTime.Now; balanceTableRepository.Get("2201").Returns(sellerBalance); //Act transactionService.MakeSell(args, stock); //Assert balanceTableRepository.Received(1).Change(Arg.Is <BalanceEntity>(w => w.Balance == sellerBalance.Balance && w.BalanceID == sellerBalance.BalanceID && w.CreatedAt == sellerBalance.CreatedAt && w.StockID == sellerBalance.StockID && w.StockAmount == sellerBalance.StockAmount && w.UserID == sellerBalance.UserID)); balanceTableRepository.Received(1).SaveChanges(); }
public int MakeDeal(TransactionInfo args) { var _args = args; StockEntity stock = stockTableRepository.Get(args.StockID); ValidationOfTransactionService validationService = new ValidationOfTransactionService(this.balanceTableRepository); bool sellerValidation = validationService.CheckPermissionToSell(args.SellerID); bool buyerValidation = validationService.CheckPermissionToBuy(args.BuyerID); if (sellerValidation && buyerValidation) { MakeBuy(_args, stock); return(TransactionHistoryLogger(_args, stock)); } else { throw new ArgumentException($"This deal can't be realized, because seller permission to sell is {sellerValidation} and buyer permission to buy is {buyerValidation}"); } }