コード例 #1
0
        public List <CompleteFood> FilteredFoods(FoodsParameters foodsParameters)
        {
            double minProtein   = foodsParameters.MinProtein;
            bool   validProtein = minProtein > 0 && minProtein <= 100;

            if (!validProtein)
            {
                return(new List <CompleteFood>());
            }
            var filteredFoods = context_.Macronutrients.Where(f => f.Name.ToLower().Contains("protein, deklaration") && f.Value >= minProtein);
            var foods         = (from f in context_.Foods
                                 join e in filteredFoods
                                 on f.NameHash equals e.FoodId
                                 select new
            {
                Food = f,
            }).ToList();

            List <CompleteFood> cfs = new List <CompleteFood>();

            foreach (var f in foods)
            {
                CompleteFood cf = new CompleteFood();
                cf.Id     = f.Food.Id;
                cf.NameDk = f.Food.NameDk;
                cf.Food   = f.Food;
                cf.Macros = context_.Macronutrients.Where(o => o.FoodId == cf.Food.NameHash).ToList();
                cfs.Add(cf);
            }

            return(cfs);
        }
コード例 #2
0
        public async Task <List <Foods> > GetPagedFoods(FoodsParameters foodsParameters)
        {
            var foodsTask = context_.Foods
                            .OrderBy(f => f.NameDk)
                            .Skip((foodsParameters.PageNumber - 1) * foodsParameters.PageSize)
                            .Take(foodsParameters.PageSize)
                            .ToListAsync();

            return(await foodsTask);
        }
コード例 #3
0
        /* A get on food name returns a list of SearchFood types since this
         * method is used in the search api call.
         *
         * Input: A food name.
         * Output: A list of SearchFood instances.
         */
        public async Task <List <SearchFood> > GetFoodsByName(FoodsParameters foodsParameters)
        {
            string name = foodsParameters.Name;

            if (string.IsNullOrWhiteSpace(name))
            {
                return(new List <SearchFood>());
            }
            var foodsTask = context_.Foods
                            .Where(o => o.NameDk
                                   .ToLower()
                                   .Contains(name.Trim().ToLower()))
                            .OrderBy(f => f.NameDk)
                            .Skip((foodsParameters.PageNumber - 1) * foodsParameters.PageSize)
                            .Take(foodsParameters.PageSize)
                            .ToListAsync();

            List <SearchFood> sfs = new List <SearchFood>();
            var foods             = await foodsTask;

            foreach (var f in foods)
            {
                SearchFood sf = new SearchFood();
                sf.Id     = f.Id;
                sf.NameDk = f.NameDk;

                List <Macronutrients> macros = await context_.Macronutrients.Where(o => o.FoodId == f.NameHash).ToListAsync();

                sf.Kcal    = macros.Where(o => o.Name.ToLower().Contains("kcal")).Select(o => o.Value).FirstOrDefault();
                sf.Protein = macros.Where(o => o.Name.ToLower().Contains("protein, deklaration")).Select(o => o.Value).FirstOrDefault();
                sf.Carbs   = macros.Where(o => o.Name.ToLower().Contains("kulhydrat, tilgængelig, deklaration")).Select(o => o.Value).FirstOrDefault();
                sf.Fat     = macros.Where(o => o.Name.ToLower().Contains("fedt, total")).Select(o => o.Value).FirstOrDefault();

                sfs.Add(sf);
            }
            return(sfs);
        }
コード例 #4
0
        public List <CompleteFood> GetCompleteFoods(FoodsParameters foodsParameters)
        {
            var foods = FilteredFoods(foodsParameters);

            return(foods);
        }