Esempio n. 1
0
    public async Task AddItem(string orderId, Filling filling)
    {
        var(cart, version) = await repository.Get(orderId);

        if (!cart.Items.Contains(filling))
        {
            cart.Items.Add(filling);
        }
        await repository.Put(cart, version);
    }
Esempio n. 2
0
        static void Main(string[] args)
        {
            Console.WriteLine("Command Pattern Example");

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

            var product = productRepository.Find(1);

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

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

            var command = new Command();

            Console.WriteLine("Adding item to shopping cart...");
            command.Invoke(addToCartCommand);
            var addedLineItem = shoppingCartRepository.Get(product.Id);

            Console.WriteLine($"Item added to shopping cart : Name : {addedLineItem.Product.Name} Quantity : {addedLineItem.Quantity}");

            Console.WriteLine("Increasing quantity...");
            command.Invoke(increaseQuantityCommand);
            addedLineItem = shoppingCartRepository.Get(product.Id);
            Console.WriteLine($"Item added to shopping cart : Name : {addedLineItem.Product.Name} Quantity : {addedLineItem.Quantity}");

            Console.WriteLine("Increasing quantity...");
            command.Invoke(increaseQuantityCommand);
            addedLineItem = shoppingCartRepository.Get(product.Id);
            Console.WriteLine($"Item added to shopping cart : Name : {addedLineItem.Product.Name} Quantity : {addedLineItem.Quantity}");

            Console.WriteLine("Increasing quantity...");
            command.Invoke(increaseQuantityCommand);
            addedLineItem = shoppingCartRepository.Get(product.Id);
            Console.WriteLine($"Item added to shopping cart : Name : {addedLineItem.Product.Name} Quantity : {addedLineItem.Quantity}");

            PrintShoppingCart(shoppingCartRepository);

            Console.WriteLine("Undo all actions");
            command.Undo();

            PrintShoppingCart(shoppingCartRepository);
        }
Esempio n. 3
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);
        }
        public ActionResult removeBookFromCart(int id)
        {
            ShoppingCart objSC = repository.Get(id);

            repository.Remove(objSC);
            repository.SaveChanges();
            return(RedirectToAction("Index"));
        }
        public void Handle(QuantityAddedEvent eventData)
        {
            ShoppingCartElement shoppingCartElement = ShoppingCartRepository.Get(eventData.ProductId);

            if (shoppingCartElement == null)
            {
                shoppingCartElement = new ShoppingCartElement();
            }

            shoppingCartElement.ProductId = eventData.ProductId;
            shoppingCartElement.Quantity += eventData.AddedQuantity;
            ShoppingCartRepository.Save(shoppingCartElement);
        }
Esempio n. 6
0
        public IActionResult Index()
        {
            Category food = new Category("food");

            Product apple  = new Product("Apple", 100, food);
            Product almond = new Product("Almond", 150, food);

            Cart cart = shoppingCart.Get(currentCart.Guid);

            shoppingCart.AddItem(apple, 3);
            shoppingCart.AddItem(almond, 1);

            HttpContext.Session.Set("cart", cart);

            Order o = new Order();

            o.Created = DateTime.Now;
            o.Cart    = cart;
            o.CartId  = cart.Id;

            //Campaign
            Campaign campaign1 = new Campaign(food, 10, 3, DiscountType.Rate);
            Campaign campaign2 = new Campaign(food, 20, 5, DiscountType.Rate);
            Campaign campaign3 = new Campaign(food, 30, 1, DiscountType.Amount);

            shoppingCart.ApplyDiscount(campaign1, campaign2, campaign3);
            shoppingCart.GetCampaignDiscount();

            //Coupon
            Coupon coupon = new Coupon(100, 10, DiscountType.Rate);

            shoppingCart.ApplyCoupon(coupon);
            shoppingCart.GetCouponDiscount();

            //Delivery
            var deliveryCost = shoppingCart.GetDeliveryCost();

            o.ShipmentPrice = deliveryCost;

            //TotalAmount After Discount
            var totalAmountDiscount = shoppingCart.GetTotalAmountAfterDiscount();

            o.Amount = o.ShipmentPrice + totalAmountDiscount;

            return(View());
        }
Esempio n. 7
0
        public string GetShoppingCartElement(int productId)
        {
            var product = ProductRepository.GetById(productId);

            if (product == null)
            {
                return("");
            }

            var shoppingCartElement = ShoppingCartRepository.Get(productId);

            if (shoppingCartElement == null)
            {
                return("");
            }

            return(product.Name + ": " + shoppingCartElement.Quantity);
        }
Esempio n. 8
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);
        }
Esempio n. 9
0
        public ShoppingCart GetShoppingCart(int productId)
        {
            var shoppingCartElement = ShoppingCartRepository.Get(productId);

            if (shoppingCartElement == null)
            {
                shoppingCartElement           = new ShoppingCartElement();
                shoppingCartElement.ProductId = productId;
                shoppingCartElement.Quantity  = 0;
            }

            var quantityAdditionFailedEventHandler = new QuantityAdditionFailedEventHandler();
            var quantityAddedDbEventHandler        = new QuantityAddedDbEventHandler();
            var clearShoppingCartEventHandler      = new ClearShoppingCartEventHandler();

            return(new ShoppingCart(new EventPublisher(quantityAdditionFailedEventHandler, quantityAddedDbEventHandler, clearShoppingCartEventHandler),
                                    shoppingCartElement.ProductId,
                                    shoppingCartElement.Quantity));
        }
Esempio n. 10
0
        public void SellProduct(string productId)
        {
            var shoppingCart = _shoppingCarts.Get();

            if (shoppingCart == null)
            {
                throw new ShoppingCartNotFound();
            }

            var product = _products.Get(productId);

            if (product == null)
            {
                throw new ProductNotFound(productId);
            }

            shoppingCart.Add(product);

            _shoppingCarts.Save(shoppingCart);
        }
Esempio n. 11
0
        public bool IsCartFullOfProduct(int productId)
        {
            var shoppingCartElement = ShoppingCartRepository.Get(productId);

            return(shoppingCartElement.Quantity >= 20);
        }