示例#1
0
        public void AddToCart(Product product, int quantity)
        {
            var shoppingCartItem = _context.CartItems.SingleOrDefault(s => s.Product.Id == product.Id && s.CartId == CartId);

            if (shoppingCartItem == null)
            {
                shoppingCartItem = new CartItem
                {
                    CartId   = CartId,
                    Product  = product,
                    Quantity = quantity
                };
                _context.CartItems.Add(shoppingCartItem);
            }
            else
            {
                shoppingCartItem.Quantity++;
            }
            _context.SaveChanges();
        }
示例#2
0
        public IActionResult Create(OrderInfo orderInfo)
        {
            try
            {
                Order order = new Order
                {
                    DateTime     = DateTime.Now,
                    CustomerName = orderInfo.CustomerName,
                    Address      = orderInfo.Address,
                    PhoneNumber  = orderInfo.PhoneNumber,
                    Email        = orderInfo.Email,
                    UserName     = orderInfo.Username,
                    State        = orderInfo.State,
                    OrderTotal   = orderInfo.OrderTotal.ToString()
                };
                _context.Orders.Add(order);
                _context.SaveChanges();

                foreach (OrderDetailsInfo orderDetailsInfo in
                         orderInfo.orderDetailsInfo)
                {
                    OrderDetails orderDetails = new OrderDetails
                    {
                        OrderId     = order.Id,
                        ProductId   = orderDetailsInfo.ProductId,
                        ProductName = orderDetailsInfo.ProductName,
                        Quantity    = orderDetailsInfo.Quantity,
                        Price       = orderDetailsInfo.Price,
                        Size        = orderDetailsInfo.Size
                    };
                    _context.OrderDetails.Add(orderDetails);
                    _context.SaveChanges();
                }
                return(Ok());
            }
            catch (Exception e)
            {
                return(BadRequest());
            }
        }
示例#3
0
 //201
 public ActionResult <ProductDTO> CreateProduct(ProductDTO productDTO)
 {
     _context.Products.Add(productDTO.ToProduct());
     _context.SaveChanges();
     return(CreatedAtAction(nameof(GetProduct), new { id = productDTO.Id }, productDTO));
 }