Esempio n. 1
0
        public void Handle(CopyToDiaryCommand command)
        {
            var foodlogs = _context.FoodLogs
                           .Include(f => f.Product)
                           .Where(f => f.Date == command.DateToCopy.Date && f.MealId == command.MealId);

            foreach (var foodlog in foodlogs)
            {
                DiaryHelper.CopyFoodlog(_context, foodlog, command.MealId, command.DateToCopyTo);
            }

            _context.SaveChanges();
        }
        public void Handle(AddRecipeToDiaryCommand command)
        {
            var recipe = _context.Recipes
                         .Include(r => r.Ingredients.Select(i => i.Product))
                         .First(r => r.RecipeId == command.RecipeId);

            var servingsRatio = (decimal)command.NumberOfServingsConsumed / recipe.Servings;

            foreach (var ingredient in recipe.Ingredients)
            {
                var amountConsumed = (int)Math.Round(ingredient.Amount * servingsRatio, 0, MidpointRounding.AwayFromZero);
                DiaryHelper.AddProductToDiary(_context, amountConsumed, ingredient.Product, command.MealId, command.ConsumedDate);
            }

            _context.SaveChanges();
        }