示例#1
0
        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)));
        }
示例#2
0
        public IActionResult OnPost(int recipeId)
        {
            var recipe = recipeData.Delete(recipeId);

            recipeData.Commit();

            if (recipe == null)
            {
                return(RedirectToPage("./NotFound"));
            }

            TempData["Message"] = $"The recipe {recipe.Name} has been deleted.";
            return(RedirectToPage("./List"));
        }
示例#3
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 }));
        }
示例#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 }));
        }
示例#5
0
        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 }));
        }
示例#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 }));
        }