Пример #1
0
        public async Task <IActionResult> BuyProducts(int[] productIds)
        {
            if (!productIds.Any())
            {
                return(new BadRequestObjectResult("You must chose some items to perchause"));
            }

            var checkInventoryCommand = new CheckInventoryCommand(productIds.ToList());
            var inventoryResult       = await this._mediator.Send(checkInventoryCommand);

            if (inventoryResult == null || !inventoryResult.Succes)
            {
                return(new BadRequestObjectResult("Some items wher out of stock"));
            }

            var buycommand      = new BuyProductsCommand((List <InventoryProduct>)inventoryResult.Payload);
            var perchauseResult = await this._mediator.Send(buycommand);

            if (!perchauseResult)
            {
                return(new BadRequestObjectResult("Something went wrong"));
            }

            return(new OkObjectResult("Yai"));
        }
        public void ThrowArgumentOutOfRangeException_InventoryProductsIsEmpty()
        {
            var command = new BuyProductsCommand(new List <InventoryProduct>());
            var handler = new BuyProductsCommandHandler(this._unitOfwork.Object);

            Assert.ThrowsAnyAsync <ArgumentOutOfRangeException>(() => handler.Handle(command, new CancellationToken()));
        }
        public void ThrowArgumentNullException_InventoryProductsIsNull()
        {
            var command = new BuyProductsCommand(null);
            var handler = new BuyProductsCommandHandler(this._unitOfwork.Object);

            Assert.ThrowsAnyAsync <ArgumentNullException>(() => handler.Handle(command, new CancellationToken()));
        }
        public async void ReturnTrue_AllInventoryProductsUpdated()
        {
            var options = new DbContextOptionsBuilder <ApplicationDbContext>()
                          .UseInMemoryDatabase(databaseName: "ReturnTrue_AllInventoryProductsUpdated")
                          .Options;

            using (var context = new ApplicationDbContext(options))
            {
                var unitOfWork = new UnitOfWork <ApplicationDbContext>(context);

                unitOfWork.Repository.Add(new InventoryProduct()
                {
                    Amount = 5, Id = 1, ProductId = 1, Price = 10.0
                });
                unitOfWork.Repository.Add(new InventoryProduct()
                {
                    Amount = 5, Id = 2, ProductId = 2, Price = 10.0
                });

                await unitOfWork.SaveChanges();
            }

            using (var context = new ApplicationDbContext(options))
            {
                var unitOfWork = new UnitOfWork <ApplicationDbContext>(context);

                var inventoryProducts = new List <InventoryProduct>()
                {
                    new InventoryProduct()
                    {
                        Amount = 4, Id = 1, ProductId = 1, Price = 10.0
                    },
                    new InventoryProduct()
                    {
                        Amount = 4, Id = 2, ProductId = 2, Price = 10.0
                    }
                };

                var command = new BuyProductsCommand(inventoryProducts);

                var handler = new BuyProductsCommandHandler(unitOfWork);

                Assert.True(handler.Handle(command, new CancellationToken()).Result);
            }
        }