Ejemplo n.º 1
0
        public void WhenRemovingAddedProduct_ShouldClearProductFromCartAndReturnZeroQuantity()
        {
            var productsRepository = new ProductsRepository();
            var cartRepository     = new ShoppingCartRepository();

            var cartManager = new CartCommandManager();
            var canon       = new Product("EOSR1", "Canon EOS R", 1099);

            var addCanonCommand = new AddToCartCommand(
                cartRepository,
                productsRepository,
                canon);

            var removeCanonCommand = new RemoveFromCartCommand(
                cartRepository,
                productsRepository,
                canon
                );

            cartManager.Execute(addCanonCommand);
            cartManager.Execute(addCanonCommand);
            cartManager.Execute(removeCanonCommand);
            cartManager.Execute(removeCanonCommand);
            cartManager.Execute(removeCanonCommand);

            cartRepository.Get("EOSR1").Quantity.Should().Be(0);
        }
Ejemplo n.º 2
0
        public async Task <IActionResult> AddToCart(int id)
        {
            if (ModelState.IsValid && id > 0)
            {
                var userId = await GetUserId();

                var product = await _productsService.GetProduct(id);

                if (!product.Success)
                {
                }
                var command = new AddToCartCommand
                {
                    CustomerId = userId,
                    Product    = product.Data
                };
                var result = await _cartService.AddToCart(command);

                if (result.Success)
                {
                    return(RedirectToAction("Details", "Cart"));
                }

                return(RedirectToAction(product.Data.ProductType.ToString(), "Product"));
            }

            return(View());
        }
        public static void Execute()
        {
            ConsoleExtension.WriteSeparator("Shopping cart example");

            var productRepository      = new ProductRepository();
            var shoppingCartRepository = new ShoppingCartRepository();

            var product = productRepository.GetById("XI-NOTE10");

            var addToCartCommand = new AddToCartCommand(
                product,
                productRepository,
                shoppingCartRepository);

            var increaseQuantityCommand = new ChangeCartQuantityCommand(
                ChangeCartQuantityOperation.Increase,
                product,
                productRepository,
                shoppingCartRepository);

            var manager = new CommandManager();

            manager.Invoke(addToCartCommand);
            manager.Invoke(increaseQuantityCommand);
            manager.Invoke(increaseQuantityCommand);
            manager.Invoke(increaseQuantityCommand);
            manager.Invoke(increaseQuantityCommand);

            PrintCart(shoppingCartRepository);

            manager.Undo();

            PrintCart(shoppingCartRepository);
        }
Ejemplo n.º 4
0
        public static void Main(string[] args)
        {
            var shoppingCartRepository = new ShoppingCartRepository();
            var productRepository      = new ProductRepository();

            var product = productRepository.FindBy("SM7B");

            var addToCartCommand        = new AddToCartCommand(shoppingCartRepository, productRepository, product);
            var increaseQuantityCommand = new ChangeQuantityCommand(ChangeQuantityCommand.Operation.Increase,
                                                                    productRepository,
                                                                    shoppingCartRepository,
                                                                    product);
            var manager = new CommandManager();

            manager.Invoke(addToCartCommand);
            manager.Invoke(increaseQuantityCommand);
            manager.Invoke(increaseQuantityCommand);
            manager.Invoke(increaseQuantityCommand);
            manager.Invoke(increaseQuantityCommand);

            PrintCart(shoppingCartRepository);

            manager.Undo();
            PrintCart(shoppingCartRepository);
        }
Ejemplo n.º 5
0
        private static void TestCommand()
        {
            var shoppingCartRepository = new ShoppingCartRepository();
            var productsRepository     = new ProductsRepository();

            var product = productsRepository.FindBy("SM7B");

            var addToCartCommand = new AddToCartCommand(shoppingCartRepository,
                                                        productsRepository,
                                                        product);

            var increaseQuantityCommand = new ChangeQuantityCommand(
                ChangeQuantityCommand.Operation.Increase,
                shoppingCartRepository,
                productsRepository,
                product);

            var manager = new CommandManager();

            manager.Invoke(addToCartCommand);
            manager.Invoke(increaseQuantityCommand);
            manager.Invoke(increaseQuantityCommand);
            manager.Invoke(increaseQuantityCommand);
            manager.Invoke(increaseQuantityCommand);

            ShoppingCartRepository.PrintCart(shoppingCartRepository);

            manager.Undo();

            ShoppingCartRepository.PrintCart(shoppingCartRepository);
        }
        public async Task <Response <bool> > AddToCart(AddToCartCommand addToCartCommand)
        {
            try
            {
                if (!addToCartCommand.IsValid)
                {
                    return(new Response <bool> {
                        Success = false, Message = "Invalid Object"
                    });
                }
                var result = await _cartRepository.AddToCart(addToCartCommand);

                return(result ?  new Response <bool> {
                    Success = true, Data = result
                } :new Response <bool> {
                    Success = false, Message = "Invalid Object"
                });
            }
            catch (Exception ex)
            {
                return(new Response <bool> {
                    Success = false, Message = ex.GetBaseException().Message
                });
            }
        }
