public RecipeAddViewModel(IControllerFactory factory, IDishSubject subject, IngredientListViewModel ingredientsViewModel)
        {
            this.factory        = factory;
            this.recipe         = new RecipeAddDTO();
            this.MustSelectDish = true;

            this.SaveCommand   = new DelegateCommand(Save, CanSave);
            this.RemoveCommand = new DelegateCommand(
                () => Ingredients.Remove(IngredientPortion),
                obj => IngredientPortion != null);

            this.IngredientsViewModel = ingredientsViewModel;

            this.Dishes      = new ObservableCollection <DishDisplayDTO>();
            this.Ingredients = new ObservableCollection <IngredientPortion>();

            ingredientsViewModel.IngredientSelected += (s, e) =>
            {
                string ingredientName = e.Data.Name;
                if (!Ingredients.Any(ingredientPortion => ingredientPortion.Ingredient == ingredientName))
                {
                    Ingredients.Add(new IngredientPortion {
                        Ingredient = ingredientName, Portion = Portion
                    });
                }

                Portion = String.Empty;
            };

            subject.Subscribe(this);
            Update();
        }
Example #2
0
        private bool Validate(RecipeAddDTO recipeAddDTO, ref string message)
        {
            bool isValid = true;

            if (String.IsNullOrEmpty(recipeAddDTO.Name))
            {
                isValid = false;
                message = "Recipe's name cannot be empty";
            }
            if (recipeAddDTO.Name.Length > 40)
            {
                isValid = false;
                message = "Recipe's name cannot be more then 40 symbols";
            }
            else if (String.IsNullOrEmpty(recipeAddDTO.DishName))
            {
                isValid = false;
                message = "Recipe's dish's name cannot be empty";
            }
            else if (unitOfWork.Recipes.GetAll().Any(recipe => recipe.Name == recipeAddDTO.Name))
            {
                isValid = false;
                message = "Recipe with such name already exists";
            }

            return(isValid);
        }
Example #3
0
        public ServiceResult <int> AddRecipe(RecipeAddDTO recipeData)        //sprawdzic
        {
            var newRecipe = _mapper.Map <Recipe>(recipeData);

            _recipeRepository.Insert(newRecipe);
            newRecipe.RecipeIngredients = GetOrCreateIngredients(recipeData.Ingredients, newRecipe.Id);
            _recipeRepository.Update(newRecipe);
            return(new ServiceResult <int>(newRecipe.Id));
        }
Example #4
0
 public ActionResult <int> Post(RecipeAddDTO recipeData)
 {
     if (recipeData.Ingredients.Length == 0)
     {
         ModelState.AddModelError("Ingredients", "At least 1 ingredient is required.");
         return(new BadRequestObjectResult(ModelState));
     }
     return(this.HandleServiceResult(_recipeService.AddRecipe(recipeData)));
 }
        private void RaiseRecipeAddedEvent(RecipeAddDTO dish)
        {
            var handler = RecipeAdded;

            if (handler != null)
            {
                GenericEventArgs <RecipeAddDTO> e = new GenericEventArgs <RecipeAddDTO>(dish);
                handler(this, e);
            }
        }
Example #6
0
        public ControllerMessage Add(RecipeAddDTO recipeAddDTO)
        {
            string message = String.Empty;
            bool   success = Validate(recipeAddDTO, ref message);

            if (success)
            {
                try
                {
                    DishEntity dish = unitOfWork.Dishes.Get(recipeAddDTO.DishName);
                    if (dish != null)
                    {
                        RecipeEntity recipeEntity = new RecipeEntity
                        {
                            Name              = recipeAddDTO.Name,
                            Description       = recipeAddDTO.Description,
                            Dish              = dish,
                            RecipeIngredients = new List <RecipeIngredientEntity>()
                        };

                        foreach (IngredientPortion ingredientPortion in recipeAddDTO.Ingredients)
                        {
                            IngredientEntity ingredientEntity = unitOfWork.Ingredients.Get(ingredientPortion.Ingredient);
                            if (ingredientEntity != null)
                            {
                                recipeEntity.RecipeIngredients.Add(new RecipeIngredientEntity
                                {
                                    Ingredient = ingredientEntity,
                                    Recipe     = recipeEntity,
                                    Portion    = ingredientPortion.Portion
                                });
                            }
                        }

                        unitOfWork.Recipes.Add(recipeEntity);
                        unitOfWork.Commit();

                        message = "Recipe added";
                    }
                    else
                    {
                        success = false;
                        message = "Dish not found";
                    }
                }
                catch (Exception ex)
                {
                    success = false;
                    message = ExceptionMessageBuilder.BuildMessage(ex);
                }
            }

            return(new ControllerMessage(success, message));
        }
Example #7
0
        public ServiceResult <RecipeDTO> EditRecipe(int id, RecipeAddDTO recipeData)
        {
            var existingRecipe = _recipeRepository.GetBy(r => r.Id == id);

            if (existingRecipe == null)
            {
                return(new ServiceResult <RecipeDTO>("Recipe doesn't exist"));
            }
            existingRecipe.Name              = recipeData.Name;
            existingRecipe.Description       = recipeData.Description;
            existingRecipe.TimeToMake        = recipeData.TimeToMake;
            existingRecipe.RecipeIngredients = GetOrCreateIngredients(recipeData.Ingredients, existingRecipe.Id);
            _recipeRepository.Update(existingRecipe);
            return(new ServiceResult <RecipeDTO>(GetRecipe(existingRecipe.Id).SuccessResult));
        }
        private void OnAdd(RecipeAddDTO recipe, RecipeAddViewModel viewModel)
        {
            using (IRecipeController controller = factory.CreateRecipeController())
            {
                ControllerMessage controllerMessage = controller.Add(recipe);
                if (controllerMessage.IsSuccess)
                {
                    viewModel.Name        = String.Empty;
                    viewModel.Description = String.Empty;
                    viewModel.Ingredients.Clear();

                    Notify();
                }
                else
                {
                    MessageBox.Show(controllerMessage.Message);
                }
            }
        }
Example #9
0
        private Recipe CreateRecipe(RecipeAddDTO recipeData)
        {
            var newRecipe = _mapper.Map <Recipe>(recipeData);

            newRecipe.RecipeIngredients = new List <RecipeIngredient>();
            foreach (var ingredientData in recipeData.Ingredients)
            {
                var ingredient = _ingredientService.GetOrCreateIngredient(ingredientData.Name);
                newRecipe.RecipeIngredients.Add(
                    new RecipeIngredient
                {
                    RecipeId     = newRecipe.Id,
                    IngredientId = ingredient.Id,
                    Amount       = ingredientData.Amount,
                    AmountUnit   = ingredientData.AmountUnit
                });
            }
            return(newRecipe);
        }
Example #10
0
 public ActionResult <RecipeDTO> Put(int id, RecipeAddDTO recipeData)
 {
     return(this.HandleServiceResult(_recipeService.EditRecipe(id, recipeData)));
 }