예제 #1
0
        public ActionResult UpdateCart(HoneyInTheCartDto honey, int clientId)
        {
            if (!_context.Clients.Where(x => x.Id == clientId).Any())
            {
                return(new NotFoundResult());
            }

            var honeyInCart = _context.HoneysInTheCart.Where(x => x.ClientId == clientId && x.Name == honey.Name).FirstOrDefault();

            if (honeyInCart == null)
            {
                return(new NotFoundResult());
            }

            honeyInCart.Amount = honey.Amount;

            _context.HoneysInTheCart.Update(honeyInCart);

            _context.SaveChanges();
            return(new OkResult());
        }
예제 #2
0
        public ActionResult AddToCart(HoneyInTheCartDto honey, int clientId)
        {
            if (honey == null || !_context.Clients.Where(x => x.Id == clientId).Any())
            {
                return(new NotFoundResult());
            }

            var  cart        = _context.HoneysInTheCart.Where(x => x.ClientId == clientId).ToList();
            bool isInTheCart = false;

            foreach (var honeyInCart in cart)
            {
                if (honey.Name == honeyInCart.Name)
                {
                    isInTheCart = true;
                    honeyInCart.Amount++;
                    _context.HoneysInTheCart.Update(honeyInCart);
                }
            }

            if (!isInTheCart)
            {
                _context.HoneysInTheCart.Add(new HoneyItem()
                {
                    Name     = honey.Name,
                    Price    = honey.Price,
                    Amount   = honey.Amount,
                    ClientId = clientId
                });
            }


            _context.SaveChanges();

            return(new OkResult());
        }
 public ActionResult UpdateCart(HoneyInTheCartDto honey, int clientId)
 {
     return(cartsService.UpdateCart(honey, clientId));
 }