public async Task <IActionResult> SearchRecipesTitle(string search, string groupName)
        {
            if (search == null)
            {
                Group group = _context.Group.Where(u => u.GroupName == groupName).First();
                TempData["NotValid"] = "Please enter a valid search";
                return(RedirectToAction("ViewGroup", "Group", group));
            }

            else
            {
                var client   = RecipeMethods.GetHttpClient();
                var response = await client.GetAsync($"api/json/v1/{_apiKey}/search.php?s={search}");

                var recipes = await response.Content.ReadAsAsync <Recipe>();

                if (recipes.meals == null)
                {
                    Group group = _context.Group.Where(u => u.GroupName == groupName).First();
                    TempData["NotValid"] = "There are no recipes that fit that search";
                    return(RedirectToAction("ViewGroup", "Group", group));
                }
                else
                {
                    var filteredRecipes = FilterRecipes(recipes);
                    if (filteredRecipes.meals == null)
                    {
                        filteredRecipes.meals[0] = null;
                    }
                    TempData["NotValid"] = null;
                    return(View("FindRecipes", filteredRecipes));
                }
            }
        }
        public async Task <Meal> GetRecipeByName(string meal)
        {
            var client   = RecipeMethods.GetHttpClient();
            var response = await client.GetAsync($"api/json/v1/{_apiKey}/search.php?s={meal}");

            var name = await response.Content.ReadAsAsync <Meal>();

            return(name);
        }
Beispiel #3
0
        public async Task <Recipe> GetRandomRecipe()
        {
            var client   = RecipeMethods.GetHttpClient();
            var response = await client.GetAsync($"api/json/v1/{_apiKey}/random.php");

            var recipes = await response.Content.ReadAsAsync <Recipe>();

            return(recipes);
        }
        public async Task <Recipe> FindRecipesById(Meal meal)
        {
            string search   = meal.idMeal;
            var    client   = RecipeMethods.GetHttpClient();
            var    response = await client.GetAsync($"api/json/v1/{_apiKey}/lookup.php?i={search}");

            var viewMeal = await response.Content.ReadAsAsync <Recipe>();

            return(viewMeal);
        }
Beispiel #5
0
        public async Task <Meal> FindFavRecipesById(string mealId)
        {
            var client   = RecipeMethods.GetHttpClient();
            var response = await client.GetAsync($"api/json/v1/{_apiKey}/lookup.php?i={mealId}");

            var viewMeal = await response.Content.ReadAsAsync <Recipe>();

            var meal = viewMeal.meals[0];

            return(meal);
        }
        public async Task <IActionResult> SearchRecipesCategory(string search)
        {
            var client   = RecipeMethods.GetHttpClient();
            var response = await client.GetAsync($"api/json/v1/{_apiKey}/filter.php?c={search}");

            var recipes = await response.Content.ReadAsAsync <Recipe>();

            var filteredRecipes = FilterRecipesGroup(recipes);

            return(View("FindRecipes", filteredRecipes));
        }
        public async Task <IActionResult> GetRandomRecipe()
        {
            Recipe filteredRecipes = new Recipe();
            bool   notNull         = false;

            while (notNull == false)
            {
                var client   = RecipeMethods.GetHttpClient();
                var response = await client.GetAsync($"api/json/v1/{_apiKey}/random.php");

                var recipes = await response.Content.ReadAsAsync <Recipe>();

                filteredRecipes = FilterRecipes(recipes);
                if (filteredRecipes.meals[0] != null)
                {
                    notNull = true;
                }
            }
            return(View("ViewRecipe", filteredRecipes));
        }
        // for ingredients
        public async Task <IActionResult> SearchRecipesIngredients(string search)
        {
            Regex rgx = new Regex(@"^[a-zA-Z]+[ a-zA-Z]*$");

            if (search == null)
            {
                TempData["RegexMatch"] = "Please enter a valid search";
                return(View("Search"));
            }
            else if (rgx.IsMatch(search))
            {
                var client = RecipeMethods.GetHttpClient();

                var response = await client.GetAsync($"api/json/v1/{_apiKey}/filter.php?i={search}");

                var recipes = await response.Content.ReadAsAsync <Recipe>();

                if (recipes.meals == null)
                {
                    TempData["RegexMatch"] = "No meals by this name";
                    return(View("search"));
                }
                else
                {
                    var actualRecipes   = GetRecipesById(recipes);
                    var filteredRecipes = FilterRecipes(actualRecipes);
                    if (filteredRecipes.meals[0] == null)
                    {
                        filteredRecipes.meals[0] = null;
                    }

                    return(View("FindRecipes", filteredRecipes));
                }
            }
            else
            {
                TempData["RegexMatch"] = "Please enter a valid search";
                return(View("search"));
            }
        }
        public Recipe FilterRecipesGroup(Recipe recipes)
        {
            var restrictions = GetAllRestrictions();

            int count         = recipes.meals.Length;
            var filteredMeals = new Meal[count];
            int index         = 0;

            if (restrictions != null)
            {
                foreach (var item in recipes.meals)
                {
                    bool isBad       = false;
                    var  ingredients = RecipeMethods.AddIngredients(item);

                    foreach (var name in restrictions)
                    {
                        var ingred = name.Id.ToLower();

                        if (ingredients.Contains(ingred))
                        {
                            isBad = true;
                        }
                    }
                    if (isBad == false)
                    {
                        filteredMeals[index] = item;
                    }
                    index++;
                }
            }
            Recipe filteredRecipes = new Recipe();

            filteredRecipes.meals = filteredMeals;
            return(filteredRecipes);
        }
        public Recipe FilterRecipes(Recipe recipes)
        {
            var restrictions = GetAllRestrictions();

            int count         = recipes.meals.Length;
            var filteredMeals = new Meal[count];
            int index         = 0;

            if (restrictions != null)
            {
                foreach (var item in recipes.meals)
                {
                    bool isBad        = false;
                    var  ingredients  = RecipeMethods.AddIngredients(item);
                    var  instructions = item.strInstructions.ToLower();

                    foreach (var name in restrictions)
                    {
                        var ingred = name.Id.ToLower();

                        if (ingredients.Contains(ingred))
                        {
                            isBad = true;
                        }
                    }
                    foreach (var name in restrictions)
                    {
                        var ingred = name.Id.ToLower();

                        if (instructions.Contains(ingred))
                        {
                            isBad = true;
                        }
                    }
                    if (isBad == false)
                    {
                        filteredMeals[index] = item;
                    }
                    index++;
                }
            }
            Recipe filteredRecipes = new Recipe();

            filteredRecipes.meals = filteredMeals;

            Recipe myRecipes = new Recipe();

            count = 0;
            foreach (var recipe in filteredRecipes.meals)
            {
                if (recipe != null)
                {
                    count++;
                }
            }
            if (count != 0)
            {
                Meal[] newMeals = new Meal[count];
                count = 0;
                foreach (var recipe in filteredRecipes.meals)
                {
                    if (recipe != null)
                    {
                        newMeals[count] = recipe;
                        count++;
                    }
                }
                filteredRecipes.meals = newMeals;
            }
            else
            {
                Meal[] mealStuff = new Meal[1];
                filteredRecipes.meals = mealStuff;
            }
            return(filteredRecipes);
        }