コード例 #1
0
 public ActionResult Edit(RecipeInfoViewModel viewModel)
 {
     viewModel.Ingredients = viewModel.Ingredients.Where(i => i.IngredientId > 0).ToList();
     if (!ModelState.IsValid)
     {
         return(View(viewModel));
     }
     if (viewModel.RecipeId > 0)
     {
         RecipeService.UpdateRecipe(viewModel);
     }
     else
     {
         RecipeService.AddRecipe(viewModel);
     }
     return(Redirect("/Recipe?RecipeId=" + viewModel.RecipeId));
 }
コード例 #2
0
        public IActionResult Index(RecipeInfoViewModel recipeInfoViewModel)
        {
            if (ModelState.IsValid)
            {
                var recipeInfo = new RecipeInfo()
                {
                    Title       = recipeInfoViewModel.RecipeInfo.Title,
                    Description = recipeInfoViewModel.RecipeInfo.Description,
                    ImageUrl    = recipeInfoViewModel.RecipeInfo.ImageUrl
                };
                _recipeInfoRepository.AddRecipeInfo(recipeInfo);

                if (recipeInfoViewModel.RecipeList == null || recipeInfoViewModel.RecipeList.Count < 1)
                {
                    return(RedirectToAction("RecipeInfoSaved"));
                }

                var recipeList = new List <Recipe>();
                foreach (var r in recipeInfoViewModel.RecipeList)
                {
                    var recipe = new Recipe()
                    {
                        Name             = r.Name,
                        LongDescription  = r.LongDescription,
                        ShortDescription = r.ShortDescription,
                        PiecesOfJobs     = r.PiecesOfJobs,
                    };
                    recipeList.Add(recipe);
                }

                _recipeRepository.AddRecipeList(recipeList);

                return(RedirectToAction("RecipeInfoSaved"));
            }
            else
            {
                return(View(recipeInfoViewModel));
            }
        }
コード例 #3
0
        public void AddRecipe(RecipeInfoViewModel viewModel)
        {
            // Add recipe entity
            var model = new Recipe
            {
                Name        = viewModel.Name,
                Description = viewModel.Description,
                Directions  = viewModel.Directions,
                CreatedDate = DateTime.Now
            };

            BaseRepository.AddOrUpdateEntity(model);
            viewModel.RecipeId = model.RecipeId;

            // Add ingredient mappings
            foreach (var ingredient in viewModel.Ingredients)
            {
                if (ingredient.IngredientId == 0)
                {
                    var ing = BaseRepository.GetEntityList <Ingredient>(new List <ICriterion>
                    {
                        Restrictions.Eq(Ingredient.COL_NAME, ingredient.Name)
                    }).FirstOrDefault();
                    if (ing == null)
                    {
                        ing = new Ingredient
                        {
                            Name = ingredient.Name
                        };
                        BaseRepository.AddOrUpdateEntity(ing);
                    }
                    BaseRepository.AddEntity(new RecipeIngredientMapping
                    {
                        IngredientId = ing.IngredientId.Value,
                        RecipeId     = viewModel.RecipeId.Value,
                        Unit         = ingredient.Unit,
                        Quantity     = ingredient.Quantity
                    });
                }
                else
                {
                    BaseRepository.AddEntity(new RecipeIngredientMapping
                    {
                        IngredientId = ingredient.IngredientId,
                        RecipeId     = viewModel.RecipeId.Value,
                        Unit         = ingredient.Unit,
                        Quantity     = ingredient.Quantity
                    });
                }
            }

            // Add tag mappings
            if (viewModel.TagIds != null)
            {
                foreach (var id in viewModel.TagIds)
                {
                    var mapping = new RecipeTagMapping
                    {
                        TagId    = id,
                        RecipeId = viewModel.RecipeId.Value
                    };
                    BaseRepository.AddEntity(mapping);
                    viewModel.Tags.Add(new TagInfo
                    {
                        TagId       = id,
                        Name        = mapping.Tag.Name,
                        Description = mapping.Tag.Name
                    });
                }
            }

            // Add tag mappings
            foreach (var tag in viewModel.Tags)
            {
                if (tag.TagId == 0)
                {
                    var t = BaseRepository.GetEntityList <Tag>(new List <ICriterion>
                    {
                        Restrictions.Eq(Tag.COL_NAME, tag.Name)
                    }).FirstOrDefault();
                    if (t == null)
                    {
                        t = new Tag
                        {
                            Name = tag.Name
                        };
                        BaseRepository.AddOrUpdateEntity(t);
                    }
                    BaseRepository.AddEntity(new RecipeTagMapping
                    {
                        TagId    = t.TagId.Value,
                        RecipeId = viewModel.RecipeId.Value
                    });
                }
                else
                {
                    BaseRepository.AddEntity(new RecipeTagMapping
                    {
                        TagId    = tag.TagId,
                        RecipeId = viewModel.RecipeId.Value
                    });
                }
            }

            // Add images
            foreach (var image in viewModel.Images)
            {
                BaseRepository.AddEntity(new Image
                {
                    Url         = image.Url,
                    RecipeId    = viewModel.RecipeId,
                    Description = image.Description,
                    CreatedDate = DateTime.Now
                });
            }
        }
