Example #1
0
        public async Task <int> EditAsync(RecipeEditDto model)
        {
            var recipe = this.recipesRepository
                         .All()
                         .FirstOrDefault(r => r.Id == model.Id);

            recipe.Title        = model.Title;
            recipe.Description  = model.Description;
            recipe.Advices      = model.Advices;
            recipe.Servings     = model.Servings;
            recipe.PrepTime     = model.PrepTime;
            recipe.CookTime     = model.CookTime;
            recipe.SeasonalType = model.SeasonalType;
            recipe.SkillLevel   = model.SkillLevel;
            recipe.AuthorId     = model.AuthorId;
            recipe.CategoryId   = model.CategoryId;
            recipe.CuisineId    = model.CuisineId;
            recipe.IsApproved   = false;

            this.recipesRepository.Update(recipe);
            await this.recipesRepository.SaveChangesAsync();

            if (model.NewImages != null)
            {
                foreach (var imageFile in model.NewImages)
                {
                    var imageUrl = await this.cloudinaryService.UploadAsync(imageFile, imageFile.FileName, CloudinaryFolderName);

                    var image = new Image()
                    {
                        Url      = imageUrl,
                        RecipeId = recipe.Id,
                    };

                    await this.imagesService.AddAsync(image);
                }
            }

            await this.ingredientsService.DeleteIngredientsByRecipeIdAsync(recipe.Id);

            var ingredientsNames = model.IngredientsNames.Split("\r\n");

            foreach (var ingredientName in ingredientsNames)
            {
                await this.ingredientsService.SetIngredientToRecipeAsync(ingredientName, recipe.Id);
            }

            await this.DeleteCookingMethodInRecipe(recipe.Id);

            foreach (var cookingMethod in model.CookingMethods)
            {
                if (cookingMethod.Selected)
                {
                    await this.SetRecipeToRecipeCookingMethodsAsync(cookingMethod.Id, recipe.Id);
                }
            }

            await this.recipesRepository.SaveChangesAsync();

            return(recipe.Id);
        }
Example #2
0
        public async Task <IActionResult> Edit(RecipeEditInputModel input)
        {
            var userId                = this.userManager.GetUserId(this.User);
            var isUserRecipeAuthor    = this.usersService.IsUserRecipeAuthor(userId, input.Id);
            var allowedMaxCountImages = int.Parse(this.TempData["MaxCountImages"].ToString());

            var isValidImages = true;

            if (input.Images != null)
            {
                isValidImages = input.NewImages?.Count <= allowedMaxCountImages;
                if (!isValidImages)
                {
                    this.ViewData["Errors"] += $"Можете да качите най-много {allowedMaxCountImages} снимки" + "\r\n";
                }
            }

            var isExist      = this.recipesService.IsExistRecipeTitle(input.Title);
            var title        = this.recipesService.GetRecipeTitle(input.Id);
            var isValidTitle = true;

            if (isExist && title.ToLower() != input.Title.ToLower())
            {
                isValidTitle             = false;
                this.ViewData["Errors"] += RecipeExistNameError + "\r\n";
            }

            var isAtLeastChecked = false;

            foreach (var cookingMethod in input.AllCookingMethods)
            {
                if (cookingMethod.Selected)
                {
                    isAtLeastChecked = true;
                    break;
                }
            }

            var isValidIngredients = true;

            foreach (var ingredientName in input.IngredientsNames.Split("\r\n", StringSplitOptions.RemoveEmptyEntries))
            {
                if (ingredientName.Length > AttributesConstraints.IngredientNameMaxLength ||
                    ingredientName.Length < AttributesConstraints.IngredientNameMinLength)
                {
                    isValidIngredients       = false;
                    this.ViewData["Errors"] += IngredientNameError + "\r\n";
                    break;
                }
            }

            if (!this.ModelState.IsValid ||
                !isAtLeastChecked ||
                !isValidTitle ||
                !isValidIngredients ||
                !isValidImages ||
                !isUserRecipeAuthor)
            {
                var categories     = this.categoriesService.GetAll <RecipeEditCategoryDropDownViewModel>();
                var cuisines       = this.cuisinesService.GetAll <RecipeEditCuisineDropDownViewModel>();
                var cookingMethods = this.cookingMethodsService.GetAll <RecipeEditCookingMethodsCheckboxViewModel>();

                input.Categories        = categories;
                input.Cuisines          = cuisines;
                input.AllCookingMethods = cookingMethods;

                var imagesCount = input.Images?.Count ?? 0;
                this.TempData["MaxCountImages"] = AttributesConstraints.RecipeImagesMaxCount - imagesCount;

                return(this.View(input));
            }

            var user = await this.userManager.GetUserAsync(this.User);

            var recipeDto = new RecipeEditDto
            {
                Id               = input.Id,
                Title            = input.Title,
                AuthorId         = user.Id,
                Description      = input.Description,
                Advices          = input.Advices,
                Servings         = input.Servings,
                PrepTime         = input.PrepTime,
                CookTime         = input.CookTime,
                SeasonalType     = input.SeasonalType,
                SkillLevel       = input.SkillLevel,
                CategoryId       = input.CategoryId,
                CuisineId        = input.CuisineId,
                NewImages        = input.NewImages,
                IngredientsNames = input.IngredientsNames,
                CookingMethods   = input.AllCookingMethods,
            };

            int recipeId = await this.recipesService.EditAsync(recipeDto);

            this.TempData["SuccessEditRecipe"] = SuccessEditRecipe;

            return(this.Redirect("/"));
        }