public IActionResult Post([FromBody] Recipe input)
        {
            var location = _linkGenerator.GetPathByAction("Get", "Recipes");

            var recipe = _mapper.Map <Recipe>(input);

            _recipeData.Add(recipe);
            _recipeData.Commit();
            return(Created(location, _mapper.Map <ApiRecipe>(recipe)));
        }
        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 }));
        }
Exemple #3
0
        public IActionResult Create(RecipeEditModel model)
        {
            if (ModelState.IsValid)
            {
                var newRecipe = new Recipe();
                newRecipe.Name      = model.Name;
                newRecipe.Timeofday = model.Timeofday;
                newRecipe           = _recipeData.Add(newRecipe);

                //return View("Details", newRecipe);
                return(RedirectToAction(nameof(Details), new { id = newRecipe.Id }));
            }
            else
            {
                return(View());
            }
        }
Exemple #4
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 }));
        }
        public async Task <IActionResult> OnPostAsync()
        {
            var userId = userManager.GetUserId(User);

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

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

                Recipe.ImageUrl = image;
            }

            Recipe.UserId = userId;
            recipeData.Add(Recipe);
            TempData["Message"] = "Recipe Added.";
            recipeData.Commit();
            return(RedirectToPage("./Detail", new { recipeId = Recipe.Id }));
        }
        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 }));
        }