public ActionResult UploadRecipe(Recipe recipe, FormCollection formCollection) { string message = ""; bool ingredientsAdded = false; using (DBEntities de = new DBEntities()) { User user = de.Users.Where(a => a.EmailID == HttpContext.User.Identity.Name).FirstOrDefault(); if (user != null) { if (user.IsEmailVerified != true) { ViewBag.Message = "You cannot upload recipes until you verify your account!"; return(View()); } else { var userRoles = user.UsersRoles; foreach (var userRole in userRoles) { if (userRole.Role.RoleName.Equals("Admin")) { if (userRole.UserID == user.UserID) { bool canUpload = false; if (ModelState.IsValid) { if (recipe != null) { if (de.Recipes.Count() < 1) { canUpload = true; } else { foreach (var recp in de.Recipes) { if (recp.RecipeName.Equals(recipe.RecipeName)) { canUpload = false; break; } else { canUpload = true; } } } if (canUpload == true) { de.Recipes.Add(recipe); de.SaveChanges(); var recp = de.Recipes.Where(a => a.RecipeName.Equals(recipe.RecipeName)).FirstOrDefault(); if (recp != null) { if (formCollection != null) { foreach (var checkbox in formCollection) { if (checkbox != null) { string isChecked = formCollection[checkbox.ToString()]; if (isChecked == "true,false") // This means the checkbox is selected, "false" means it's not selected. { Ingredient ingr = de.Ingredients.Where(a => a.IngredientName.Equals(checkbox.ToString())).FirstOrDefault(); if (ingr != null) { RecipesIngredient recpIngr = new RecipesIngredient(); recpIngr.Recipe = recp; recpIngr.Ingredient = ingr; recp.RecipesIngredients.Add(recpIngr); de.SaveChanges(); message = "Recipe uploaded successfully!"; ingredientsAdded = true; } } } } if (ingredientsAdded == false) { message = "You must select at least one ingredient!"; de.Recipes.Remove(recipe); de.SaveChanges(); } } } } else { message = "This recipe is already present in the database!"; } } } else { message = "Invalid Request!"; } } } } } ViewBag.Message = message; return(View(recipe)); } } return(RedirectToAction("AccessDenied", "Admin")); }
public ActionResult CreateOrderWithCustomPizza(int id, FormCollection formCollection) { string message = ""; int savedID = id; bool ingredientsSelected = false; using (DBEntities de = new DBEntities()) { User currentUser = de.Users.Where(a => a.EmailID == HttpContext.User.Identity.Name).FirstOrDefault(); if (currentUser.IsEmailVerified != true) { message = "You cannot order a custom pizza until you verify your account!"; } else { if (formCollection["CustomPizzaSizesList"] == "") { TempData["customOrderInfo"] = "Pizza size is required!"; return(RedirectToAction("CreateCustomPizza", "Home", new { id = savedID })); } var pizza = de.Pizzas.Where(a => a.PizzaID == id).FirstOrDefault(); Recipe recipe = new Recipe(); Pizza customPizza = new Pizza(); de.SaveChanges(); string pizzaName = "Custom Pizza #" + pizza.PizzaID + " | User #" + currentUser.UserID + " | Date: " + DateTime.Now.ToString() + " | " + pizza.PizzaName; string recipeName = "Custom Recipe #" + recipe.RecipeID + " | User #" + currentUser.UserID + " | Date: " + DateTime.Now.ToString() + " | " + pizza.Recipe.RecipeName; double ingredientsPrice = 0.0; double pizzaPrice = 0.0; string pizzaSize = ""; string imagePath = pizza.PizzaPicturePath; recipe.RecipeName = recipeName; de.Recipes.Add(recipe); de.SaveChanges(); if (formCollection != null) { pizzaSize = formCollection["CustomPizzaSizesList"]; } foreach (var checkbox in formCollection) { foreach (var ingredient in de.Ingredients) { if (checkbox != null) { string isChecked = formCollection[checkbox.ToString()]; if (isChecked == "true,false") // This means the checkbox is selected, "false" means it's not selected. { Ingredient ingr = de.Ingredients.Where(a => a.IngredientName.Equals(checkbox.ToString())).FirstOrDefault(); if (ingr != null) { RecipesIngredient recpIngr = new RecipesIngredient(); recpIngr.Recipe = recipe; recpIngr.Ingredient = ingr; recipe.RecipesIngredients.Add(recpIngr); ingredientsSelected = true; break; } } } } } if (ingredientsSelected == false) { TempData["customOrderInfo"] = "Pizza ingredients are required!"; return(RedirectToAction("CreateCustomPizza", "Home", new { id = savedID })); } foreach (var ingr in recipe.RecipesIngredients) { if (ingr != null) { ingredientsPrice += ingr.Ingredient.IngredientPrice; } } // Pizza price = Ingredients Price + 50% of ingredients price (overcharge, salaries) + size increase. if (pizzaSize.Equals("Small")) { pizzaPrice = (ingredientsPrice + 0.5 * ingredientsPrice); } else if (pizzaSize.Equals("Medium")) { pizzaPrice = (ingredientsPrice + 0.5 * ingredientsPrice) + (ingredientsPrice + 0.5 * ingredientsPrice) * 0.2; } else if (pizzaSize.Equals("Large")) { pizzaPrice = (ingredientsPrice + 0.5 * ingredientsPrice) + (ingredientsPrice + 0.5 * ingredientsPrice) * 0.4; } customPizza.PizzaName = pizzaName; customPizza.Recipe = recipe; customPizza.PizzaPrice = pizzaPrice; customPizza.PizzaSize = pizzaSize; customPizza.PizzaPicturePath = imagePath; de.Pizzas.Add(customPizza); de.SaveChanges(); Order order = new Order(); order.Pizza = customPizza; order.PizzaAmount = 1; order.User = currentUser; de.Orders.Add(order); de.SaveChanges(); message = "Your custom pizza has been added to cart!"; } } TempData["customOrderInfo"] = message; return(RedirectToAction("CreateCustomPizza", "Home", new { id = savedID })); }