示例#1
0
        public ReturnModel <string> AddToCart(Entity.Domain.Cart.Cart cart, Entity.Domain.Product.Product product, int quantity)
        {
            var result = new ReturnModel <string>();

            try
            {
                //product accessibility control
                if (product == null)
                {
                    result.Data      = $"No products to add to the cart were found!";
                    result.IsSuccess = false;
                }
                else
                {
                    var productToBeAdded = _productService.Get(product.Id).Data;
                    var isEnoughStock    = quantity <= productToBeAdded.UnitsInStock;

                    //stock control
                    if (isEnoughStock)
                    {
                        var cartItem = cart.CartItems.FirstOrDefault(c => c.Product.Id == product.Id);

                        if (cartItem != null) //product has already been added to the card
                        {
                            cartItem.Quantity += quantity;
                            result.Data        = $"{quantity} of {product.DisplayName} added to the card!";
                        }
                        else //product has never been added to the card
                        {
                            cart.CartItems.Add(new CartItem {
                                Product = product, Quantity = quantity
                            });
                            result.Data = $"{quantity} of {product.DisplayName} added to the card!";
                        }
                    }
                    else //the product stock is less than quantity
                    {
                        result.Data =
                            $"Only {productToBeAdded.UnitsInStock} of {product.DisplayName} can be added to the card!";
                        result.IsSuccess = false;
                    }
                }
            }
            catch (Exception ex)
            {
                result.IsSuccess = false;
                result.Exception = ex;
                result.Message   = ex.Message;
            }

            return(result);
        }
示例#2
0
        public ReturnModel <List <CartItem> > ListCart(Entity.Domain.Cart.Cart cart)
        {
            var result = new ReturnModel <List <CartItem> >();

            try
            {
                result.Data = cart.CartItems;
            }
            catch (Exception ex)
            {
                result.IsSuccess = false;
                result.Exception = ex;
                result.Message   = ex.Message;
            }

            return(result);
        }