コード例 #1
0
        public void Handle(DeleteFoodLogCommand command)
        {
            var foodLog = _context.FoodLogs.First(log => log.FoodLogId == command.FoodLogId);

            _context.FoodLogs.Remove(foodLog);
            _context.SaveChanges();
        }
        public void Handle(AddProductToDiaryCommand command)
        {
            var product = _context.Products.First(p => p.ProductId == command.ProductId);

            DiaryHelper.AddProductToDiary(_context, command.AmountConsumed, product, command.MealId, command.ConsumedDate);
            _context.SaveChanges();
        }
コード例 #3
0
        public void Handle(AddProducerCommand command)
        {
            var producer = new Producer {
                Name = command.Name
            };

            _context.Producers.Add(producer);
            _context.SaveChanges();
        }
コード例 #4
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();
        }