public void CartViewModelValidator()
        {
            var validator = new CartViewModelValidator();

            validator.ShouldNotHaveValidationErrorFor(e => e.MovieId, "aaa111");
            validator.ShouldHaveValidationErrorFor(e => e.MovieId, string.Empty);
            validator.ShouldNotHaveValidationErrorFor(e => e.Price, 1M);
            validator.ShouldHaveValidationErrorFor(e => e.Price, 0);
        }
        public ActionResult AdminEditCartItems([FromBody] CartViewModel _cartItem, int Id)
        {
            CartViewModelValidator validator = new CartViewModelValidator();
            ValidationResult       results   = validator.Validate(_cartItem);

            if (!results.IsValid)
            {
                foreach (var failure in results.Errors)
                {
                    Errors.AddErrorToModelState(failure.PropertyName, failure.ErrorMessage, ModelState);
                }
            }

            if (!ModelState.IsValid || _cartItem.CartQuantity == 0)
            {
                return(BadRequest(ModelState));
            }

            if (_context.Users.Find(Id) != null)
            {
                var cartItem = _context.CartProducts
                               .Where(c => c.Cart.UserId == Id && c.ProductId == _cartItem.ProductId)
                               .Select(a => a).ToArray();

                if (cartItem.Length == 0)
                {
                    return(NotFound());
                }

                var oldQuantity = cartItem[0].CartQuantity;
                var stockid     = (_context.Stock.Where(s => s.Product.Id == _cartItem.ProductId).Select(p => p.Id)).ToArray().First();
                var stock       = _context.Stock.Find(stockid);

                if (stock.ProductQuantity + oldQuantity < _cartItem.CartQuantity)
                {
                    cartItem[0].CartQuantity = stock.ProductQuantity + oldQuantity;
                    stock.ProductQuantity    = 0;
                }

                else
                {
                    cartItem[0].CartQuantity = _cartItem.CartQuantity;
                    stock.ProductQuantity    = stock.ProductQuantity + oldQuantity - _cartItem.CartQuantity;
                }

                TotalPrice(cartItem[0].CartId);

                _context.Update(stock);
                _context.CartProducts.Update(cartItem[0]);
                _context.SaveChanges();

                return(Ok());
            }
            return(NotFound());
        }
Beispiel #3
0
        public ActionResult DeleteCartItems([FromBody] CartViewModel _cartItem)
        {
            CartViewModelValidator validator = new CartViewModelValidator();
            ValidationResult       results   = validator.Validate(_cartItem);

            if (!results.IsValid)
            {
                foreach (var failure in results.Errors)
                {
                    Errors.AddErrorToModelState(failure.PropertyName, failure.ErrorMessage, ModelState);
                }
            }

            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            var userId   = _caller.Claims.Single(c => c.Type == "id");
            var cartItem = (from item in _context.CartProducts
                            where item.Cart.UserId == int.Parse(userId.Value) && item.ProductId == _cartItem.ProductId
                            select item).ToArray();
            var cartId = (from item in _context.Users
                          where item.Id == int.Parse(userId.Value)
                          select item.Cart.Id).ToArray();

            if (cartItem.Length == 0)
            {
                return(NotFound());
            }

            var stockid = (_context.Stock.Where(s => s.Product.Id == _cartItem.ProductId).Select(p => p.Id)).ToArray().First();
            var stock   = _context.Stock.Find(stockid);

            stock.ProductQuantity = stock.ProductQuantity + cartItem[0].CartQuantity;

            _context.Stock.Update(stock);
            _context.CartProducts.Remove(cartItem[0]);
            _context.SaveChanges();

            TotalPrice(cartId[0]);

            return(Ok());
        }