コード例 #4
0
        public void UpdateRecipe(RecipeInfoViewModel viewModel)
        {
            var model = BaseRepository.GetEntityById <Recipe>(viewModel.RecipeId);

            if (model == null)
            {
                throw new RecipeNotFoundException();
            }
            model.Name        = viewModel.Name;
            model.Description = viewModel.Description;
            model.Directions  = viewModel.Directions;
            // Update recipe entity
            BaseRepository.UpdateEntity(model);

            // Update ingredient mapping
            var oldIngredients = BaseRepository.GetEntityList <RecipeIngredientMapping>(new List <ICriterion>
            {
                Restrictions.Eq(RecipeIngredientMapping.COL_RECIPEID, viewModel.RecipeId)
            });
            var oldIngredientIds = oldIngredients.Select(i => i.IngredientId);
            var newIngredientIds = viewModel.Ingredients.Select(i => i.IngredientId);

            oldIngredientIds.Where(i => !newIngredientIds.Contains(i)).ForEach(i =>
            {
                // delete old ingredient
                BaseRepository.DeleteEntity(oldIngredients.First(j => j.IngredientId == i));
            });
            foreach (var ingredient in viewModel.Ingredients)
            {
                var oldIngredient = oldIngredients.FirstOrDefault(i => i.IngredientId == ingredient.IngredientId);
                // update or add ingredient
                BaseRepository.AddOrUpdateEntity(new RecipeIngredientMapping
                {
                    MappingId    = oldIngredient == null ? 0 : oldIngredient.MappingId,
                    IngredientId = ingredient.IngredientId,
                    RecipeId     = viewModel.RecipeId.Value,
                    Quantity     = ingredient.Quantity,
                    Unit         = ingredient.Unit
                });
            }

            // Update tag mapping
            var oldTags = BaseRepository.GetEntityList <RecipeTagMapping>(new List <ICriterion>
            {
                Restrictions.Eq(RecipeTagMapping.COL_RECIPEID, viewModel.RecipeId)
            });
            var oldTagIds = oldTags.Select(i => i.TagId);
            var newTagIds = viewModel.TagIds;

            oldTagIds.Where(i => !newTagIds.Contains(i)).ForEach(i =>
            {
                // delete old tag
                BaseRepository.DeleteEntity(oldTags.First(j => j.TagId == i));
            });
            foreach (var tagId in viewModel.TagIds.Where(i => !oldTagIds.Contains(i)))
            {
                // add tag
                BaseRepository.AddOrUpdateEntity(new RecipeTagMapping
                {
                    TagId    = tagId,
                    RecipeId = viewModel.RecipeId.Value
                });
            }

            // Update image mapping
            var oldImages = BaseRepository.GetEntityList <Image>(new List <ICriterion>
            {
                Restrictions.Eq(Image.COL_RECIPEID, viewModel.RecipeId)
            });
            var oldImageIds = oldImages.Select(i => i.ImageId.GetValueOrDefault());
            var newImageIds = viewModel.Images.Select(i => i.ImageId).ToList();

            oldImageIds.Where(i => !newImageIds.Contains(i)).ForEach(i =>
            {
                // delete old image
                BaseRepository.DeleteEntity(oldImages.First(j => j.ImageId == i));
            });
            foreach (var image in viewModel.Images)
            {
                // update image
                BaseRepository.AddOrUpdateEntity(new Image
                {
                    ImageId  = image.ImageId,
                    RecipeId = viewModel.RecipeId.Value
                });
            }
        }
コード例 #5
0
 public RecipeInfoPage(Product obj, RecipePageViewModel obj2)
 {
     r = new RecipeInfoViewModel(obj, obj2);
     InitializeComponent();
     DataContext = r;
 }