Beispiel #1
0
        //&intolerances=egg&diet=vegetarian&maxCarbs=400&maxProtein=15&minProtein=1
        //Called to list out recipes based on the user search criteria
        public virtual async Task <List <Recipe> > SearchForRecipeByQuery(string diet, Intolerances intolerance, int?maxCalorie, int?maxCarb, int?maxProtein, int?minProtein)
        {
            StringBuilder query = new StringBuilder();

            if (diet != null)
            {
                query.Append($"&diet={diet}");
            }
            if (intolerance != null)
            {
                query.Append($"&intolerances={intolerance.ToString()}");
            }
            if (maxCalorie.HasValue)
            {
                query.Append($"&maxCalories={maxCalorie}");
            }
            if (maxCarb.HasValue)
            {
                query.Append($"&maxCarbs={maxCarb}");
            }
            if (maxProtein.HasValue)
            {
                query.Append($"&maxProtein={maxProtein}");
            }
            if (minProtein.HasValue)
            {
                query.Append($"&minProtein={minProtein}");
            }
            var response = await _httpClient.GetAsync($"/recipes/complexSearch?apiKey={_config.ApiKey}{query}&number=25");

            var recipe = JsonConvert.DeserializeObject <ReciepeApiResults>(await response.Content.ReadAsStringAsync());

            return(recipe.Results);
        }
Beispiel #2
0
        //Saves user preferences to database
        public async Task <UserPreference> SaveUserPreferences(int id, string userId, string diet, Intolerances intolerances, int?maxCalorie, int?maxCarb, int?maxProtein, int?minProtein)
        {
            var savedUserPreferences = new UserPreference()
            {
                Id           = id,
                UserId       = userId,
                Diet         = diet,
                Intolerances = intolerances.ToString(),
                MaxCalorie   = maxCalorie,
                MaxCarb      = maxCarb,
                MaxProtein   = maxProtein,
                MinProtein   = minProtein,
            };

            try
            {
                _context.UserPreferences.Add(savedUserPreferences);
                await _context.SaveChangesAsync();
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex);
            }
            return(savedUserPreferences);
        }