public ActionResult Save(RecipeFormViewModel viewModel) { // 1. Save Recipe Recipe recipe = viewModel.Recipe; if (recipe.Id == 0) { _context.Recipes.Add(recipe); } recipe = _context.Recipes.Add(recipe); _context.SaveChanges(); // 2. Save Recipe Files if (viewModel.RecipeFiles.Count > 0) { List <HttpPostedFileBase> ojbImage = viewModel.RecipeFiles; foreach (var file in ojbImage) { if (file != null && file.ContentLength > 0) { RecipeFile recipeFile = new RecipeFile(); recipeFile.ImgFile = Guid.NewGuid() + Path.GetExtension(file.FileName); recipeFile.RecipeId = recipe.Id; file.SaveAs(Path.Combine(Server.MapPath("~/RecipeImageFiles"), recipeFile.ImgFile)); _context.RecipeFiles.Add(recipeFile); _context.SaveChanges(); } } } return(RedirectToAction("Index", "Recipe")); }
private static RecipeFormViewModel CreateRecipeFormViewModel(ObjectState recipeObjectState, ObjectState recipeIngredientObjectState) { var recipeFormViewModel = new RecipeFormViewModel { Id = 0, Title = "Test Recipe", FoodTypeId = 2, FoodId = 2, RecipeIngredients = new List <IngredientViewModel> { new IngredientViewModel { Id = -1, IngredientTypeId = 1, IngredientId = 2, Amount = 1, UnitOfMeasureId = 2, RecipeId = 0, ObjectState = recipeIngredientObjectState } }, ObjectState = recipeObjectState }; return(recipeFormViewModel); }
public ActionResult DeleteIngredient(int id, string userId, int recipeId) { if (User.Identity.GetUserId() != userId) { return(RedirectToAction("Index", "Recipes")); } var ingredientInDb = _context.Ingredients.SingleOrDefault(r => r.Id == id); if (ingredientInDb == null) { return(HttpNotFound()); } _context.Ingredients.Remove(ingredientInDb); _context.SaveChanges(); var recipe = _context.Recipes.SingleOrDefault(r => r.Id == recipeId); var viewModel = new RecipeFormViewModel { Recipe = recipe, Difficulties = _context.Difficulties.ToList(), Cuisines = _context.Cuisines.ToList(), Ingredient = new Ingredient(), Ingredients = _context.Ingredients .Where(r => r.RecipeId == recipeId) .ToList() }; return(View("RecipeForm", viewModel)); }
public ActionResult Create(RecipeFormViewModel viewModel) { if (!ModelState.IsValid) { viewModel.Meals = _context.Meals.ToList(); return(View("RecipeForm", viewModel)); } var recipe = new Recipe { ChefId = User.Identity.GetUserId(), DateAdded = DateTime.Now, Name = viewModel.Name, Time = viewModel.GetDateTime(), MealId = viewModel.Meal, Description = viewModel.Description, Ingredient = viewModel.Ingredient, Serving = viewModel.Serving, PreparationInstruction = viewModel.PreparationInstruction }; _context.Recipes.Add(recipe); _context.SaveChanges(); return(RedirectToAction("List", "Recipes")); }
private static RecipeFormViewModel CreateRecipeFormViewModelWithRecipe(Recipe recipe, ObjectState recipeObjectState, ObjectState recipeIngredientObjectState) { var recipeFormViewModel = new RecipeFormViewModel { Id = recipe.Id, Title = recipe.Title, FoodTypeId = recipe.FoodTypeId, FoodId = recipe.FoodId, RecipeIngredients = new List <IngredientViewModel> { new IngredientViewModel { Id = recipe.RecipeIngredients[0].Id, IngredientTypeId = recipe.RecipeIngredients[0].IngredientTypeId, IngredientId = recipe.RecipeIngredients[0].IngredientId, Amount = recipe.RecipeIngredients[0].Amount, UnitOfMeasureId = recipe.RecipeIngredients[0].UnitOfMeasureId, RecipeId = recipe.Id, ObjectState = recipeIngredientObjectState } }, ObjectState = recipeObjectState }; return(recipeFormViewModel); }
//currently unused: add a actionlink linked to this method and a recipeEdit page // Edit existing recipe public ActionResult Edit(int id) { var loggedInUser = User.Identity.GetUserName(); var recipe = _context.Recipes.SingleOrDefault(c => c.Id == id); if (loggedInUser != recipe.Username || loggedInUser == null) { ViewBag.Message = "You don't have permissions to edit this recipe!"; return(View("PermissionsError", recipe)); } if (recipe == null) { return(HttpNotFound()); } var viewModel = new RecipeFormViewModel(recipe) { RecipeTypes = _context.RecipeTypes.ToList() }; return(View("RecipeForm", viewModel)); }
public ActionResult Save(Recipe recipe) { if (!ModelState.IsValid) { var viewModel = new RecipeFormViewModel(); return(View("RecipeForm", viewModel)); } if (recipe.Id == 0) { recipe.UserId = HttpContext.User.Identity.GetUserId(); recipe.UserName = HttpContext.User.Identity.GetUserName(); recipe.TotalViews = 0; _context.Recipes.Add(recipe); } else { var recipeInDb = _context.Recipes.Single(r => r.Id == recipe.Id); recipeInDb.Name = recipe.Name; recipeInDb.Description = recipe.Description; recipeInDb.Ingredients = recipe.Ingredients; } _context.SaveChanges(); return(RedirectToAction("Index", "Recipes")); }
public ActionResult Edit(int id) { var recipe = _context.Recipes.SingleOrDefault(r => r.Id == id); if (recipe == null) { return(HttpNotFound()); } if (User.Identity.GetUserId() != recipe.AspNetUserId) { return(RedirectToAction("Index", "Recipes")); } var viewModel = new RecipeFormViewModel { Recipe = recipe, Difficulties = _context.Difficulties.ToList(), Cuisines = _context.Cuisines.ToList(), Ingredient = new Ingredient(), Ingredients = _context.Ingredients .Where(r => r.RecipeId == id) .ToList() }; return(View("RecipeForm", viewModel)); }
public ActionResult Save(RecipeFormViewModel viewModel) { if (!ModelState.IsValid) { // The form is not valid --> return same form to the user return(View("Edit", viewModel)); } // 1. Save Recipe Recipe recipe = viewModel.Recipe; if (recipe.Id == 0) { _context.Recipes.Add(recipe); } else { var recipeInDB = _context.Recipes.Single(r => r.Id == viewModel.Recipe.Id); /* * This method has a security flow. * TryUpdateModel(customerInDB); */ // Manually update the fields I want. recipeInDB.Title = viewModel.Recipe.Title; recipeInDB.Ingredients = viewModel.Recipe.Ingredients; recipeInDB.Contents = viewModel.Recipe.Contents; recipeInDB.ModifiedDate = DateTime.Today; } _context.SaveChanges(); // 2. Save Recipe Files if (viewModel.RecipeFiles.Count > 0) { List <HttpPostedFileBase> ojbImage = viewModel.RecipeFiles; foreach (var file in ojbImage) { if (file != null && file.ContentLength > 0) { RecipeFile recipeFile = new RecipeFile(); recipeFile.ImgFile = Guid.NewGuid() + Path.GetExtension(file.FileName); recipeFile.RecipeId = recipe.Id; file.SaveAs(Path.Combine(Server.MapPath("~/RecipeImageFiles"), recipeFile.ImgFile)); _context.RecipeFiles.Add(recipeFile); _context.SaveChanges(); } } } return(RedirectToAction("Index", "Recipe")); }
public ActionResult New() { var viewModel = new RecipeFormViewModel { Recipe = new Recipe() }; return(View("RecipeForm", viewModel)); }
// New Recipe public ViewResult New() { var recipeTypes = _context.RecipeTypes.ToList(); var viewModel = new RecipeFormViewModel { RecipeTypes = recipeTypes }; return(View("RecipeForm", viewModel)); }
public ActionResult New() { var viewModel = new RecipeFormViewModel { Meals = _context.Meals.ToList(), Heading = "Dodaj recept" }; return(View("RecipeForm", viewModel)); }
public ActionResult New() { var dishTypes = _context.DishTypes.ToList(); var viewModel = new RecipeFormViewModel { Recipe = new Recipe(), DishTypes = dishTypes }; return(View("RecipesForm", viewModel)); }
public ActionResult Detail(int id) { RecipeFormViewModel recipe = new RecipeFormViewModel { Recipe = _ctx.Recipe.FirstOrDefault(r => r.Id == id), IngredientsRecipe = _ctx.IngredientRecipe.Include(i => i.IngredientIndex).Where(i => i.RecipeId == id) }; ViewBag.MessageSuccess = TempData["MessageSuccess"]; return(View(recipe)); }
// GET: WriteRecipe public ActionResult New() { var viewModel = new RecipeFormViewModel() { Recipe = new Recipe() }; viewModel.Recipe.Email = "*****@*****.**"; viewModel.Recipe.CreateDate = DateTime.Today; viewModel.Recipe.ModifiedDate = DateTime.Today; return(View(viewModel)); }
public ActionResult New() { List <IngredientRecipe> ingRecipe = new List <IngredientRecipe>(); ingRecipe.Add(new IngredientRecipe()); RecipeFormViewModel recipeFVM = new RecipeFormViewModel() { Recipe = new Recipe(), IngredientsRecipe = ingRecipe }; return(View("RecipeForm", recipeFVM)); }
public ActionResult New(string s) { var difficulties = _context.Difficulties.ToList(); var cuisines = _context.Cuisines.ToList(); var viewModel = new RecipeFormViewModel { Difficulties = difficulties, Cuisines = cuisines, Ingredient = new Ingredient() }; return(View("RecipeForm", viewModel)); }
public ActionResult Create() { var viewModel = new RecipeFormViewModel { Title = "", ContextFoodTypes = _unitOfWork.FoodTypes.GetFoodTypes(), ContextFoods = _unitOfWork.Foods.GetFoods(), ContextIngredientTypes = _unitOfWork.IngredientTypes.GetIngredientTypes(), ContextIngredients = _unitOfWork.Ingredients.GetIngredients(), ContextUnitOfMeasures = _unitOfWork.UnitOfMeasures.GetUnitOfMeasures(), Heading = "New Recipe", ObjectState = ObjectState.Added }; return(View("Recipe", viewModel)); }
public ActionResult Edit(int id) { Recipe recipe = _ctx.Recipe.FirstOrDefault(m => m.Id == id); RecipeFormViewModel recipeFVM = new RecipeFormViewModel() { Recipe = recipe, IngredientsRecipe = _ctx.IngredientRecipe .Include(m => m.IngredientIndex) .Where(m => m.RecipeId == recipe.Id) .ToList() }; return(View("RecipeForm", recipeFVM)); }
public ActionResult Edit(int?Id) { if (Id == null) { return(new HttpStatusCodeResult(HttpStatusCode.BadRequest)); } var userId = User.Identity.GetUserId(); var recipe = _unitOfWork.Recipes.GetRecipe(Id, userId); if (recipe == null) { return(new HttpNotFoundResult("Recipe not found")); } var viewModel = new RecipeFormViewModel { Id = recipe.Id, Title = recipe.Title, ContextFoodTypes = _unitOfWork.FoodTypes.GetFoodTypes(), ContextFoods = _unitOfWork.Foods.GetFoods().ToList(), ContextIngredientTypes = _unitOfWork.IngredientTypes.GetIngredientTypes(), ContextIngredients = _unitOfWork.Ingredients.GetIngredients(), ContextUnitOfMeasures = _unitOfWork.UnitOfMeasures.GetUnitOfMeasures(), FoodTypeId = recipe.FoodTypeId, FoodId = recipe.FoodId, FoodName = recipe.Food.FoodName, Heading = "Edit Recipe", ObjectState = ObjectState.Unchanged }; foreach (var ing in recipe.RecipeIngredients) { var ingVM = new IngredientViewModel { Id = ing.Id, RecipeId = ing.RecipeId, Amount = ing.Amount, IngredientId = ing.IngredientId, IngredientTypeId = ing.IngredientTypeId, UnitOfMeasureId = ing.UnitOfMeasureId, ObjectState = ObjectState.Unchanged }; viewModel.RecipeIngredients.Add(ingVM); } return(View("Recipe", viewModel)); }
// GET: ReadRecipe public ActionResult Index(int id) { // Get the specified recipe var recipeInDB = _context.Recipes.SingleOrDefault(r => r.Id == id); if (recipeInDB == null) { return(HttpNotFound()); } var viewModel = new RecipeFormViewModel { Recipe = recipeInDB, //RecipeFiles = _context.RecipeFiles.ToList() // Get the image of recipe }; return(View(viewModel)); }
public ActionResult Update(RecipeFormViewModel viewModel) { if (!ModelState.IsValid) { viewModel.Meals = _context.Meals.ToList(); return(View("RecipeForm", viewModel)); } var userId = User.Identity.GetUserId(); var recipe = _context.Recipes .Single(r => r.Id == viewModel.Id && r.ChefId == userId); recipe.Modify(viewModel.GetDateTime(), viewModel.Name, viewModel.Meal, viewModel.Description, viewModel.Ingredient, viewModel.Serving, viewModel.PreparationInstruction); _context.SaveChanges(); return(RedirectToAction("List", "Recipes")); }
public ActionResult Save(Recipe recipe) { if (!ModelState.IsValid) { var viewModel = new RecipeFormViewModel(recipe) { RecipeTypes = _context.RecipeTypes.ToList() }; return(View("RecipeForm", viewModel)); } if (recipe.Id == 0) //doesn't already exist in DB { if (User.Identity.GetUserId() == null || User.Identity.GetUserName() == "") { recipe.Username = "******"; } else //if the current user is not null { recipe.Username = User.Identity.GetUserName(); } _context.Recipes.Add(recipe); } else //Something already exists with the recipe Id. { var recipeInDb = _context.Recipes.Single(c => c.Id == recipe.Id); recipeInDb.Name = recipe.Name; recipeInDb.RecipeTypeId = recipe.RecipeTypeId; recipeInDb.PrepTime = recipe.PrepTime; recipeInDb.CookTime = recipe.CookTime; recipeInDb.Description = recipe.Description; recipeInDb.Directions = recipe.Directions; recipeInDb.Ingredients = recipe.Ingredients; recipeInDb.Username = recipe.Username; } _context.SaveChanges(); return(RedirectToAction("Index", "Community")); }
public ActionResult Edit(int id) { //El resultado de esta asignacion a var recipe, devuelve el primer elemento de la lista que coincida con el valor pasado por parametro //Usaremos LINQ con una expresion LAMBDA para obtener dicho elemento //Esto ( r => r.id == id), que se lee como "r tal que r.id == id", buscara el primer elemento dentro de la lista, que coincida con id //Estas mismas expresiones las usaremos para hacer peticiones a la base de datos var recipe = _context.Recipes.SingleOrDefault(r => r.Id == id); if (recipe == null) { return(HttpNotFound()); } var viewModel = new RecipeFormViewModel { Recipe = recipe }; return(View("RecipeForm", viewModel)); }
public ActionResult Edit(int id) { var userId = User.Identity.GetUserId(); var recipe = _context.Recipes.Single(r => r.Id == id && r.ChefId == userId); var viewModel = new RecipeFormViewModel { Meals = _context.Meals.ToList(), Id = recipe.Id, Name = recipe.Name, Time = recipe.DateAdded.ToString("HH:mm"), Meal = recipe.MealId, Description = recipe.Description, Ingredient = recipe.Ingredient, Serving = recipe.Serving, PreparationInstruction = recipe.PreparationInstruction, Heading = "Uredi Recept" }; return(View("RecipeForm", viewModel)); }
public ViewResult Edit(RecipeItem recipe) { RecipeFormViewModel recipeData = new RecipeFormViewModel(); recipeData.Recipe = repository.Recipes.Where(r => r.RecipeId == recipe.RecipeId).FirstOrDefault <RecipeItem>(); recipeData.Equipment = repository.Equipment.Where(r => r.EquipmentId == recipe.RecipeId).FirstOrDefault <Equipment>(); recipeData.Ingredient = repository.Ingredients.Where(r => r.IngredientId == recipe.RecipeId).FirstOrDefault <Ingredient>(); recipeData.Review = repository.Reviews.Where(r => r.ReviewId == recipe.RecipeId).FirstOrDefault <Review>(); RecipeFormViewModelData recipeDataOutput = new RecipeFormViewModelData(); recipeDataOutput.RecipeId = recipeData.Recipe.RecipeId; recipeDataOutput.RecipeName = recipeData?.Recipe?.RecipeName; recipeDataOutput.ServingSize = recipeData.Recipe.ServingSize; recipeDataOutput.Description = recipeData?.Recipe?.Description; recipeDataOutput.Ingredient = recipeData?.Ingredient?.IngredientName; recipeDataOutput.Equipment = recipeData?.Equipment?.EquipmentName; recipeDataOutput.Review = recipeData?.Review?.ReviewText; recipeDataOutput.Instructions = recipeData?.Recipe?.Instructions; return(View(recipeDataOutput)); }
public ActionResult AddOrUpdate(RecipeFormViewModel recipeFVM) { if (!ModelState.IsValid) { ViewBag.MessageError = "Il y a des erreurs dans le formulaire"; return(View("RecipeForm", recipeFVM)); } int recipeId = 0; if (recipeFVM.Recipe == null) { ViewBag.MessageError = "Il n'y a aucune recette à ajouter"; } //// on est en train d'éditer la recette, on récupère les ingrédients associés //if (recipeFVM.IngredientsRecipe == null) // recipeFVM.IngredientsRecipe = _ctx.IngredientRecipe.Where(m => m.Recipe.Id == recipeFVM.Recipe.Id).ToList(); if (recipeFVM.Recipe.Id > 0) { // Edit recipe Recipe recipeFromDb = _ctx.Recipe.FirstOrDefault(m => m.Id == recipeFVM.Recipe.Id); if (recipeFromDb == null) { return(HttpNotFound()); } recipeFromDb.Instructions = recipeFVM.Recipe.Instructions; recipeFromDb.CookingTime = recipeFVM.Recipe.CookingTime; recipeFromDb.Level = recipeFVM.Recipe.Level; recipeFromDb.Name = recipeFVM.Recipe.Name; recipeFromDb.NbServing = recipeFVM.Recipe.NbServing; recipeFromDb.PreparationTime = recipeFVM.Recipe.PreparationTime; _ctx.SaveChanges(); recipeId = recipeFromDb.Id; TempData["MessageSuccess"] = "La recette a bien été modifiée."; } else { // Add recipe Recipe newRecipe = new Recipe() { Instructions = recipeFVM.Recipe.Instructions, CookingTime = recipeFVM.Recipe.CookingTime, Level = recipeFVM.Recipe.Level, Name = recipeFVM.Recipe.Name, NbServing = recipeFVM.Recipe.NbServing, PreparationTime = recipeFVM.Recipe.PreparationTime }; _ctx.Recipe.Add(newRecipe); _ctx.SaveChanges(); recipeId = newRecipe.Id; TempData["MessageSuccess"] = "La recette a bien été ajoutée."; } return(RedirectToAction("Detail", new { id = recipeId })); }
public ActionResult Save(RecipeFormViewModel recipeFormViewModel) { if (!ModelState.IsValid) { recipeFormViewModel.ContextFoodTypes = _unitOfWork.FoodTypes.GetFoodTypes(); recipeFormViewModel.ContextFoods = _unitOfWork.Foods.GetFoods(); recipeFormViewModel.ContextIngredientTypes = _unitOfWork.IngredientTypes.GetIngredientTypes(); recipeFormViewModel.ContextIngredients = _unitOfWork.Ingredients.GetIngredients(); recipeFormViewModel.ContextUnitOfMeasures = _unitOfWork.UnitOfMeasures.GetUnitOfMeasures(); return(View("Recipe", recipeFormViewModel)); } var userId = User.Identity.GetUserId(); Recipe recipe = new Recipe { Id = recipeFormViewModel.Id, CookApplicationUserId = userId, Title = recipeFormViewModel.Title, FoodTypeId = recipeFormViewModel.FoodTypeId, FoodId = recipeFormViewModel.FoodId, RecipeIngredients = new List <RecipeIngredients>(), ObjectState = recipeFormViewModel.ObjectState }; // A negative Id value for added (New) ingredients var ingredientId = -1; foreach (var ing in recipeFormViewModel.RecipeIngredients) { var recipeIngredient = new RecipeIngredients { RecipeId = recipeFormViewModel.Id, Id = ing.ObjectState == ObjectState.Added ? ingredientId : ing.Id, IngredientTypeId = ing.IngredientTypeId, IngredientId = ing.IngredientId, UnitOfMeasureId = ing.UnitOfMeasureId, Amount = ing.Amount, ObjectState = ing.ObjectState }; recipe.RecipeIngredients.Add(recipeIngredient); ingredientId--; } _unitOfWork.Recipes.Attach(recipe); foreach (int riToDelete in recipeFormViewModel.RecipeIngredientsToDelete) { var recipeIngredient = _unitOfWork.RecipeIngredients.GetRecipeIngredient(riToDelete); if (recipeIngredient != null) { recipeIngredient.ObjectState = ObjectState.Deleted; } } _unitOfWork.ApplyStateChanges(); _unitOfWork.Complete(); switch (recipeFormViewModel.ObjectState) { case ObjectState.Added: //TODO: Add a message to user for added recipes break; case ObjectState.Modified: //TODO: Add a message to user for modified recipes break; } // If we decide in the future to stay in Edit view, then we'll restore the viewModel to show changes in the dataBase. // Like new Ids for added records and we'll also need to return ObjectState to Unchange. recipeFormViewModel.ObjectState = ObjectState.Unchanged; recipeFormViewModel.Id = recipe.Id; recipeFormViewModel.ContextFoodTypes = _unitOfWork.FoodTypes.GetFoodTypes(); recipeFormViewModel.ContextFoods = _unitOfWork.Foods.GetFoods(); recipeFormViewModel.ContextIngredientTypes = _unitOfWork.IngredientTypes.GetIngredientTypes(); recipeFormViewModel.ContextIngredients = _unitOfWork.Ingredients.GetIngredients(); recipeFormViewModel.ContextUnitOfMeasures = _unitOfWork.UnitOfMeasures.GetUnitOfMeasures(); recipeFormViewModel.RecipeIngredients = new List <IngredientViewModel>(); foreach (var ing in recipe.RecipeIngredients) { var ingVM = new IngredientViewModel { Id = ing.Id, RecipeId = ing.RecipeId, Amount = ing.Amount, IngredientId = ing.IngredientId, IngredientTypeId = ing.IngredientTypeId, UnitOfMeasureId = ing.UnitOfMeasureId, ObjectState = ObjectState.Unchanged }; recipeFormViewModel.RecipeIngredients.Add(ingVM); } var redirectedToPage = "/Home/Index"; return(Json(new { homePage = redirectedToPage, vm = recipeFormViewModel })); }