Beispiel #1
0
        public ActionResult <FoodsDTO> GetAllFoods(string name = "")
        {
            using (var db = new SqliteContext())
            {
                try
                {
                    var foods = db.Foods.Where(x => x.Name.ToLower().Contains(name.ToLower()));

                    var dto = new FoodsDTO();
                    dto.FoodList = new List <FoodDTO>();

                    foreach (var food in foods)
                    {
                        var entry = new FoodDTO
                        {
                            Id                 = food.Id,
                            Name               = food.Name,
                            ServingSize        = food.ServingSize,
                            ProteinAmount      = food.ProteinAmount,
                            FatAmount          = food.FatAmount,
                            CarbohydrateAmount = food.CarbohydrateAmount,
                            Calories           = food.Calories,
                        };

                        dto.FoodList.Add(entry);
                    }

                    return(dto);
                }
                catch (ArgumentOutOfRangeException)
                {
                    return(StatusCode(404));
                }
            }
        }
        public ActionResult <FoodsDTO> SearchFoods(
            string name = "",
            int take    = 5)
        {
            try
            {
                var products = this.Database.Products
                               .Where(x => x.long_name.ToLower().Contains(name.ToLower()))
                               .Take(take);

                var dto = new FoodsDTO();
                dto.FoodList = new List <FoodDTO>();

                foreach (var product in products)
                {
                    var nutrients = this.Database.Nutrients
                                    .Where(x => x.NDB_No == product.NDB_Number);

                    var servingSizes = this.Database.Serving_size
                                       .Where(x => x.NDB_No == product.NDB_Number);

                    var entry = new FoodDTO
                    {
                        Id           = int.Parse(product.NDB_Number),
                        Name         = product.long_name,
                        Manufacturer = product.manufacturer,
                        ServingSize  = new ServingSizeDTO(),
                        Nutrients    = new List <NutrientDTO>(),
                    };

                    string housingServingSize =
                        servingSizes.FirstOrDefault(x => x.NDB_No == product.NDB_Number).Household_Serving_Size;
                    string housingServingSizeUOM =
                        servingSizes.FirstOrDefault(x => x.NDB_No == product.NDB_Number).Household_Serving_Size_UOM;

                    // TODO Exception thrown here...
                    entry.ServingSize.Value = double.Parse(housingServingSize);
                    entry.ServingSize.UOM   = housingServingSizeUOM;

                    var protein = nutrients.FirstOrDefault(x => x.Nutrient_name == "Protein");
                    if (protein != null)
                    {
                        entry.Nutrients.Add(new NutrientDTO
                        {
                            NutrientName = "Protein",
                            Value        = double.Parse(protein.Output_value),
                            UOM          = protein.Output_uom,
                        });
                    }

                    var carbohydrate = nutrients.FirstOrDefault(x => x.Nutrient_name == "Carbohydrate, by difference");
                    if (carbohydrate != null)
                    {
                        entry.Nutrients.Add(new NutrientDTO
                        {
                            NutrientName = "Carbohydrate",
                            Value        = double.Parse(carbohydrate.Output_value),
                            UOM          = carbohydrate.Output_uom,
                        });
                    }


                    double fatAmount = double.Parse(
                        nutrients.FirstOrDefault(x => x.Nutrient_name == "Total lipid (fat)").Output_value);
                    double sodiumAmount = double.Parse(
                        nutrients.FirstOrDefault(x => x.Nutrient_name == "Sodium, Na").Output_value);

                    // TODO A `NullReferenceException` can be thrown here.
                    double calories = double.Parse(
                        nutrients.FirstOrDefault(x => x.Nutrient_name == "Energy").Output_value);

                    dto.FoodList.Add(entry);
                }

                return(dto);
            }
            catch (ArgumentOutOfRangeException)
            {
                return(StatusCode(404));
            }
        }