public void ShouldCalculateKcalsForMeal()
 {
     var ingredient1 = ModelCreator.CreateIngredient();
     var ingredient2 = ModelCreator.CreateIngredient();
     ingredient2.Kcal = 200;
     var meal = ModelCreator.CreateMeal();
     _mealRepository.Create(meal);
     var mealIngredients1 = ModelCreator.CreateMealIngredients(meal, ingredient1, ModelCreator.CreateQuantityType(12));
     var mealIngredients2 = ModelCreator.CreateMealIngredients(meal, ingredient2, ModelCreator.CreateQuantityType(12));
     meal.MealIngredients = new List<MealIngredient> { mealIngredients1, mealIngredients2 };
     var mealCalculator = new MealCalculator(meal, _ingredientQuantityRepository);
     mealCalculator.CalculateTotalKcal().ShouldEqual(960);
 }
 public ActionResult Meal(int id)
 {
     var meal = _mealRepository.Get(id);
     var mealViewModel = Mapper.Map<Meal, MealViewModel>(meal);
     var mealCalculator = new MealCalculator(meal, _ingredientQuantityRepository);
     mealViewModel.Kcal = mealCalculator.CalculateTotalKcal();
     mealViewModel.Protein = mealCalculator.CalculateTotalProtein();
     mealViewModel.Fat = mealCalculator.CalculateTotalFat();
     mealViewModel.Carb = mealCalculator.CalculateTotalCarb();
     return View(mealViewModel);
 }
        public DietCalculator(Diet diet, IIngredientQuantityRepository ingredientQuantityRepository, ITracer tracer)
        {
            _diet = diet;
            _ingredientQuantityRepository = ingredientQuantityRepository;
            _tracer = tracer;
            tracer.WriteTrace("Henter ut ingredienser og måltid");
            var ingredients = _diet.DietIngredients.ToList();
            var meals = _diet.DietMeals.ToList();
            foreach (var di in ingredients)
            {
                var quantityConversion = di.Quantity / QuantityConverter.ConvertTo100Grams(di.QuantityTypeId, _ingredientQuantityRepository.GetConvertFactor(di.IngredientId, di.QuantityTypeId)) * di.Day.ToIntArray().Count();
                _totalIngredientCarbsGrams += di.Ingredient.Carb * quantityConversion;
                _totalIngredientFatGrams += di.Ingredient.Fat * quantityConversion;
                _totalIngredientProteinGrams += di.Ingredient.Protein * quantityConversion;
                _totalIngredientKcals += di.Ingredient.Kcal * quantityConversion;
            }

            foreach (var dm in meals)
            {
                var mealDays = dm.Day.ToIntArray().Count();
                var mealCalulator = new MealCalculator(dm.Meal, _ingredientQuantityRepository);
                _totalMealCarbGrams += mealCalulator.CalculateTotalCarb() * mealDays;
                _totalMealFatGrams += mealCalulator.CalculateTotalFat() * mealDays;
                _totalMealProteinGrams += mealCalulator.CalculateTotalProtein() * mealDays;
                _totalMealKcals += mealCalulator.CalculateTotalKcal() * mealDays;
            }

            _totalGrams = _totalIngredientCarbsGrams + _totalIngredientFatGrams + _totalIngredientProteinGrams +
                          _totalMealCarbGrams + _totalMealFatGrams + _totalMealProteinGrams;
            _tracer.WriteTrace("Ferdig med constructor");
        }