Beispiel #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());
            }
        }
Beispiel #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));
            }
        }
Beispiel #3
0
        public RecipeBindingModel(RecipeDomModel model, List <IngredientDomModel> availableIngredients)
            : this()
        {
            this.Name        = model.Name;
            this.Description = model.Description;
            //model.Ingredients.Concat(availableIngredients);

            this.Ingredients = model.Ingredients.Concat(availableIngredients).Select(i => new RecipeIngredientBindingModel(i)).ToList();;
        }
Beispiel #4
0
        private RecipeDomModel ConvertToDomModel(Guid id, RecipeBindingModel model)
        {
            var result = new RecipeDomModel
            {
                Id = id,
            };

            return(result);
        }
Beispiel #5
0
 public RecipeViewModel(RecipeDomModel model)
 {
     this.Id          = model.Id.ToString();
     this.Name        = model.Name;
     this.Description = model.Description;
     if (model.Ingredients != null)
     {
         this.Ingredients = model.Ingredients.Select(i => new RecipeIngredientViewModel(i)).ToList();
     }
 }