Ejemplo n.º 1
0
        public ActionResult New()
        {
            var viewModel = new IngredientFormViewModel
            {
                FormType   = "Nowy skladnik",
                Ingredient = new Ingredient()
            };

            return(View("IngredientForm", viewModel));
        }
Ejemplo n.º 2
0
        public ActionResult Edit(int id)
        {
            var ingredient = _context.Ingredients.SingleOrDefault(c => c.Id == id);

            if (ingredient == null)
            {
                return(HttpNotFound());
            }

            var viewModel = new IngredientFormViewModel(ingredient)
            {
                Ingredient = ingredient,
                FormType   = "Edytuj skladnik"
            };

            return(View("IngredientForm", viewModel));
        }
Ejemplo n.º 3
0
        public ActionResult ChangePhoto(int id)
        {
            var ingredient = _context.Ingredients.SingleOrDefault(c => c.Id == id);

            if (ingredient == null)
            {
                return(HttpNotFound());
            }

            var viewModel = new IngredientFormViewModel(ingredient)
            {
                Ingredient = ingredient,
                FormType   = "Zmiana zdjęcia"
            };

            return(View("ChangePhoto", viewModel));
        }
Ejemplo n.º 4
0
        public ActionResult Save(Ingredient ingredient, HttpPostedFileBase UploadImage)
        {
            if (!ModelState.IsValid)
            {
                var typ = ingredient.Id == 0 ? "Nowy skladnik" : "Edytuj skladnik";

                var viewModel = new IngredientFormViewModel()
                {
                    Ingredient = ingredient,
                    FormType   = typ
                };
                return(View("IngredientForm", viewModel));
            }

            if (UploadImage != null)
            {
                if (UploadImage.ContentType == "image/jpg" || UploadImage.ContentType == "image/png" || UploadImage.ContentType == "image/gif" || UploadImage.ContentType == "image/jpeg")
                {
                    UploadImage.SaveAs(Server.MapPath("/") + "Content/Images/Ingredients/" + UploadImage.FileName);
                    ingredient.ImgUrl = UploadImage.FileName;
                }
                else
                {
                    return(RedirectToAction("Index", "Ingredients"));
                }
            }
            else
            {
                ingredient.ImgUrl = null;
            }

            if (ingredient.Id == 0)
            {
                _context.Ingredients.Add(ingredient);
            }
            else
            {
                var ingredientInDb = _context.Ingredients.Single(c => c.Id == ingredient.Id);


                ingredientInDb.Name = ingredient.Name;
            }
            _context.SaveChanges();

            return(RedirectToAction("Index", "Ingredients"));
        }
Ejemplo n.º 5
0
        public ActionResult Edit(int Id)
        {
            IngredientIndex ingredient = _ctx.IngredientIndex.FirstOrDefault(m => m.Id == Id);

            if (ingredient == null)
            {
                return(HttpNotFound());
            }

            IngredientFormViewModel ingredientFormViewModel = new IngredientFormViewModel()
            {
                Ingredient = new IngredientIndex()
            };

            ingredientFormViewModel.Ingredient.Id          = ingredient.Id;
            ingredientFormViewModel.Ingredient.Name        = ingredient.Name;
            ingredientFormViewModel.Ingredient.Description = ingredient.Description;

            return(View("IngredientForm", ingredientFormViewModel));
        }
Ejemplo n.º 6
0
        public IActionResult IngredientForm(IngredientFormViewModel ingredientViewModel)
        {
            var data = IngredientRepository.ActionIngredient(new IngredientDto()
            {
                Id             = ingredientViewModel.Id,
                Name           = ingredientViewModel.Name,
                Description    = ingredientViewModel.Description,
                IngredientType = ingredientViewModel.IngredientType,
            });

            if (data != null)
            {
                if (Request.IsAjaxRequest())
                {
                    return(Json(data));
                }
                return(RedirectToAction("Index"));
            }

            return(Content("Failed"));
        }
Ejemplo n.º 7
0
        public IActionResult IngredientForm(int?id)
        {
            IngredientFormViewModel vm;

            if (id.HasValue && id > 0)
            {
                var dtoResult = IngredientRepository.GetIngredient(id.Value);

                vm = new IngredientFormViewModel()
                {
                    Id             = dtoResult.Id,
                    Name           = dtoResult.Name,
                    Description    = dtoResult.Description,
                    IngredientType = dtoResult.IngredientType,
                };
            }
            else
            {
                vm = new IngredientFormViewModel();
            }

            return(View(vm));
        }
Ejemplo n.º 8
0
        public async Task <IActionResult> Save(IngredientFormViewModel vm)
        {
            if (!ModelState.IsValid)
            {
                return(View("IngredientForm", vm));
            }

            Ingredient ingredient = vm.ToIngredient();

            if (vm.Id == null)
            {
                // add
                await _ingredientRepo.Create(ingredient);
            }
            else
            {
                // edit
                try
                {
                    await _ingredientRepo.Update(ingredient);
                }
                catch (DbUpdateConcurrencyException)
                {
                    if (!_ingredientRepo.Exists(ingredient.Id))
                    {
                        return(NotFound());
                    }
                    else
                    {
                        throw;
                    }
                }
            }

            return(RedirectToAction(nameof(Index)));
        }