Ejemplo n.º 7
0
        public void it_adds_product_to_cart_and_prints_it_out_using_command_pattern()
        {
            // Arrange
            var shoppingCartRepository = new ShoppingCartRepository();
            var productsRepository     = new ProductsRepository();

            Product product = productsRepository.FindBy("SM7B");

            var addToCartCommand = new AddToCartCommand(shoppingCartRepository,
                                                        productsRepository,
                                                        product);

            var increaseQuantityCommand = new ChangeQuantityCommand(ChangeQuantityCommand.Operation.Increase,
                                                                    shoppingCartRepository,
                                                                    productsRepository,
                                                                    product);

            var manager = new CommandManager();

            manager.Invoke(addToCartCommand);
            manager.Invoke(increaseQuantityCommand);
            manager.Invoke(increaseQuantityCommand);
            manager.Invoke(increaseQuantityCommand);
            manager.Invoke(increaseQuantityCommand);

            PrintCart(shoppingCartRepository);

            manager.Undo();

            PrintCart(shoppingCartRepository);
        }
        public ProductViewModel(ShoppingCartViewModel shoppingCartViewModel,
                                IShoppingCartRepository shoppingCartRepository,
                                IProductRepository productRepository,
                                Product product,
                                int quantity = 0)
        {
            Product  = product;
            Quantity = quantity;

            var addToCartCommand =
                new AddToCartCommand(shoppingCartRepository, productRepository, product);

            var increaseQuantityCommand =
                new ChangeQuantityCommand(ChangeQuantityCommand.Operation.Increase,
                                          shoppingCartRepository,
                                          productRepository,
                                          product);

            var decreaseQuantityCommand =
                new ChangeQuantityCommand(ChangeQuantityCommand.Operation.Decrease,
                                          shoppingCartRepository,
                                          productRepository,
                                          product);

            var removeFromCartCommand =
                new RemoveFromCartCommand(shoppingCartRepository,
                                          productRepository,
                                          product);

            AddToCartCommand = new RelayCommand(
                execute: () => {
                addToCartCommand.Execute();
                shoppingCartViewModel.Refresh();
            },
                canExecute: () => addToCartCommand.CanExecute());

            IncreaseQuantityCommand = new RelayCommand(
                execute: () => {
                increaseQuantityCommand.Execute();
                shoppingCartViewModel.Refresh();
            },
                canExecute: () => increaseQuantityCommand.CanExecute());

            DecreaseQuantityCommand = new RelayCommand(
                execute: () => {
                decreaseQuantityCommand.Execute();
                shoppingCartViewModel.Refresh();
            },
                canExecute: () => decreaseQuantityCommand.CanExecute());

            RemoveFromCartCommand = new RelayCommand(
                execute: () => {
                removeFromCartCommand.Execute();
                shoppingCartViewModel.Refresh();
            },
                canExecute: () => removeFromCartCommand.CanExecute());
        }
Ejemplo n.º 9
0
        public async Task ExecuteAsync_Persists_A_New_CartEntry()
        {
            var e = new CartEntry();

            _table.SetupOperation(e, TableOperationType.Insert);
            var cmd = new AddToCartCommand(e);

            await cmd.ExecuteAsync(_table.Object);

            _table.VerifyOperation(e, TableOperationType.Insert);
        }
Ejemplo n.º 10
0
        public async Task <IActionResult> AddToCart(int ID)
        {
            var userID   = User.Claims.First(x => x.Type == "UserID").Value;
            var cmd      = new AddToCartCommand(ID, userID);
            var response = await _mediator.Send(cmd);

            if (response)
            {
                return(Ok($"Item of ID {ID} has been successfully added to cart."));
            }

            return(BadRequest("Something bad has happend while trying to add new item to cart."));
        }
