예제 #1
0
        public void SaveRecipeEditViewModel(RecipeEditViewModel model)
        {
            #region Recipe

            int measurementRecipeId = _measurementRepository.FindOrAddMeasurement(model.Measurement);

            IRecipeData recipeData = new RecipeData()
            {
                Id               = model.Id,
                Content          = model.Content,
                Description      = model.Description,
                IsPublished      = model.IsPublished,
                IsOnlyForFriends = model.IsOnlyForFriends,
                Name             = model.Name,
                Url              = model.Name.BuildUrl(),
                PublishDate      = ConvertPublishDate(model.PublishDate, model.PublishHour, model.PublishMinute),
                IngredientCount  = model.IngredientCount,
                Measurement      = new MeasurementData()
                {
                    Id = measurementRecipeId, Name = model.Measurement
                },
                PreparationTime = model.PreparationTime,
                WaitingTime     = model.WaitingTime,
                TeaserImageUrl  = model.TeaserImageUrl
            };
            if (model.Id == 0)
            {
                model.Id = _recipeRepository.AddRecipe(recipeData);
            }
            else
            {
                _recipeRepository.EditRecipe(recipeData);
            }
            #endregion

            #region TeaserImage
            if (model.TeaserImageUrl != null && model.TeaserImageUrl.ToLower().StartsWith("/upload/"))
            {
                string fileName    = model.TeaserImageUrl.ToLower().Replace("/upload/", "");
                string newFileName = fileName.BuildUrl();
                string mediaPath;
                string mediaExtension = "";
#if DEBUG
                mediaPath = "D:\\Dropbox\\Dokumente Peter\\Visual Studio\\ImageManager\\ImageManager.Web\\media\\LudwigsRezepte";
#else
                mediaPath = "C:\\DarkZero\\Dropbox\\DarkServer\\Webs\\ImageManager\\media\\LudwigsRezepte";
#endif

                mediaPath = mediaPath + "\\" + model.Id + "\\teaser\\";
                string uploadPath = Path.Combine(Environment.CurrentDirectory, "upload\\");

                Directory.CreateDirectory(mediaPath);
                if (File.Exists(mediaPath + newFileName))
                {
                    File.Delete(mediaPath + newFileName);
                }
                if (File.Exists(uploadPath + fileName))
                {
                    File.Move(uploadPath + fileName, mediaPath + newFileName);
                }

                string imageUrl = model.Id + "/teaser/" + newFileName;
                _recipeRepository.EditTeaserImage(model.Id, imageUrl);
            }
            #endregion

            #region IngredientList
            if (model.IngredientList != null)
            {
                List <int> IngredientListIds   = new List <int>();
                int        ingredientListOrder = 0;
                int        measurementId;
                int        ingredientId;
                foreach (IngredientListItemViewModel ingredientListItemViewModel in model.IngredientList)
                {
                    measurementId = _measurementRepository.FindOrAddMeasurement(ingredientListItemViewModel.MeasurementName);
                    ingredientId  = _ingredientRepository.FindOrAddIngredient(ingredientListItemViewModel.IngredientName);


                    if (measurementId != 0 && ingredientId != 0)
                    {
                        ingredientListOrder = ingredientListOrder + 1;
                        IngredientListIds.Add(_ingredientListRepository.AddOrUpdateIngredientListFromRecipe(new IngredientListData()
                        {
                            Id            = ingredientListItemViewModel.Id,
                            Amount        = ingredientListItemViewModel.Amount,
                            SortOrder     = ingredientListOrder,
                            IngredientId  = ingredientId,
                            MeasurementId = measurementId,
                            RecipeId      = model.Id
                        }));
                    }
                }
                _ingredientListRepository.DeleteIngredientListFromRecipeWhereNotInList(IngredientListIds, model.Id);
            }
            #endregion

            #region Categories
            List <int> categoryIds = model.Categories.Where(x => x.IsSelected == true).Select(x => x.Id).ToList();
            _categoryRespository.AddAndRemoveCategoriesFromRecipe(categoryIds, model.Id);
            List <int> subCategoryIds = model.Categories.SelectMany(x => x.SubCategories).Where(x => x.IsSelected == true).Select(x => x.Id).ToList <int>();
            _categoryRespository.AddAndRemoveSubCategoriesFromRecipe(subCategoryIds, model.Id);
            #endregion

            #region Contents
            _recipeRepository.DeleteAllRecipeContents(model.Id);
            int sortOrder = 1;
            foreach (var contentItem in model.ContentItems.OrderBy(x => x.SortOrder))
            {
                if (String.IsNullOrWhiteSpace(contentItem.Content))
                {
                    continue;
                }
                _recipeRepository.AddRecipeContent(model.Id, new LudwigRecipe.Data.DataModels.Recipe.RecipeContent()
                {
                    Content             = contentItem.Content,
                    RecipeContentTypeId = (int)contentItem.ContentType,
                    SortOrder           = sortOrder
                });
                sortOrder = sortOrder + 1;
            }
            #endregion
        }