Exemplo n.º 1
0
        public int AddProduct(
            string id,
            int count)
        {
            var product = _productRepositorie.GetProductById(id);

            _productRepositorie.AddProduct(id, product.ProductName, product.ProductPrice, count);
            return(0);
        }
Exemplo n.º 2
0
 public Product AddProduct(
     int id,
     string name,
     decimal price,
     int count = 1)
 {
     _productRepositories.AddOrUpdateProdcut(id, name, price, count);
     return(_productRepositories.GetProductById(id));
 }
Exemplo n.º 3
0
        public async Task <IActionResult> GetProductById(int?postId)
        {
            if (postId == null)
            {
                return(BadRequest());
            }

            try
            {
                var post = await _ProductRepositories.GetProductById(postId);

                if (post == null)
                {
                    return(NotFound());
                }

                return(Ok(post));
            }
            catch (Exception)
            {
                return(BadRequest());
            }
        }
        public void AddShoppingItem(
            ShoppingCar shoppingCart,
            string productId,
            int count)
        {
            if (shoppingCart == null)
            {
                throw new ArgumentNullException();
            }

            var product = _productRepositorie.GetProductById(productId);

            if (product.ProductCount < count)
            {
                throw new InvalidOperationException($"Product {product.ProductId} count is not enough.");
            }

            var shoppingItem = shoppingCart.ItemListInCar
                               .FirstOrDefault(x => x.ProductId.Equals(productId));

            if (shoppingItem == null)
            {
                shoppingItem = new ShoppingItem
                {
                    ProductId   = productId,
                    ProductName = product.ProductName,
                    ItemPrice   = product.ProductPrice,
                    ItemCount   = count
                };
                shoppingCart.ItemListInCar.Add(shoppingItem);
            }
            else
            {
                shoppingItem.ItemCount += count;
            }
            _productRepositorie.RemoveProduct(productId, count);
        }