Exemplo n.º 1
0
        public async Task <OperationDetails> AddProduct(string basketId, int productId, int quantity = 1)
        {
            try
            {
                var product = await _uow.ShoppingCartItem.Get(l => l.ShoppingCartID == basketId && l.ProductID == productId);

                if (product == null)
                {
                    var lastCartId = await _uow.ShoppingCartItem.GetMaxCartId();

                    product = new ShoppingCartItem
                    {
                        ShoppingCartID = basketId,
                        Quantity       = quantity,
                        ProductID      = productId,
                        DateCreated    = DateTime.Now,
                        ModifiedDate   = DateTime.Now
                    };
                    _uow.ShoppingCartItem.Create(product);

                    if (!await BasketHelper.ProductAvailability(product, _uow))
                    {
                        return(new OperationDetails(OperationDetails.Statuses.Error, "Not enought quantity", "AddProduct"));
                    }
                }
                else
                {
                    product.Quantity += quantity;
                }
                await _uow.Save();

                return(new OperationDetails(OperationDetails.Statuses.Success, "Success operation", "AddProduct"));
            }
            catch (Exception ex)
            {
                return(new OperationDetails(OperationDetails.Statuses.Error, ex.Message, "AddProduct"));
            }
        }