Exemple #1
0
        public void Delete(int orderId)
        {
            Order order = _context.Orders.Include(o => o.OrderDetails).FirstOrDefault(o => o.Id == orderId);

            if (order != null)
            {
                _context.Entry(order).State = EntityState.Deleted;
                _context.SaveChanges();
            }
        }
Exemple #2
0
        public string Add(CartItemDto dto)
        {
            if (dto.Quantity <= 0)
            {
                dto.Quantity = 1;
            }
            var product = _context.Products.FirstOrDefault(p => p.Id == dto.ProductId);

            if (product == null)
            {
                throw new KeyNotFoundException($"The product with ID={dto.ProductId} was not found.");
            }
            CartItem cartItem = null;
            Guid     x;

            if (!Guid.TryParse(dto.CartId, out x))
            {
                dto.CartId = Guid.NewGuid().ToString();
            }
            else
            {
                cartItem = _context.CartItems.FirstOrDefault(c => c.CartId == dto.CartId && c.ProductId == dto.ProductId);
            }

            if (cartItem == null)
            { // create new
                cartItem = new CartItem
                {
                    Id        = Guid.NewGuid().ToString(),
                    CartId    = dto.CartId,
                    ProductId = dto.ProductId,
                    Quantity  = dto.Quantity,
                    Created   = DateTime.Now
                };
                _context.CartItems.Add(cartItem);
                _context.SaveChanges();
            }
            else
            {
                cartItem.Quantity = dto.Quantity;
                _context.Entry(cartItem).Property("Quantity").IsModified = true;
                _context.SaveChanges();
            }
            return(cartItem.CartId);
        }