public async Task <IActionResult> Create([Bind("DrinkID,Title,StyleID,Price,DrinkArtUrl")] Drink drink) { if (ModelState.IsValid) { _context.Add(drink); await _context.SaveChangesAsync(); return(RedirectToAction("Index")); } ViewData["StyleID"] = new SelectList(_context.Styles, "StyleID", "Title", drink.StyleID); return(View(drink)); }
public async Task <IActionResult> AddToCart(int id) { //Retrieve drink from db var addedDrink = await _context.Drinks.SingleOrDefaultAsync(d => d.DrinkID == id); var cart = ShoppingCart.GetCart(_context, HttpContext); await cart.AddToCart(addedDrink); await _context.SaveChangesAsync(); return(RedirectToAction("Index")); }
public async Task <IActionResult> AddressAndPayment([FromServices] DrinkStoreContext dbContext, [FromForm] Order order, CancellationToken requestAborted) { if (!ModelState.IsValid) { return(View(order)); } var formCollection = await HttpContext.Request.ReadFormAsync(); try { if (string.Equals(formCollection["PromoCode"].FirstOrDefault(), PromoCode, StringComparison.OrdinalIgnoreCase) == false) { return(View(order)); } else { order.UserName = HttpContext.User.Identity.Name; order.OrderDate = DateTime.Now; //Add the Order dbContext.Orders.Add(order); //Process the order var cart = ShoppingCart.GetCart(dbContext, HttpContext); await cart.CreateOrder(order); _logger.LogInformation("User {userName} started checkout of {orderId}.", order.UserName, order.OrderId); // Save all changes await dbContext.SaveChangesAsync(requestAborted); return(RedirectToAction("Complete", new { id = order.OrderId })); } } catch { //Invalid - redisplay with errors return(View(order)); } }