Beispiel #4
0
        public ActionResult PostCartItems([FromBody] CartViewModel _cartItem)
        {
            CartViewModelValidator validator = new CartViewModelValidator();
            ValidationResult       results   = validator.Validate(_cartItem);

            if (!results.IsValid)
            {
                foreach (var failure in results.Errors)
                {
                    Errors.AddErrorToModelState(failure.PropertyName, failure.ErrorMessage, ModelState);
                }
            }

            if (!ModelState.IsValid || _cartItem.CartQuantity == 0)
            {
                return(BadRequest(ModelState));
            }

            var userId = _caller.Claims.Single(c => c.Type == "id");
            var cartId = (from cart in _context.Carts
                          where cart.UserId == int.Parse(userId.Value)
                          select cart.Id).ToArray();
            var cartItem = _context.CartProducts.Where(x => x.CartId == cartId.FirstOrDefault() && x.ProductId == _cartItem.ProductId).Select(x => x).ToArray();

            var stockid = (_context.Stock.Where(s => s.Product.Id == _cartItem.ProductId).Select(p => p.Id)).ToArray().First();
            var stock   = _context.Stock.Find(stockid);

            Console.WriteLine("Lengte van cartItem is: {0}", cartItem.Length);

            if (cartItem.Length >= 1)
            {
                int oldQuantity = cartItem.FirstOrDefault().CartQuantity;

                if (stock.ProductQuantity < _cartItem.CartQuantity)
                {
                    cartItem[0].CartQuantity = stock.ProductQuantity + oldQuantity;
                    stock.ProductQuantity    = 0;
                }

                else
                {
                    cartItem[0].CartQuantity = _cartItem.CartQuantity + oldQuantity;
                    stock.ProductQuantity    = stock.ProductQuantity - cartItem[0].CartQuantity;
                }


                _context.Update(stock);
                _context.CartProducts.Update(cartItem[0]);
                _context.SaveChanges();

                TotalPrice(cartItem[0].CartId);

                return(Ok());
            }

            var remainingQuantity = _cartItem.CartQuantity;


            if (stock.ProductQuantity == 0)
            {
                return(Ok("Niet op vooraad"));
            }

            else if (stock.ProductQuantity < _cartItem.CartQuantity)
            {
                remainingQuantity     = stock.ProductQuantity;
                stock.ProductQuantity = 0;
            }

            else
            {
                stock.ProductQuantity = stock.ProductQuantity - _cartItem.CartQuantity;
            }


            CartProduct product = new CartProduct()
            {
                CartId        = cartId[0],
                ProductId     = _cartItem.ProductId,
                CartQuantity  = remainingQuantity,
                CartDateAdded = DateTime.Now
            };

            _context.Add(product);
            _context.Stock.Update(stock);
            _context.SaveChanges();

            TotalPrice(cartId[0]);

            return(Ok());
        }
        public ActionResult AdminPostCartItems([FromBody] CartViewModel _cartItem, int Id)
        {
            CartViewModelValidator validator = new CartViewModelValidator();
            ValidationResult       results   = validator.Validate(_cartItem);

            if (!results.IsValid)
            {
                foreach (var failure in results.Errors)
                {
                    Errors.AddErrorToModelState(failure.PropertyName, failure.ErrorMessage, ModelState);
                }
            }

            if (!ModelState.IsValid || _cartItem.CartQuantity == 0)
            {
                return(BadRequest(ModelState));
            }

            if (_context.Users.Find(Id) != null)
            {
                var cartId = (from cart in _context.Carts
                              where cart.UserId == Id
                              select cart.Id).ToArray();

                var stockid           = (_context.Stock.Where(s => s.Product.Id == _cartItem.ProductId).Select(p => p.Id)).ToArray().First();
                var stock             = _context.Stock.Find(stockid);
                var remainingQuantity = _cartItem.CartQuantity;


                if (stock.ProductQuantity == 0)
                {
                    return(Ok("Niet op vooraad"));
                }

                else if (stock.ProductQuantity < _cartItem.CartQuantity)
                {
                    remainingQuantity     = stock.ProductQuantity;
                    stock.ProductQuantity = 0;
                }

                else
                {
                    stock.ProductQuantity = stock.ProductQuantity - _cartItem.CartQuantity;
                }

                CartProduct product = new CartProduct()
                {
                    CartId        = cartId[0],
                    ProductId     = _cartItem.ProductId,
                    CartQuantity  = remainingQuantity,
                    CartDateAdded = DateTime.Now
                };

                TotalPrice(cartId[0]);

                _context.Add(product);
                _context.Stock.Update(stock);
                _context.SaveChanges();

                return(Ok());
            }
            return(NotFound());
        }