public void ShouldAddStockToCustomerAfterBuyingIfNotExists() { //Arrange var args = new BuyArguments() { SellerID = 5, CustomerID = 40, StockID = 20, //Customer hasnt this stock StockCount = 2, PricePerItem = 100.0M }; var entityToAdd = new StockToTraderEntityDB() { TraderId = args.CustomerID, StockId = args.StockID, StockCount = args.StockCount, PricePerItem = args.PricePerItem }; //Act saleHandler.HandleBuy(args); //Assert this.traderStockTableRepository.Received(1).Add(Arg.Any <StockToTraderEntityDB>()); this.traderStockTableRepository.Received(2).SaveChanges(); }
public StockToTraderEntityDB GetStocksFromSeller(BuyArguments buyArguments) { var item = this.dbContext.TraderStocks.First(t => t.TraderId == buyArguments.SellerID && t.StockId == buyArguments.StockID); return(item); }
public ActionResult MakeDeal(int sellerID, int customerID, int stockID, int stockCount, decimal pricePerItem) { try { var stock = stockService.GetStockById(stockID); BuyArguments buy = new BuyArguments { SellerID = sellerID, CustomerID = customerID, StockID = stockID, StockCount = stockCount, PricePerItem = stock.PricePerItem }; saleService.HandleBuy(buy); } catch (ArgumentException) { return(StatusCode(400, "Operation cancel")); } catch (Exception ex) { return(BadRequest(ex.Message)); } return(Ok("Transaction was succesfully")); }
public void AdditionalStockToCustomer(BuyArguments args) { var ItemToUpdate = this.dbContext.TraderStocks.First(t => t.TraderId == args.CustomerID && t.StockId == args.StockID); ItemToUpdate.StockCount += args.StockCount; }
public void SubtractStockFromSeller(BuyArguments args) { var ItemToUpdate = this.dbContext.TraderStocks.First(t => t.TraderId == args.SellerID && t.StockId == args.StockID); ItemToUpdate.StockCount -= args.StockCount; }
public void HandleBuy(BuyArguments args) { this.ValidateBuyArguments(args); this.SubtractProductsInGoodsTable(args); this.StoreProductsInSoldTable(args); this.StoreProductsInSaleHistoryTable(args); }
private void AdditionBalance(BuyArguments args) { if (!this.traderTableRepository.ContainsById(args.SellerID)) { throw new ArgumentException($"Cant get trader by this id = {args.SellerID}."); } this.traderTableRepository.AdditionBalance(args.SellerID, args.StockCount * args.PricePerItem); this.traderTableRepository.SaveChanges(); }
public void HandleBuy(BuyArguments args) { this.ValidateBuyArguments(args); this.SubtractStockFromSeller(args); this.AdditionStockToCustomer(args); this.SubstractBalance(args); this.AdditionBalance(args); this.SaveHistory(args); }
public IEnumerable <ProductEntity> FindProductsByRequest(BuyArguments buyArguments) { var retVal = new List <ProductEntity>(); foreach (var item in buyArguments.ItemsToBuy) { var itemInDb = this.dbContext.Goods.First(f => f.Name == item.Name); retVal.Add(itemInDb); } return(retVal); }
private void ValidateBuyArguments(BuyArguments args) { if (!this.traderStockTableRepository.ContainsSeller(args)) { throw new ArgumentException($"Imposible to make a sale, because seller hasn`t this stock id = {args.StockID}"); } var checkEntity = traderStockTableRepository.GetStocksFromSeller(args); if (args.StockCount > checkEntity.StockCount) { throw new ArgumentException($"Imposible to make a sale, because seller has only {checkEntity.StockCount} stocks, but requested {args.StockCount}."); } }
private void SubtractProductsInGoodsTable(BuyArguments args) { var productsInStore = this.goodsTableRepository.FindProductsByRequest(args); foreach (var arg in args.ItemsToBuy) { var product = productsInStore.First(f => f.Name == arg.Name); this.goodsTableRepository.SubtractProduct(product.Id, arg.Count); } this.goodsTableRepository.SaveChanges(); }
private void ValidateBuyArguments(BuyArguments args) { var productsInStore = this.goodsTableRepository.FindProductsByRequest(args); foreach (var arg in args.ItemsToBuy) { var product = productsInStore.First(f => f.Name == arg.Name); if (arg.Count > product.Count) { throw new ArgumentException($"Can't handle this request, because products amount is not enough. Product with Name:{product.Name} has only {product.Count} items, but requested {arg.Count}."); } } }
public void ShouldThrowExceptionIfStockAmountsIsNotEnough() { // Arrange var args = new BuyArguments() { SellerID = 5, CustomerID = 40, StockID = 7, StockCount = 15, PricePerItem = 123.0M }; // Act saleHandler.HandleBuy(args); }
public void ShouldThrowExceptionIfNotContainsSellerToAdditional() { //Arrange var args = new BuyArguments() { SellerID = 23, //Bad value CustomerID = 40, StockID = 7, StockCount = 2, PricePerItem = 123.0M }; //Act saleHandler.HandleBuy(args); }
public void SaveHistory(BuyArguments args) { var stockInSaleHistory = new HistoryEntity() { CreateAt = DateTime.Now, SellerID = args.SellerID, CustomerID = args.CustomerID, StockID = args.StockID, StockCount = args.StockCount, TotalPrice = args.StockCount * args.PricePerItem }; this.historyTableRepository.Add(stockInSaleHistory); this.historyTableRepository.SaveChanges(); }
public void ShouldSubtractStockFromSellerAfterSale() { //Arrange var args = new BuyArguments() { SellerID = 5, CustomerID = 40, StockID = 7, StockCount = 2, PricePerItem = 123.0M }; //Act saleHandler.HandleBuy(args); //Assert this.traderStockTableRepository.Received(1).SubtractStockFromSeller(Arg.Any <BuyArguments>()); this.traderStockTableRepository.Received(2).SaveChanges(); }
public void ShouldAdditionalBalanceToSeller() { //Arrange var args = new BuyArguments() { SellerID = 5, CustomerID = 40, StockID = 7, StockCount = 2, PricePerItem = 123.0M }; //Act saleHandler.HandleBuy(args); //Assert this.traderTableRepository.AdditionBalance(5, 246); this.traderStockTableRepository.Received(2).SaveChanges(); }
public void ShouldAddStockToCustomerAfterBuyingIfExists() { //Arrange var args = new BuyArguments() { SellerID = 5, CustomerID = 40, StockID = 7, //Customer has this stock StockCount = 2, PricePerItem = 123.0M }; //Act saleHandler.HandleBuy(args); //Assert this.traderStockTableRepository.Received(1).AdditionalStockToCustomer(Arg.Any <BuyArguments>()); this.traderStockTableRepository.Received(2).SaveChanges(); }
public void AdditionStockToCustomer(BuyArguments args) { var entityToAdd = new StockToTraderEntityDB() { TraderId = args.CustomerID, StockId = args.StockID, StockCount = args.StockCount, PricePerItem = args.PricePerItem }; if (traderStockTableRepository.Contains(entityToAdd)) { traderStockTableRepository.AdditionalStockToCustomer(args); } else { traderStockTableRepository.Add(entityToAdd); } traderStockTableRepository.SaveChanges(); }
public void Run() { var listTradersStock = traderStocks.GetListTradersStock(); Random random = new Random(); int randomNumber = random.Next(1, listTradersStock.Count() + 1); var seller = traderStocks.GetTraderStockById(randomNumber); var listTraders = traders.GetList(); TraderEntity customer; do { randomNumber = random.Next(1, listTraders.Count() + 1); customer = traders.GetTraderById(randomNumber); } while (seller.TraderId == customer.Id); BuyArguments buy = new BuyArguments { SellerID = seller.TraderId, CustomerID = customer.Id, StockID = seller.StockId, StockCount = 2, PricePerItem = seller.PricePerItem }; logger.Info($"Try to make a sale sellerId = {buy.SellerID}, customerId = {buy.CustomerID}, stockId = {buy.StockID}, count = {buy.StockCount}"); try { saleService.HandleBuy(buy); logger.Info($"Succesfully operation for sale sellerId = {buy.SellerID}, customerId = {buy.CustomerID}, stockId = {buy.StockID}, count = {buy.StockCount}"); } catch (ArgumentException e) { logger.Info($"Operation for sale sellerId = {buy.SellerID}, customerId = {buy.CustomerID}, stockId = {buy.StockID}, count = {buy.StockCount} canceled"); logger.Error(e); } }
private void StoreProductsInSoldTable(BuyArguments args) { var productsInStore = this.goodsTableRepository.FindProductsByRequest(args); foreach (var arg in args.ItemsToBuy) { var product = productsInStore.First(f => f.Name == arg.Name); var productInSoldGoods = new SoldGoodsTableEntity() { Id = product.Id, Count = arg.Count, PricePerItem = product.PricePerItem, Name = product.Name, SupplierId = product.SupplierId }; this.soldGoodsTableRepository.Add(productInSoldGoods); } this.soldGoodsTableRepository.SaveChanges(); }
public void ShouldThrowExceptionIfProductAmountsIsNotEnough() { // Arrange SaleService saleHandler = new SaleService( this.supplierTableRepository, this.goodsTableRepository, this.soldGoodsTableRepository, this.saleHistoryTableRepository); var args = new BuyArguments(); args.ItemsToBuy = new List <ItemToBuy>() { new ItemToBuy() { Name = "John's Product A", Count = 2000 } }; // Act saleHandler.HandleBuy(args); }
public void ShouldPopulateSaleHistoryTableOnceWeHaveSaleActivity() { // Arrange SaleService saleHandler = new SaleService( this.supplierTableRepository, this.goodsTableRepository, this.soldGoodsTableRepository, this.saleHistoryTableRepository); var args = new BuyArguments(); args.ItemsToBuy = new List <ItemToBuy>() { new ItemToBuy() { Name = "John's Product A", Count = 2 }, new ItemToBuy() { Name = "Mark's Product B", Count = 2 } }; // Act saleHandler.HandleBuy(args); // Assert foreach (var item in args.ItemsToBuy) { this.saleHistoryTableRepository.Received(1).Add(Arg.Is <SaleHistoryTableEntity>(w => w.Name == item.Name && w.Count == item.Count)); } this.saleHistoryTableRepository.Received(1).SaveChanges(); }
public void ShouldAddNewLineToHistory() { //Arrange var args = new BuyArguments() { SellerID = 40, CustomerID = 5, StockID = 7, StockCount = 2, PricePerItem = 123.0M }; //Act saleHandler.SaveHistory(args); //Assert this.historyTableRepository.Received(1).Add(Arg.Is <HistoryEntity>( w => w.CustomerID == args.CustomerID && w.SellerID == args.SellerID && w.StockID == args.StockID && w.StockCount == args.StockCount && w.TotalPrice == (args.StockCount * args.PricePerItem))); this.historyTableRepository.Received(1).SaveChanges(); }
public void ShouldSubtractFromGoodsTableOnceWeHaveSaleActivity() { // Arrange SaleService saleHandler = new SaleService( this.supplierTableRepository, this.goodsTableRepository, this.soldGoodsTableRepository, this.saleHistoryTableRepository); var args = new BuyArguments(); args.ItemsToBuy = new List <ItemToBuy>() { new ItemToBuy() { Name = "John's Product A", Count = 2 }, new ItemToBuy() { Name = "Mark's Product B", Count = 2 } }; // Act saleHandler.HandleBuy(args); // Assert foreach (var item in args.ItemsToBuy) { this.goodsTableRepository.Received(1).SubtractProduct(this.goodsTableData.First(f => f.Name == item.Name).Id, item.Count); } this.goodsTableRepository.Received(1).SaveChanges(); }
public bool ContainsCustomer(BuyArguments args) { return(this.dbContext.TraderStocks.Any(t => t.TraderId == args.CustomerID && t.StockId == args.StockID)); }
private void SubtractStockFromSeller(BuyArguments args) { traderStockTableRepository.SubtractStockFromSeller(args); traderStockTableRepository.SaveChanges(); }