Example #1
0
        public async Task <IActionResult> Edit(int productId)
        {
            RecipeEditModel recipe = await this.recipes.GetRecipeToEdit(productId);

            if (recipe == null)
            {
                return(this.NotFound());
            }

            return(View(recipe));
        }
        public IActionResult Edit(RecipeEditModel model)
        {
            var recipe = this.recipesService
                         .GetById(model.Id);

            if (recipe == null || recipe.Author != this.User.Identity.Name)
            {
                return(this.NotFound());
            }

            this.recipesService.Edit(model);

            this.AddAlertSuccess("The Recipe was successfully eddited.");
            return(Redirect("/Recipes/Details/" + model.Id));
        }
Example #3
0
        public IActionResult Create(RecipeEditModel model)
        {
            if (ModelState.IsValid)
            {
                var newRecipe = new Recipe();
                newRecipe.Name      = model.Name;
                newRecipe.Timeofday = model.Timeofday;
                newRecipe           = _recipeData.Add(newRecipe);

                //return View("Details", newRecipe);
                return(RedirectToAction(nameof(Details), new { id = newRecipe.Id }));
            }
            else
            {
                return(View());
            }
        }
Example #4
0
        public async Task <IActionResult> Edit(RecipeEditModel recModel)
        {
            if (!ModelState.IsValid)
            {
                return(this.View(recModel));
            }

            int recipeId = await this.recipes.AddRecipeIngredient(recModel.RecipeId, recModel.ProductId, recModel.IngredientId, recModel.Quantity);

            if (recipeId == 0)
            {
                return(this.NotFound());
            }

            recModel = await this.recipes.GetRecipeToEdit(recModel.ProductId);

            recModel.RecipeId = recipeId;

            return(this.View(recModel));
        }
        public async Task <RecipeEditModel> GetRecipeToEdit(int productId)
        {
            Product product = await this.db.Products.FindAsync(productId);

            if (product == null)
            {
                return(null);
            }

            RecipeEditModel recipe = new RecipeEditModel()
            {
                RecipeId    = product.RecipeId ?? 0,
                ProductId   = product.Id,
                ProductName = product.Name,
                Ingredients = await this.db.Ingredients
                              .ProjectTo <IngredientDropDownListModel>()
                              .ToListAsync()
            };

            return(recipe);
        }
Example #6
0
 public Task <IActionResult> Update([FromRoute] Guid id, [FromBody] RecipeEditModel model) =>
 _bus.Publish(new RecipeUpdateCommand(id, model.Name, model.Description)).ToAcceptedAtResult();
Example #7
0
 public Task <IActionResult> Create([FromBody] RecipeEditModel model) => _bus
 .Publish(new RecipeCreateCommand(model.Name, model.Description))
 .ToCreatedAtResult();
        public void Edit(RecipeEditModel model)
        {
            var course = this.dbContext
                .Courses
                .FirstOrDefault(c => c.Name == model.Course);

            var cat = this.dbContext
                .Categories
                .FirstOrDefault(c => c.Name == model.Category);

            var recipe = this.dbContext
                .Recipes
                .Find(model.Id);

            recipe.Name = model.Name;
            recipe.Description = model.Description;
            recipe.Preparation = model.Preparation;
            recipe.Servings = model.Servings;
            recipe.PrepTime = model.PrepTime;
            recipe.CookingTime = model.CookingTime;
            recipe.ImageUrl = model.ImageUrl;
            recipe.Course = course;
            recipe.Category = cat;

            var ingredients = new List<RecipeIngredient>();

            if(model.Ingredients != null)
            {
                foreach (var ingr in model.Ingredients)
                {
                    var isAlreadyAdded = this.dbContext.RecipesIngredients
                        .Any(i => i.Ingredient.Name == ingr.Name && 
                        i.Recipe.Id == recipe.Id);

                    if (!ingr.IsDeleted && !isAlreadyAdded)
                    {
                        this.AddIngredient(ingr, ingredients, recipe);
                    }
                    else if(ingr.IsDeleted)
                    {
                        var removed = dbContext
                            .RecipesIngredients
                            .FirstOrDefault(r => r.Ingredient.Name == ingr.Name &&
                            r.RecipeId == model.Id);

                        this.dbContext.RecipesIngredients.Remove(removed);
                        this.dbContext.SaveChanges();
                    }
                }
            }
            
            foreach(var ingr in model.AddedIngredients)
            {
                if (!ingr.IsDeleted)
                {
                    this.AddIngredient(ingr, ingredients, recipe);
                }
            }


            this.dbContext.RecipesIngredients.AddRange(ingredients);
            //recipe.Ingredients = ingredients;
            this.dbContext.SaveChanges();
        }