Exemple #1
0
        public AddToCart AddProductToCart(string id, Cart cart)
        {
            UserMDB    user    = _sportsShopDBContext.GetUser(id);
            ProductMDB product = _sportsShopDBContext.GetProduct(cart.ProductId);

            AddToCart cartAdd = _graphContext
                                .GetRelations <AddToCart>(user, product)
                                .Where(c => c.Status == AddToCart.CartStatus.InCart || c.Status == AddToCart.CartStatus.Deleted || c.Status == AddToCart.CartStatus.Bought)
                                .FirstOrDefault();

            bool itemInCart = cartAdd != null;

            if (itemInCart && (cartAdd.Status == AddToCart.CartStatus.InCart || cartAdd.Status == AddToCart.CartStatus.Deleted || cartAdd.Status == AddToCart.CartStatus.Bought))
            {
                cartAdd.Quantity += cart.Quantity;
                cartAdd.Date      = System.DateTime.Now;
                cartAdd.Status    = AddToCart.CartStatus.InCart;
                _graphContext.UpdateRelation(user, cartAdd, product);
            }
            else
            {
                cartAdd = new AddToCart(cart.Quantity);
                _graphContext.CreateRelation(user, cartAdd, product);
            }


            return(cartAdd);
        }
        public async Task <IActionResult> Buy(Resources.Bought bought)
        {
            UserMDB user = _sportsShopDBContext.GetUser(bought.UserId);
            IEnumerable <CartModel> products = bought.Articles;

            foreach (var cart in products)
            {
                ProductMDB product = _sportsShopDBContext.GetProduct(cart.prodId);
                product.UnDisp = product.UnDisp - cart.quantity;
                _sportsShopDBContext.UpdateProduct(product.Id, product);

                AddToCart cartAdd = _graphContext
                                    .GetRelations <AddToCart>(user, product)
                                    .Where(c => c.Status == AddToCart.CartStatus.InCart)
                                    .FirstOrDefault();

                bool itemInCart = cartAdd != null;
                if (itemInCart && cartAdd.Status == AddToCart.CartStatus.InCart)
                {
                    cartAdd.Date   = System.DateTime.Now;
                    cartAdd.Status = AddToCart.CartStatus.Bought;
                }

                if (cartAdd != null)
                {
                    for (int i = 0; i < cartAdd.Quantity; i++)
                    {
                        _graphContext.CreateRelation(user, new Models.Bought(), product);
                    }
                }
                cartAdd.Quantity = 0;
                _graphContext.UpdateRelation(user, cartAdd, product);
            }
            return(Ok(_graphContext.GetRelatives <Models.Bought>(user)));
        }