Ejemplo n.º 1
0
        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"));
        }
Ejemplo n.º 2
0
        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"));
        }