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(); }
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()); } }
//201 public ActionResult <ProductDTO> CreateProduct(ProductDTO productDTO) { _context.Products.Add(productDTO.ToProduct()); _context.SaveChanges(); return(CreatedAtAction(nameof(GetProduct), new { id = productDTO.Id }, productDTO)); }