Exemple #1
0
        public async Task <List <Recipe> > SearchRecipes(SearchRecipeModel searchRecipeModel)
        {
            if (searchRecipeModel != null && "".Equals(searchRecipeModel.Name))
            {
                throw new SearchException("Name for search can't be empty");
            }


            if (searchRecipeModel.LowTime.HasValue && searchRecipeModel.LowTime < 0)
            {
                throw new SearchException("The low kcal constraint can't be negative");
            }

            if (searchRecipeModel.HighTime.HasValue && searchRecipeModel.HighTime < 0)
            {
                throw new SearchException("The high kcal constraint can't be negative");
            }

            if (searchRecipeModel.LowTime.HasValue &&
                searchRecipeModel.LowTime.HasValue &&
                searchRecipeModel.LowTime > searchRecipeModel.HighTime)
            {
                throw new SearchException("The low kcal constraint can't be bigger than high kcal");
            }


            if (searchRecipeModel.LowTotalCost.HasValue && searchRecipeModel.LowTotalCost < 0)
            {
                throw new SearchException("The low kcal constraint can't be negative");
            }

            if (searchRecipeModel.HighTotalCost.HasValue && searchRecipeModel.HighTotalCost < 0)
            {
                throw new SearchException("The high kcal constraint can't be negative");
            }

            if (searchRecipeModel.LowTotalCost.HasValue &&
                searchRecipeModel.HighTotalCost.HasValue &&
                searchRecipeModel.LowTotalCost > searchRecipeModel.HighTotalCost)
            {
                throw new SearchException("The low kcal constraint can't be bigger than high kcal");
            }

            if (searchRecipeModel.IngridientsIds != null && searchRecipeModel.IngridientsIds.Count != 0)
            {
                try
                {
                    await _ingridientService.GetIngridients(searchRecipeModel.IngridientsIds);
                }
                catch (EntityDoesNotExistException e)
                {
                    throw new SearchException("One or more ingridient from search don't exist", e);
                }
            }

            return(await _recipeRepository.SearchRecipes(searchRecipeModel));
        }
        public void SearchIngridients_ShouldThrow_SearchException(SearchRecipeModel searchRecipeModel, string expectedMessage)
        {
            //Arrange
            var(recipeRepository, ingridientService, dataBase) = GetMocks();
            var recipeService = new RecipeService(recipeRepository.Object, ingridientService.Object);

            //Act
            var exception = Assert.ThrowsAsync <SearchException>(() => recipeService.SearchRecipes(searchRecipeModel));

            //Assert
            Assert.AreEqual(expectedMessage, exception.Message);
        }
        public async Task SearchRecipes_ShouldReturn_Ingridients()
        {
            //Arrange
            var(recipeRepository, ingridientService, dataBase) = GetMocks();
            var recipeService     = new RecipeService(recipeRepository.Object, ingridientService.Object);
            var searchRecipeModel = new SearchRecipeModel();

            //Act
            var recipes = await recipeService.SearchRecipes(searchRecipeModel);

            //Assert
            Assert.AreEqual(dataBase.Select(p => p.Value).ToList(), recipes);
        }