public IActionResult AddToCart([FromBody] CartItemViewModel model) { Product ProductToAdd = _context.product.Where(p => p.ProductId == model.ProductId).SingleOrDefault(); Cart ActiveCart; Cart ExistingCart = _context.cart.Include(c => c.CartItems).Where(c => c.UserId == model.UserId && c.Status == "active").SingleOrDefault(); if (ExistingCart == null) { Cart NewCart = new Cart { UserId = model.UserId, Status = "active" }; _context.Add(NewCart); _context.SaveChanges(); ActiveCart = NewCart; } else { ActiveCart = ExistingCart; } if (ActiveCart.ContainsCartItem(ProductToAdd) == false) { if (model.Quantity <= ProductToAdd.Quantity) { CartItem NewCartItem = new CartItem { Quantity = model.Quantity, Cart = ActiveCart, Product = ProductToAdd }; _context.Add(NewCartItem); _context.SaveChanges(); return(StatusCode(201)); } else { string response = "This quantity is not available"; return(Json(response)); } } else { string response = "This product is already in your cart. Please update quantity in cart."; return(Json(response)); } }
public IActionResult NewCustomer(string name) { User Newuser = new User { FirstName = name, Created_At = DateTime.Now }; _context.Add(Newuser); _context.SaveChanges(); return(RedirectToAction("Index")); }
public IActionResult Order(Order order, int UserId, int ProductId, int Quantity) { Product Ordered = _context.Products.SingleOrDefault(p => p.ProductId == ProductId); Order NewOrder = new Order { ProductId = ProductId, UserId = UserId, Quantity = Quantity, Created_At = DateTime.Now }; Ordered.ProductInventory -= Quantity; _context.Add(NewOrder); _context.SaveChanges(); return(RedirectToAction("Index")); }
public IActionResult MakeProduct([FromBody] ProductViewModel model) { if (ModelState.IsValid) { List <Product> ExistingProd = _context.product.Where(p => p.Name == model.Name).ToList(); if (ExistingProd.Count == 0) { Product NewProduct = new Product { CreatedAt = DateTime.Now, Name = model.Name, Quantity = model.Quantity, Description = model.Description }; _context.Add(NewProduct); _context.SaveChanges(); return(StatusCode(201)); } } return(StatusCode(400)); }
public IActionResult AddProduct(NewProduct product) { if (ModelState.IsValid) { Product NewProduct = new Product { ProductName = product.ProductName, ProductDescription = product.ProductDescription, ProductPrice = product.ProductPrice, ProductInventory = product.ProductInventory, ProductImage = product.ProductImage, Created_At = DateTime.Now }; _context.Add(NewProduct); _context.SaveChanges(); return(RedirectToAction("Index")); } return(View("Index")); }
public IActionResult MakeOrder(int UserId) { Order NewOrder = new Order { CreatedAt = DateTime.Now, UserId = UserId }; Cart ExistingCart = _context.cart.Include(c => c.CartItems).ThenInclude(i => i.Product).Where(c => c.UserId == UserId && c.Status == "active").SingleOrDefault(); if (ExistingCart == null) { return(StatusCode(400)); } foreach (var item in ExistingCart.CartItems) { OrderItem NewOrderItem = new OrderItem { Quantity = item.Quantity, Order = NewOrder, Product = item.Product }; _context.Add(NewOrderItem); item.Product.Quantity -= item.Quantity; } ExistingCart.Status = "purchased"; _context.SaveChanges(); return(StatusCode(201)); }