예제 #1
0
        public async Task <int> AddAsync(RecipeCreateServiceModel model)
        {
            var recipe = new Recipe()
            {
                Title        = model.Title,
                Description  = model.Description,
                Advices      = model.Advices,
                Servings     = model.Servings,
                PrepTime     = model.PrepTime,
                CookTime     = model.CookTime,
                SeasonalType = model.SeasonalType,
                SkillLevel   = model.SkillLevel,
                AuthorId     = model.AuthorId,
                CategoryId   = model.CategoryId,
                CuisineId    = model.CuisineId,
                IsApproved   = false,
            };

            await this.recipesRepository.AddAsync(recipe);

            await this.recipesRepository.SaveChangesAsync();

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

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

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

            if (model.TitleImage != null)
            {
                var titleImageUrl = await this.cloudinaryService.UploadAsync(model.TitleImage, model.TitleImage.FileName, CloudinaryFolderName);

                var titleImage = new Image()
                {
                    Url          = titleImageUrl,
                    IsTitlePhoto = true,
                    RecipeId     = recipe.Id,
                };

                await this.imagesService.AddAsync(titleImage);
            }

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

            foreach (var ingredientName in ingredientsNames)
            {
                await this.ingredientsService.SetIngredientToRecipeAsync(ingredientName, 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);
        }
예제 #2
0
        public async Task <IActionResult> Create(RecipeCreateInputModel input)
        {
            var isExist = this.recipesService.IsExistRecipeTitle(input.Title);

            if (isExist)
            {
                this.ViewData["Errors"] += RecipeExistNameError + "\r\n";
            }

            var isAtLeastChecked = false;

            foreach (var cookingMethod in input.CookingMethods)
            {
                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 || isExist || !isValidIngredients)
            {
                var categories     = this.categoriesService.GetAll <RecipeCreateCategoryDropDownViewModel>();
                var cuisines       = this.cuisinesService.GetAll <RecipeCreateCuisineDropDownViewModel>();
                var cookingMethods = this.cookingMethodsService.GetAll <RecipeCreateCookingMethodsCheckboxViewModel>();

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

                return(this.View(input));
            }

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

            var serviceModel = new RecipeCreateServiceModel
            {
                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,
                Images           = input.Images,
                TitleImage       = input.TitleImage,
                IngredientsNames = input.IngredientsNames,
                CookingMethods   = input.CookingMethods,
            };

            int recipeId = await this.recipesService.AddAsync(serviceModel);

            this.TempData["SuccessCreateRecipe"] = SuccessCreateRecipe;

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