public async Task <IActionResult> Create([Bind("Id,Name,Price,Size")] Pizza pizza) { if (ModelState.IsValid) { pizza.Id = Guid.NewGuid(); _context.Add(pizza); await _context.SaveChangesAsync(); return(RedirectToAction(nameof(Index))); } return(View(pizza)); }
public async Task <IActionResult> Create(PizzaFormViewModel model) { if (!ModelState.IsValid) { return(View(model)); } var dbPizza = new Pizza { Id = Guid.NewGuid(), NameLt = model.NameLt, NameEn = model.NameEn, PriceCategoryId = model.PriceCategoryId, DescriptionLt = model.DescriptionLt, DescriptionEn = model.DescriptionEn, ImagePath = "~/uploads/img/gallery/" + model.ImagePath }; _context.Add(dbPizza); await _context.SaveChangesAsync(); return(RedirectToAction(nameof(Index))); }
public async Task <IActionResult> Create(string id, OrderCreateViewModel model) { var execRes = new ExecutionResult(); var pizza = _context.Pizzas.SingleOrDefault(i => i.NameEn == id); if (pizza == null) { execRes.AddError(_localizer["Requested pizza was not found. Please try again."]).PushTo(TempData); return(RedirectToAction(nameof(Index))); } if (!ModelState.IsValid) { model.Prices = _context.PizzaPriceInfo.Where(info => info.PriceCategoryId == pizza.PriceCategoryId); model.MinPrice = model.Prices.Min(p => p.Price); return(View(model)); } if (pizza.IsDepricated) { execRes.AddError(_localizer["Pizza no longer exists. Please try another pizza."]).PushTo(TempData); return(RedirectToAction(nameof(Index))); } var ppi = _context.PizzaPriceInfo.SingleOrDefault(g => g.Id == model.SizeId); if (ppi == null || pizza.PriceCategoryId != ppi.PriceCategoryId) { execRes.AddError(_localizer["Unexpected size was selected. Please try again."]).PushTo(TempData); return(RedirectToAction(nameof(Create), new { Id = id })); } var userId = User.GetId(); if (string.IsNullOrEmpty(userId)) { execRes.AddError(_localizer["You are logged out. Please log in to add the order."]).PushTo(TempData); return(RedirectToAction(nameof(Create), new { Id = id })); } // Check if there is an order in the basket already var order = await _context.Orders.SingleOrDefaultAsync(o => !o.IsPaid && o.PizzaId == pizza.Id && o.ClientId == userId && o.Size == ppi.Size); if (order != null) { order.Price = ppi.Price; order.Quantity += model.Quantity; order.PlacementDate = DateTime.Now; // Adds a new line to the comment with the new comment. order.CookingComment = string.IsNullOrEmpty(order.CookingComment) ? model.Comment : string.IsNullOrEmpty(model.Comment) ? order.CookingComment : $"{order.CookingComment}\n-----\n{model.Comment}"; } else { order = new Order { Id = Guid.NewGuid(), Pizza = pizza, ClientId = userId, CookingComment = model.Comment, Quantity = model.Quantity, Price = ppi.Price, Size = ppi.Size, PlacementDate = DateTime.Now }; _context.Add(order); } if (await _context.SaveChangesAsync() > 0) { execRes.AddInfo(_localizer["Pizza was succesfully added to the cart."]); } else { execRes.AddError(_localizer["Pizza could not be added to the cart. Please try again."]); } execRes.PushTo(TempData); return(RedirectToAction(nameof(Index))); }