Ejemplo n.º 11
0
        public void PassValidObject()
        {
            AddToCartCommand command = new AddToCartCommand
            {
                AuthenticatedUserId = Guid.NewGuid(),
                ProductId           = Guid.NewGuid(),
                Quantity            = 1
            };
            AddToCartCommandValidator testSubject = new AddToCartCommandValidator();

            ValidationResult result = testSubject.Validate(command);

            Assert.True(result.IsValid);
        }
Ejemplo n.º 12
0
        public void FailOnMissingProductId()
        {
            AddToCartCommand command = new AddToCartCommand
            {
                AuthenticatedUserId = Guid.NewGuid(),
                ProductId           = Guid.Empty,
                Quantity            = 1
            };
            AddToCartCommandValidator testSubject = new AddToCartCommandValidator();

            ValidationResult result = testSubject.Validate(command);

            Assert.False(result.IsValid);
            Assert.Equal("ProductId", result.Errors.Single().PropertyName);
        }
Ejemplo n.º 13
0
 public ActionResult <ApiResponse> AddToCart(AddToCartCommand command)
 {
     try
     {
         _requestBus.Send(command);
     }
     catch (Exception ex)
     {
         _logger.LogError(ex, "AddToCart", command);
         return(BadRequest("Error adding to the cart"));
     }
     return(new ApiResponse
     {
         Message = "item is added to the cart successfully"
     });
 }
Ejemplo n.º 14
0
        public async Task <bool> AddToCart(AddToCartCommand cartCommand)
        {
            var orderLine = new OrderLines
            {
                Id         = GenerateId(),
                IsActive   = true,
                CustomerId = cartCommand.CustomerId,
                Quantity   = 1,
                Price      = cartCommand.Product.Price,
                CartStatus = CartStatus.Inprogress,
                ProductId  = cartCommand.Product.Id
            };
            await _dbContext.OrderLines.AddAsync(orderLine);

            return(await SaveChangesAsync());
        }
Ejemplo n.º 15
0
        public void it_()
        {
            // Arrange
            var shoppingCartRepository = new ShoppingCartRepository();
            var productsRepository     = new ProductsRepository();

            Product product = productsRepository.FindBy("SM7B");

            var addToCartCommand = new AddToCartCommand(shoppingCartRepository,
                                                        productsRepository,
                                                        product);

            var increaseQuantityCommand = new ChangeQuantityCommand(ChangeQuantityCommand.Operation.Increase,
                                                                    shoppingCartRepository,
                                                                    productsRepository,
                                                                    product);

            var removeAllFromCart = new RemoveAllFromCart(shoppingCartRepository,
                                                          productsRepository,
                                                          product);

            var manager = new CommandManager();

            manager.Invoke(addToCartCommand);
            manager.Invoke(increaseQuantityCommand);
            manager.Invoke(increaseQuantityCommand);
            manager.Invoke(increaseQuantityCommand);
            manager.Invoke(increaseQuantityCommand);

            PrintCart(shoppingCartRepository);

            manager.Undo();

            PrintCart(shoppingCartRepository);

            manager.Invoke(addToCartCommand);
            manager.Invoke(increaseQuantityCommand);
            manager.Invoke(increaseQuantityCommand);
            manager.Invoke(increaseQuantityCommand);
            manager.Invoke(increaseQuantityCommand);

            PrintCart(shoppingCartRepository);

            manager.Invoke(removeAllFromCart);

            PrintCart(shoppingCartRepository);
        }
Ejemplo n.º 16
0
        private static void Main(string[] args)
        {
            var shoppingCartRepository = new ShoppingCartRepository();
            var productsRepository     = new ProductsRepository();

            var product1 = productsRepository.FindBy("SM7B");
            var product2 = productsRepository.FindBy("EOSR1");

            var addProduct1ToCartCommand = new AddToCartCommand(
                shoppingCartRepository,
                productsRepository,
                product1);

            var addProduct2ToCartCommand = new AddToCartCommand(
                shoppingCartRepository,
                productsRepository,
                product2);

            var increaseProduct1QuantityCommand = new ChangeQuantityCommand(
                ChangeQuantityCommand.Operation.Increase,
                shoppingCartRepository,
                productsRepository,
                product1);

            var manager = new CommandManager();

            manager.Invoke(addProduct1ToCartCommand);
            manager.Invoke(addProduct2ToCartCommand);
            manager.Invoke(increaseProduct1QuantityCommand);
            manager.Invoke(increaseProduct1QuantityCommand);
            manager.Invoke(increaseProduct1QuantityCommand);

            shoppingCartRepository.PrintCart();

            manager.Undo();

            Console.WriteLine("--After Undoing--");
            shoppingCartRepository.PrintCart();

            // shoppingCartRepository.Add(product);
            // shoppingCartRepository.IncreaseQuantity(product.ArticleId);
            // shoppingCartRepository.IncreaseQuantity(product.ArticleId);
            // shoppingCartRepository.IncreaseQuantity(product.ArticleId);
            // shoppingCartRepository.IncreaseQuantity(product.ArticleId);
        }
