コード例 #1
0
        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();
        }
コード例 #2
0
        public StockToTraderEntityDB GetStocksFromSeller(BuyArguments buyArguments)
        {
            var item = this.dbContext.TraderStocks.First(t => t.TraderId == buyArguments.SellerID &&
                                                         t.StockId == buyArguments.StockID);

            return(item);
        }
コード例 #3
0
        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"));
        }
コード例 #4
0
        public void AdditionalStockToCustomer(BuyArguments args)
        {
            var ItemToUpdate = this.dbContext.TraderStocks.First(t =>
                                                                 t.TraderId == args.CustomerID &&
                                                                 t.StockId == args.StockID);

            ItemToUpdate.StockCount += args.StockCount;
        }
コード例 #5
0
        public void SubtractStockFromSeller(BuyArguments args)
        {
            var ItemToUpdate = this.dbContext.TraderStocks.First(t =>
                                                                 t.TraderId == args.SellerID &&
                                                                 t.StockId == args.StockID);

            ItemToUpdate.StockCount -= args.StockCount;
        }
コード例 #6
0
        public void HandleBuy(BuyArguments args)
        {
            this.ValidateBuyArguments(args);

            this.SubtractProductsInGoodsTable(args);
            this.StoreProductsInSoldTable(args);
            this.StoreProductsInSaleHistoryTable(args);
        }
コード例 #7
0
 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();
 }
コード例 #8
0
 public void HandleBuy(BuyArguments args)
 {
     this.ValidateBuyArguments(args);
     this.SubtractStockFromSeller(args);
     this.AdditionStockToCustomer(args);
     this.SubstractBalance(args);
     this.AdditionBalance(args);
     this.SaveHistory(args);
 }
コード例 #9
0
        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);
        }
コード例 #10
0
        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}.");
            }
        }
コード例 #11
0
        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();
        }
コード例 #12
0
        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}.");
                }
            }
        }
コード例 #13
0
        public void ShouldThrowExceptionIfStockAmountsIsNotEnough()
        {
            // Arrange
            var args = new BuyArguments()
            {
                SellerID     = 5,
                CustomerID   = 40,
                StockID      = 7,
                StockCount   = 15,
                PricePerItem = 123.0M
            };

            // Act
            saleHandler.HandleBuy(args);
        }
コード例 #14
0
        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);
        }
コード例 #15
0
        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();
        }
コード例 #16
0
        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();
        }
コード例 #17
0
        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();
        }
コード例 #18
0
        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();
        }
コード例 #19
0
        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();
        }
コード例 #20
0
        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);
            }
        }
コード例 #21
0
        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();
        }
コード例 #22
0
        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);
        }
コード例 #23
0
        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();
        }
コード例 #24
0
        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();
        }
コード例 #25
0
        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();
        }
コード例 #26
0
 public bool ContainsCustomer(BuyArguments args)
 {
     return(this.dbContext.TraderStocks.Any(t =>
                                            t.TraderId == args.CustomerID &&
                                            t.StockId == args.StockID));
 }
コード例 #27
0
 private void SubtractStockFromSeller(BuyArguments args)
 {
     traderStockTableRepository.SubtractStockFromSeller(args);
     traderStockTableRepository.SaveChanges();
 }