コード例 #1
0
 private void UnassignCategoryForRecipes(List <Recipe> recipesInAssignedCategory)
 {
     foreach (var recipeCategoryToClear in recipesInAssignedCategory)
     {
         recipeCategoryToClear.CategoryId = null;
         recipeData.Update(recipeCategoryToClear);
     }
 }
コード例 #2
0
        public IActionResult Put(int id, [FromBody] Recipe input)
        {
            var oldRecipe = _recipeData.GetById(input.Id);

            if (oldRecipe == null)
            {
                return(NotFound());
            }
            _recipeData.Update(input);
            _recipeData.Commit();
            return(Ok());
        }
コード例 #3
0
        public IActionResult OnGetAddIngredient(string recipeId, string ingredientToAdd, string recipeNameToAdd, string cookTimeMinutesToAdd, string servingsToAdd)
        {
            RetrieveRecipe(recipeId, string.Empty);

            if (Recipe.Ingredients.Contains(ingredientToAdd))
            {
                Message = $"Ingredient ({ingredientToAdd}) was not added.  It already exists.";
            }
            else
            {
                if (!string.IsNullOrEmpty(recipeNameToAdd))
                {
                    Recipe.Name = recipeNameToAdd;
                }
                if (!string.IsNullOrEmpty(cookTimeMinutesToAdd))
                {
                    Recipe.CookTimeMinutes = int.Parse(cookTimeMinutesToAdd);
                }
                if (!string.IsNullOrEmpty(servingsToAdd))
                {
                    Recipe.Servings = int.Parse(servingsToAdd);
                }

                Recipe.Ingredients.Add(ingredientToAdd);

                Recipe = string.IsNullOrEmpty(Recipe.Id) ? recipeData.Add(Recipe) : recipeData.Update(Recipe);

                if (ingredientToAdd.Contains("/"))
                {
                    Message = "Ingredient added.";
                }
                else
                {
                    Message = $"Ingredient ({ingredientToAdd}) added.";
                }
            }

            return(new JsonResult(new { recipeId = Recipe.Id, message = Message }));
        }
コード例 #4
0
        public async Task <IActionResult> OnPostAsync()
        {
            var userId = userManager.GetUserId(User);

            if (!ModelState.IsValid)
            {
                Cuisines = htmlHelper.GetEnumSelectList <CuisineType>();
                return(Page());
            }

            if (NewImage != null)
            {
                var image = await imageData.UploadImageAsync(NewImage);

                Recipe.ImageUrl = image;
            }

            // Update existing Ingredients
            var savedIngredients = new List <int>();

            foreach (var ingredient in Recipe.Ingredients)
            {
                if (ingredient.Id > 0)
                {
                    savedIngredients.Add(ingredient.Id);
                    ingredientData.Update(ingredient);
                }
            }

            // Delete removed ingredients
            var originalRecipe     = recipeData.GetById(Recipe.Id);
            var deletedIngredients = originalRecipe.Ingredients.Where(i => !savedIngredients.Contains(i.Id));

            foreach (var deletedIngredient in deletedIngredients)
            {
                ingredientData.Delete(deletedIngredient.Id);
            }

            // Update Recipe
            recipeData.Update(Recipe);
            TempData["Message"] = "Recipe Updated.";
            recipeData.Commit();
            return(RedirectToPage("./Detail", new { recipeId = Recipe.Id }));
        }
コード例 #5
0
        public IActionResult OnPost()
        {
            if (!ModelState.IsValid)
            {
                Cuisines = htmlHelper.GetEnumSelectList <CuisineType>();
                return(Page());
            }

            if (Recipe.Id > 0)
            {
                recipeData.Update(Recipe);
            }
            else
            {
                recipeData.Add(Recipe);
            }
            recipeData.Commit();
            TempData["Message"] = "Recipe saved!";
            return(RedirectToPage("./Detail", new { recipeId = Recipe.Id }));
        }
コード例 #6
0
        public IActionResult Edit(Recipe recipe)
        {
            Ingredient tempIngredient;

            foreach (var component in recipe.RecipeComponents)
            {
                tempIngredient = _ingredientData.GetIngredientByExactName(component.Ingredient.Name);
                if (tempIngredient != null)
                {
                    component.Ingredient   = tempIngredient;
                    component.IngredientId = tempIngredient.Id;
                }

                if (component.Id > 0)
                {
                    _recipeComponentData.Update(component);
                }
                else
                {
                    _recipeComponentData.Add(component);
                }
            }


            if (!ModelState.IsValid)
            {
                var viewModel = new RecipeViewModel(_htmlHelper)
                {
                    Recipe = recipe
                };
                return(View(recipe));
            }
            string webRootPath = _webHostEnvironment.WebRootPath;
            var    files       = HttpContext.Request.Form.Files;

            if (recipe.Id > 0)
            {
                if (files.Count > 0)
                {
                    string fileName      = Guid.NewGuid().ToString();
                    var    uploads       = Path.Combine(webRootPath, @"images\recipes");
                    var    extension_new = Path.GetExtension(files[0].FileName);
                    string currentImg    = "";
                    if (recipe.ImageUrl != null)
                    {
                        currentImg = recipe.ImageUrl.TrimStart('\\');
                    }

                    var imagePath = Path.Combine(webRootPath, currentImg);
                    if (System.IO.File.Exists(imagePath))
                    {
                        System.IO.File.Delete(imagePath);
                    }
                    using (var fileStreams = new FileStream(Path.Combine(uploads, fileName + extension_new), FileMode.Create))
                    {
                        var imageFactory = new ImageFactory(true);
                        imageFactory.Load(files[0].OpenReadStream()).Resize(
                            new ResizeLayer(new Size(720, 480), ResizeMode.Max)).Save(fileStreams);

                        //files[0].CopyTo(fileStreams);
                    }
                    recipe.ImageUrl = @"\images\recipes\" + fileName + extension_new;
                }


                _recipeData.Update(recipe);
            }
            else
            {
                if (files.Count > 0)
                {
                    string fileName  = Guid.NewGuid().ToString();
                    var    uploads   = Path.Combine(webRootPath, @"images\recipes");
                    var    extension = Path.GetExtension(files[0].FileName);

                    using (var fileStreams = new FileStream(Path.Combine(uploads, fileName + extension), FileMode.Create))
                    {
                        var imageFactory = new ImageFactory(true);
                        imageFactory.Load(files[0].OpenReadStream()).Resize(
                            new ResizeLayer(new Size(1080, 720), ResizeMode.Max)).Save(fileStreams);


                        //files[0].CopyTo(fileStreams);
                    }
                    recipe.ImageUrl = @"\images\recipes\" + fileName + extension;
                }
                recipe.CreationDate = DateTime.Now;
                _recipeData.Add(recipe);
            }
            _recipeData.Commit();


            return(RedirectToAction("Detail", new { area = "Common", recipeId = recipe.Id }));
        }