Ejemplo n.º 17
0
        public void WhenAddingNewProductToCart_ShouldIncreaseStockInCartRepository()
        {
            var productsRepository = new ProductsRepository();
            var cartRepository     = new ShoppingCartRepository();

            var cartManager = new CartCommandManager();
            var canon       = new Product("EOSR1", "Canon EOS R", 1099);

            var addCanonCommand = new AddToCartCommand(
                cartRepository,
                productsRepository,
                canon);

            cartManager.Execute(addCanonCommand);
            cartManager.Execute(addCanonCommand);

            cartRepository.Get("EOSR1").Quantity.Should().Be(2);
        }
Ejemplo n.º 18
0
        public void CallAddToCartMethod_WhenUserIsLogged()
        {
            // Arange
            var contextMock            = new Mock <IStoreContext>();
            var writerMock             = new Mock <IWriter>();
            var readerMock             = new Mock <IReader>();
            var factoryMock            = new Mock <IModelFactory>();
            var userServiceMock        = new Mock <IUserService>();
            var loggedUserProviderMock = new Mock <ILoggedUserProvider>();

            userServiceMock.Setup(m => m.IsUserLogged()).Returns(true);

            var command = new AddToCartCommand(loggedUserProviderMock.Object, userServiceMock.Object,
                                               factoryMock.Object, contextMock.Object, writerMock.Object, readerMock.Object);

            // Act
            //  command.Execute();

            // Assert
            // userServiceMock.Verify(m => m.AddToCart(It.IsAny<IList<string>>()), Times.Once);
        }
Ejemplo n.º 19
0
        public void ReturnCustomErrorMessage_WhenUserIsNotLogged()
        {
            // Arange
            var contextMock            = new Mock <IStoreContext>();
            var writerMock             = new Mock <IWriter>();
            var readerMock             = new Mock <IReader>();
            var factoryMock            = new Mock <IModelFactory>();
            var userServiceMock        = new Mock <IUserService>();
            var loggedUserProviderMock = new Mock <ILoggedUserProvider>();

            userServiceMock.Setup(m => m.IsUserLogged()).Returns(false);

            var command = new AddToCartCommand(loggedUserProviderMock.Object, userServiceMock.Object,
                                               factoryMock.Object, contextMock.Object, writerMock.Object, readerMock.Object);

            string expectedResult = "You must Login First!";
            // Act
            var actualResul = command.Execute();

            // Assert
            Assert.AreEqual(expectedResult, actualResul);
        }
 private void MessageCenterSubmitAsync(RecorderViewModel arg1, PredictionData data)
 {
     Device.BeginInvokeOnMainThread(async() =>
     {
         if (data == null)
         {
             await Application.Current.MainPage.DisplayAlert("Error", "No data returned. Please record your command again or contact support", "Cancel", "ok");
         }
         if (data.ClassId.Trim() == "Add to cart" && data.Probability > 70.0)
         {
             AddToCartCommand.Execute(this);
         }
         else if (data.ClassId.Trim() == "Open Cart" && data.Probability > 70.0)
         {
             CardItemCommand.Execute(null);
         }
         else
         {
             await Application.Current.MainPage.DisplayAlert("Error", "Please record your command again. Recording is not clear enough", "Cancel", "ok");
         }
     });
 }
Ejemplo n.º 21
0
 public async Task <CartItemDto> AddToCart(AddToCartCommand request)
 {
     return(await _mediator.Send(request));
 }
Ejemplo n.º 22
0
        public async Task <IActionResult> Add(AddToCartCommand command)
        {
            var result = await _mediator.Send(command);

            return(Ok(result));
        }
 public async Task <IActionResult> Put([FromRoute] AddToCartCommand command) => await ExecuteCommand(command);