Ejemplo n.º 1
0
 public NutritionFactsViewModel(NutritionFacts nutFacts)
 {
     if (nutFacts != null)
     {
         Calories = nutFacts.Calories;
         TotalCarbohydrate = nutFacts.TotalCarbohydrate;
         TotalFat = nutFacts.TotalFat;
         SaturatedFat = nutFacts.SaturatedFat;
         Protein = nutFacts.Protein;
         Cholesterol = nutFacts.Cholesterol;
         Sugars = nutFacts.Sugars;
         Sodium = nutFacts.Sodium;
     }
 }
Ejemplo n.º 2
0
 private static NutritionFacts SumIngredientsNutritionFacts(List<Ingredient> ingredients)
 {
     NutritionFacts nutFactsSum = new NutritionFacts();
     foreach (Ingredient ingredient in ingredients)
     {
         nutFactsSum += (ingredient.NutritionFacts * (ingredient.Amount.Weight / 100));
     }
     return nutFactsSum;
 }
Ejemplo n.º 3
0
        public NutritionFacts NutDataListToNutritionFacts(List<NUT_DATA> nutList)
        {
            log.InfoFormat("[NutDataListToNutritionFacts] nutList.Count={0}", nutList.Count);

            NutritionFacts returnValue = new NutritionFacts();
            try
            {
                foreach (var nut in nutList)
                {
                    NutritionData tempNutritionData = NutDataToNutritionData(nut);

                    returnValue.FullNutrisionList.Add(tempNutritionData);
                    if (nut.Nutr_Val.HasValue)
                    {
                        switch (tempNutritionData.Nutr_No)
                        {
                            case 203:
                                returnValue.Protein = nut.Nutr_Val.Value;
                                break;
                            case 204:
                                returnValue.TotalFat = nut.Nutr_Val.Value;
                                break;
                            case 205:
                                returnValue.TotalCarbohydrate = nut.Nutr_Val.Value;
                                break;
                            case 269:
                                returnValue.Sugars = nut.Nutr_Val.Value;
                                break;
                            case 307:
                                returnValue.Sodium = nut.Nutr_Val.Value;
                                break;
                            case 601:
                                returnValue.Cholesterol = nut.Nutr_Val.Value;
                                break;
                            case 606:
                                returnValue.SaturatedFat = nut.Nutr_Val.Value;
                                break;
                            default:
                                break;
                        }//switch
                    }
                }
            }
            catch (Exception e)
            {
                log.ErrorFormat("[NutDataListToNutritionFacts] Exception={0}", e);
            }
            return returnValue;
        }
Ejemplo n.º 4
0
        private static List<Ingredient> ExtractIngredients(HtmlNode ingredientsNode)
        {
            List<Ingredient> ingredients = new List<Ingredient>();

            string[] ingredientLines = ingredientsNode.InnerHtml.Split(new string[] { "<br>" }, StringSplitOptions.RemoveEmptyEntries);

            foreach (string ingredientLine in ingredientLines)
            {
                string ingredientName = ingredientLine;
                Fraction incredientAmountFraction = Fraction.Zero;
                double incredientWeight = 1;
                string[] ingredientParsed = null;

                string measureUnit = GetMeasureUnit(ingredientLine);

                if (measureUnit != null)
                {
                    ingredientParsed = ingredientLine.Split(new string[] { measureUnit }, StringSplitOptions.RemoveEmptyEntries);
                }
                else
                {
                    ingredientParsed = ingredientLine.Split(null as string[], 2, StringSplitOptions.RemoveEmptyEntries);
                }

                try
                {
                    if (ingredientParsed.Length == 1)
                    {
                        incredientAmountFraction = new Fraction(1);
                        ingredientName = ingredientParsed[0].Trim(' ', '-');
                    }
                    else
                    {
                        string incredientAmount = ingredientParsed[0]
                            .Trim(' ', '-')
                            .Replace("חצי", "1/2").Replace("½", "1/2")
                            .Replace("שליש", "1/3")
                            .Replace("רבע", "1/4");
                        incredientAmountFraction = new Fraction(incredientAmount);
                        ingredientName = ingredientParsed[1].Trim(' ', '-');
                        if (measureUnit != null)
                        {
                            incredientWeight = incredientAmountFraction.ToDouble() * IncredientAmount.MEASURE_UNITS[measureUnit];
                        }
                    }
                }
                catch (Exception e) 
                { 
                    log.WarnFormat("Failed to parse ingredient: {0} {1}", ingredientLine, e);
                }

                ProductBasicData product = FindProductByName(ingredientName);
                string productId = null, productName = null;
                NutritionFacts nutFacts = new NutritionFacts();
                if (product != null)
                {
                    productId = product.ProductId;
                    productName = product.ProductName;
                    if (product.ExtendedData != null && product.ExtendedData.NutritionTable != null)
                    {
                        nutFacts = new NutritionFacts(product.ExtendedData.NutritionTable);
                    }
                }
                Ingredient ingredient = new Ingredient()
                {
                    Name = ingredientName,
                    ProductId = productId,
                    ProductName = productName,
                    NutritionFacts = nutFacts,
                    Amount = new IncredientAmount()
                    {
                        MeasureUnit = measureUnit,
                        Amount = incredientAmountFraction,
                        Weight = incredientWeight
                    }
                };
                ingredients.Add(ingredient);
            }

            return ingredients;
        }