public void IngredientToGeneralDishTest_ShouldCreatNewGeneralDishFromIngredientPropertiesAndAddIngredientToThisDishIngrList() { //arrange Ingredient ingr = new Ingredient() { Id = ObjectId.GenerateNewId(), USDA_NDB_No = 11111, FoodGrp = FoodGroupTypeEnum.BabyFoods, LongDesc = new Localized("Test Long Description"), NutritionFacts = new NutritionFacts() { TotalCarbohydrate = 25, Calories = 100, TotalFat = 10 } }; Ingredient ingr2 = new Ingredient() { Id = ObjectId.GenerateNewId(), USDA_NDB_No = 11111, FoodGrp = FoodGroupTypeEnum.BabyFoods, LongDesc = new Localized("he", "שם לנסיון"), NutritionFacts = new NutritionFacts() { TotalCarbohydrate = 25, Calories = 100, TotalFat = 10 } }; ingr2.LongDesc.AddDescription("en-US", "Test description in english"); //act GeneralDish genDish = ingr.IngredientToGeneralDish(); GeneralDish genDish2 = ingr2.IngredientToGeneralDish("he"); //assert Assert.IsNotNull(genDish); Assert.IsNotNull(genDish.Dish.OverrideIngredients); Assert.AreEqual(genDish.Dish.OverrideIngredients[0], ingr); Assert.IsNotNull(genDish.Dish.NutritionFacts); Assert.IsTrue(genDish.Dish.CreatedAt < DateTime.UtcNow); Assert.IsTrue(genDish.Dish.UpdatedAt < DateTime.UtcNow); Assert.AreEqual(genDish.Dish.Name, ingr.LongDesc.GetDescription()); Assert.IsNotNull(genDish2); Assert.IsNotNull(genDish2.Dish.OverrideIngredients); Assert.AreEqual(genDish2.Dish.OverrideIngredients[0], ingr2); Assert.IsNotNull(genDish2.Dish.NutritionFacts); Assert.IsTrue(genDish2.Dish.CreatedAt < DateTime.UtcNow); Assert.IsTrue(genDish2.Dish.UpdatedAt < DateTime.UtcNow); Assert.AreEqual(genDish2.Dish.Name, ingr2.LongDesc.GetDescription("he")); Assert.AreNotEqual(genDish2.Dish.Name, ingr2.LongDesc.GetDescription()); Assert.AreNotEqual(genDish2.Dish.Name, ingr2.LongDesc.GetDescription("en-US")); }
public void AddDishToDB_ShouldSaveGeneralDishInDBAndReturnStringedID() { //arrange DateTime time = DateTime.UtcNow; Ingredient ingr1 = new Ingredient() { Id = ObjectId.GenerateNewId(), USDA_NDB_No = 11111, FoodGrp = FoodGroupTypeEnum.BabyFoods, LongDesc = new Localized("Test Long Description"), NutritionFacts = new NutritionFacts() { TotalCarbohydrate = 25, Calories = 100, TotalFat = 10 } }; Ingredient ingr2 = new Ingredient() { Id = ObjectId.GenerateNewId(), USDA_NDB_No = 11111, FoodGrp = FoodGroupTypeEnum.BabyFoods, LongDesc = new Localized("he", "שם לנסיון"), NutritionFacts = new NutritionFacts() { TotalCarbohydrate = 25, Calories = 100, TotalFat = 10 } }; GeneralDish genDish1 = ingr1.IngredientToGeneralDish(); GeneralDish genDish2 = ingr2.IngredientToGeneralDish("he"); //act string id1 = serviceLayer.AddGeneralDishToDB(genDish1); string id2 = serviceLayer.AddGeneralDishToDB(genDish2); GeneralDish storedDish1 = serviceLayer.GetGeneralDishById(id1); GeneralDish storedDish2 = serviceLayer.GetGeneralDishById(id2); //clear DB from inserted dishes serviceLayer.DeleteGeneralDish(id1); serviceLayer.DeleteGeneralDish(id2); GeneralDish deletedDish1 = serviceLayer.GetGeneralDishById(id1); GeneralDish deletedDish2 = serviceLayer.GetGeneralDishById(id2); //assert Assert.IsNotNull(storedDish1); Assert.IsNotNull(storedDish2); Assert.IsNull(deletedDish1); Assert.IsNull(deletedDish2); Assert.AreEqual(genDish1.Dish.Name, storedDish1.Dish.Name); Assert.AreEqual(genDish1.Dish.NutritionFacts.TotalCarbohydrate, storedDish1.Dish.NutritionFacts.TotalCarbohydrate); Assert.AreEqual(genDish1.Dish.NutritionFacts.Calories, storedDish1.Dish.NutritionFacts.Calories); Assert.AreEqual(genDish1.Dish.NutritionFacts.TotalFat, storedDish1.Dish.NutritionFacts.TotalFat); Assert.AreEqual(genDish2.Dish.Name, storedDish2.Dish.Name); Assert.AreEqual(genDish2.Dish.NutritionFacts.TotalCarbohydrate, storedDish2.Dish.NutritionFacts.TotalCarbohydrate); Assert.AreEqual(genDish2.Dish.NutritionFacts.Calories, storedDish2.Dish.NutritionFacts.Calories); Assert.AreEqual(genDish2.Dish.NutritionFacts.TotalFat, storedDish2.Dish.NutritionFacts.TotalFat); Assert.AreEqual(genDish2.Dish.Name, storedDish2.Dish.Name); }
public void UpdateIngredient(Ingredient ingredient) { log.DebugFormat("[UpdateIngredient] Ingredient={0}.", ingredient.LongDesc.GetDescription()); //ingredient.UpdatedAt = DateTime.UtcNow; using (Restaurants restaurantsDb = new Restaurants()) { MongoEntityRepositoryBase<Ingredient> basicData = new MongoEntityRepositoryBase<Ingredient>(restaurantsDb.DB); basicData.Update(ingredient); } }
public Dish IngredientToDish(Ingredient ingredient) { log.InfoFormat("[IngredientToDish] ingredient.Id={0}, ingredient.Name={1}.", ingredient.Id, ingredient.LongDesc.GetDescription() ); Dish dish = new Dish() { OverrideIngredients = new List<Ingredient>() { ingredient }, CreatedAt = DateTime.UtcNow, DishState = DishStateEnum.Active, IsItPublic = true, State = new SuspiciousState() { Index = 100 } }; if (ingredient.LongDesc != null) dish.Name = ingredient.LongDesc.GetDescription(new CultureInfo("en-US")); if (ingredient.NutritionFacts != null) dish.NutritionFacts = ingredient.NutritionFacts; return dish; }
public Ingredient FoodDesToIngredient(USDADataImport.FOOD_DES foodDes, List<USDADataImport.FD_GROUP> food_groups) { log.InfoFormat("[FoodDesToIngredient] foodDes.NDB_No={0}, food_groups.count={1}.", foodDes.NDB_No, food_groups.Count); try { Spontaneous.DataModel.Ingredient tempIngredient = new Spontaneous.DataModel.Ingredient() { Amount = new IncredientAmount() { Weight = 100 }, USDA_NDB_No = int.Parse(foodDes.NDB_No), NutritionFacts = NutDataListToNutritionFacts(foodDes.NUT_DATA.ToList()), Weigts = new List<Spontaneous.DataModel.Foods.Weight>(), Langual = new List<string>() }; if (foodDes.Long_Desc != null) tempIngredient.LongDesc = new Localized(foodDes.Long_Desc); if (foodDes.Shrt_Desc != null) tempIngredient.ShrtDesc = new Localized(foodDes.Shrt_Desc); if (foodDes.ComName != null) tempIngredient.ComName = new Localized(foodDes.ComName); if (foodDes.ManufacName != null) tempIngredient.ManufacName = new Localized(foodDes.ManufacName); if (foodDes.Ref_Desc != null) tempIngredient.RefDesc = new Localized(foodDes.Ref_Desc); if (foodDes.SciName != null) tempIngredient.SciName = new Localized(foodDes.SciName); if (foodDes.Refuse != null && foodDes.Refuse.HasValue) tempIngredient.Refuse = foodDes.Refuse.Value; if (foodDes.N_Factor != null && foodDes.N_Factor.HasValue) tempIngredient.NFactor = foodDes.N_Factor.Value; if (foodDes.Pro_Factor != null && foodDes.Pro_Factor.HasValue) tempIngredient.ProFactor = foodDes.Pro_Factor.Value; if (foodDes.Fat_Factor != null && foodDes.Fat_Factor.HasValue) tempIngredient.FatFactor = foodDes.Fat_Factor.Value; if (foodDes.CHO_Factor != null && foodDes.CHO_Factor.HasValue) tempIngredient.CHOFactor = foodDes.CHO_Factor.Value; tempIngredient.FoodGrp = (FoodGroupTypeEnum)int.Parse(food_groups.FirstOrDefault(c => c.FdGrp_CD == foodDes.FdGrp_Cd).FdGrp_CD); //Add Weigts foreach (var weight in foodDes.WEIGHT) { Spontaneous.DataModel.Foods.Weight tempWeight = new Spontaneous.DataModel.Foods.Weight() { NDB_No = tempIngredient.USDA_NDB_No, Seq = int.Parse(weight.Seq), Amount = weight.Amount.Value, Msre_Desc = weight.Msre_Desc, Gm_Wgt = weight.Gm_Wgt.Value }; if (weight.Num_Data_Pts != null && weight.Num_Data_Pts.HasValue) tempWeight.Num_Data_Pts = weight.Num_Data_Pts.Value; if (weight.Std_Dev != null && weight.Std_Dev.HasValue) tempWeight.Std_Dev = weight.Std_Dev.Value; tempIngredient.Weigts.Add(tempWeight); } //Add Langual foreach (var tempLangDesc in foodDes.LANGDESC) { string temp_lang = tempLangDesc != null ? tempLangDesc.ToString() : ""; tempIngredient.Langual.Add(temp_lang); } return tempIngredient; } catch (Exception e) { log.ErrorFormat("[FoodDesToIngredient] Exception={0}", e); return null; } }
public void AddTranslationForIngredient(Ingredient ingredient) { log.InfoFormat("[AddTranslationForIngredient] ingredient.USDA_NDB_No={0}, ingredient.LongDesc={1}.", ingredient.USDA_NDB_No, ingredient.LongDesc.GetDescription()); string text = ""; string translation = ""; if (ingredient.LongDesc != null && ingredient.LongDesc.GetDescription() != null && ingredient.LongDesc.GetDescription() != "") { text = ingredient.LongDesc.GetDescription(); charsCount += text.Length; translation = GoogleTranslate(text, "iw"); if (translation != null && translation != "" && translation != text) { ingredient.LongDesc.AddDescription("he", translation); log.InfoFormat("[AddTranslationForIngredient] Ingredient.LongDescription={0}, translation={1}", text, translation); LongDescCount++; } if (translation == text) log.WarnFormat("[AddTranslationForIngredient] Translation same to input text. translation=input={0}. Translation not added.", translation); } if (ingredient.ComName != null && ingredient.ComName.GetDescription() != null && ingredient.ComName.GetDescription() != "") { text = ingredient.ComName.GetDescription(); charsCount += text.Length; translation = GoogleTranslate(text, "iw"); if (translation != null && translation != "" && translation != text ) { ingredient.ComName.AddDescription("he", translation); log.InfoFormat("[AddTranslationForIngredient] Ingredient.ComName={0}, translation={1}", text, translation); ComNameCount++; } if (translation == text) log.WarnFormat("[AddTranslationForIngredient] Translation same to input text. translation=input={0}. Translation not added.", translation); } IngredientCount++; serviceLayer.UpdateIngredient(ingredient); //log.InfoFormat("[AddTranslationForIngredient] Ingredient.LongDescription={0}, translation={1}", text, translation); Console.Write("[AddTranslationForIngredient] Ingredient.LongDescription={0}, translation={1}, Ingredient.ComName={0}.\n", ingredient.LongDesc.GetDescription(), translation); }
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; }