Esempio n. 1
0
        public ActionResult Create(RecipeBindingModel model, string id)
        {
            if (!ModelState.IsValid)
            {
                return(View(nameof(Create), id));
            }
            try
            {
                var ingredients = model.Ingredients.Where(i => i.Quantity > 0)
                                  .Select(i => new IngredientDomModel
                {
                    Id       = Guid.Parse(i.Id),
                    Quantity = i.Quantity
                });
                var recipeDom = new RecipeDomModel
                {
                    Name        = model.Name,
                    Description = model.Description,
                    Ingredients = ingredients.ToList()
                };
                var recipe = this.manager.Add(recipeDom);

                return(RedirectToAction(nameof(Index)));
            }
            catch
            {
                return(View());
            }
        }
Esempio n. 2
0
        public ActionResult Edit(RecipeBindingModel model, string id)
        {
            if (!ModelState.IsValid)
            {
                ModelState.AddModelError("Error", "Error!");
                return(RedirectToAction("Edit", "Recipe", id));
            }
            try
            {
                var ingredients = model.Ingredients.Where(i => i.Quantity > 0)
                                  .Select(i => new IngredientDomModel
                {
                    Id       = Guid.Parse(i.Id),
                    Quantity = i.Quantity,
                });

                var recipeDom = new RecipeDomModel
                {
                    Id          = Guid.Parse(id),
                    Name        = model.Name,
                    Description = model.Description,
                    Ingredients = ingredients.ToList()
                };

                this.manager.Edit(recipeDom);

                return(RedirectToAction(nameof(Index)));
            }
            catch (Exception e)
            {
                ModelState.AddModelError("Error", e.Message);
                return(RedirectToAction("Edit", "Recipe", id));
            }
        }
Esempio n. 3
0
        // GET: Recipe/Create
        public ActionResult Create()
        {
            var availableIngredients = this.manager.GetAvailableIngredients();

            var model = new RecipeBindingModel(availableIngredients);

            return(View(model));
        }
Esempio n. 4
0
        private RecipeDomModel ConvertToDomModel(Guid id, RecipeBindingModel model)
        {
            var result = new RecipeDomModel
            {
                Id = id,
            };

            return(result);
        }
Esempio n. 5
0
        // GET: Recipe/Edit/5
        public ActionResult Edit(string id)
        {
            var recipe = this.manager.Find(Guid.Parse(id));
            var availableIngredients = this.manager.GetAvailableIngredients()
                                       .Where(i => !recipe.Ingredients.Select(ri => ri.Id)
                                              .Contains(i.Id)).ToList();
            //recipe.Ingredients.ToList().AddRange(availableIngredients);
            RecipeBindingModel model = new RecipeBindingModel(recipe, availableIngredients);

            return(View(model));
        }