Example #1
0
 public IngredientAdvice AddIngredientAdvice(Mentor mentor, IngredientAdvice advice)
 {
     if (advice.IngredientsId == null)
     {
         throw new ArgumentException("IngredientsId cannot be null when adding IngredientAdvice");
     }
     using (var ingredientRepository = _repositoryFactory.Build <IRepository <Ingredient>, Ingredient>())
     {
         var ingredient = ingredientRepository.FindOne(x => x.Id == advice.IngredientsId);
         return(AddAdvice(mentor, advice, ingredient));
     }
 }
        public ActionResult Edit(IngredientAdvice ingredientAdvice, FormCollection form)
        {
            ValidateAdvice(ingredientAdvice);
            if (ModelState.IsValid)
            {
                _adviceApplicationService.UpdateAdvice(ingredientAdvice);
                return(RedirectToAction("Index", "Advice"));
            }
            ViewData["Semaphores"] = _semaphoreApplicationService.GetAllSemaphores();
            var advice     = _adviceApplicationService.GetAdvice(ingredientAdvice.Id.Value) as IngredientAdvice;
            var ingredient = _ingredientApplicationService.GetIngredientById(advice.IngredientsId.Value);

            ViewData["Ingredient"] = ingredient;
            return(View(advice));
        }
        public ActionResult Create(IngredientAdvice ingredientAdvice, FormCollection form)
        {
            //if (ModelState.IsValid)
            //{

            //Ingredient ingredient = new Ingredient();
            // Deserialize (Include white list!)
            //bool isModelUpdated = TryUpdateModel(ingredientAdvice,
            //                                     new[]
            //                                         {
            //                                             "IngredientsId", "Label", "Introduction", "Advice", "KeyWords",
            //                                             "Semaphore", "Published"
            //                                         }, form.ToValueProvider());

            // Validate

            if (ingredientAdvice.IngredientsId == null)
            {
                ModelState.AddModelError("IngredientsId", "Please choose an ingredient.");
            }
            ValidateAdvice(ingredientAdvice);
            if (ModelState.IsValid)
            {
                try
                {
                    _adviceApplicationService.AddIngredientAdvice(CurrentMentor, ingredientAdvice);

                    return(RedirectToAction("Index", "Advice"));
                }
                catch
                {
                    return(RedirectToAction("Create"));
                }
            }
            var ingredients = _ingredientApplicationService.GetAllIngredients();

            ViewData["Ingredients"] = new SelectList(ingredients, "Id", "IngredientName", ingredientAdvice.IngredientsId);
            ViewData["Semaphores"]  = _semaphoreApplicationService.GetAllSemaphores();
            return(View(ingredientAdvice));
        }