Example #1
0
        public CartItem AddToCart(JsonElement request)
        {
            var     productId  = request.GetProperty("productId").GetString();
            var     categoryId = request.GetProperty("categoryId").GetString();
            Product product;

            product = _giftRepository.GetGift(int.Parse(productId));

            var user = GetCurrentUserAsync().Result;
            var cart = _cartRepository.GetAllCarts().LastOrDefault(c => c.CustomerId == user.Id && c.isOrdered == false) ?? new Cart();

            cart.Customer   = user;
            cart.CustomerId = user.Id;
            var cartItem = _cartItemRepository.GetAllCartItems()
                           .FirstOrDefault(c => c.ProductId == product.Id && c.CartId == cart.Id) ?? new CartItem();

            cartItem.Product = product;
            cartItem.Cart    = cart;
            cartItem.Quantity++;
            cartItem.CartId    = cart.Id;
            cartItem.ProductId = product.Id;
            _cartItemRepository.AddOrUpdate(cartItem);
            _cartRepository.AddOrUpdate(cart);

            return(cartItem);
        }
Example #2
0
        public void AddItem(Guid cartId, LineItem item)
        {
            var cart = cartRepository.GetById(cartId);

            if (cart != null)
            {
                if (cart.LineItems.Any() && cart.LineItems.FirstOrDefault(x => x.Product?.Id == item.Product.Id) != null)
                {
                    cart.LineItems.FirstOrDefault(x => x.Product.Id == item.Product.Id).Quantity++;
                }
                else
                {
                    cart.LineItems.Add(item);
                }
            }
            else
            {
                cart = new Cart
                {
                    Id          = cartId,
                    DateCreated = DateTime.Now,
                    LineItems   = new List <LineItem> {
                        item
                    }
                };
            }
            cartRepository.AddOrUpdate(cart);
        }