Ejemplo n.º 1
0
        public async Task <CommandResult> Handle(AddMealFoodCommand request, CancellationToken cancellationToken)
        {
            Meal meal = await _mealRepository.GetByIdAsync(request.MealId);

            if (!FoundValidMeal(meal))
            {
                return(FailureDueToMealNotFound());
            }

            Food food = await _foodRepository.GetByIdAndProfileIdAsync(request.FoodId, _currentProfileId);

            if (food == null)
            {
                return(FailureDueToCustomFoodNotFound());
            }

            MealFood mealFood = new MealFood(
                food,
                request.Quantity
                );

            meal.AddMealFood(mealFood);
            if (!meal.IsValid)
            {
                return(FailureDueToEntityStateInconsistency(meal));
            }

            await _mealRepository.UpdateAsync(meal);

            return(await CommitAndPublishAsync(new MealFoodAddedDomainEvent(
                                                   meal.Id,
                                                   meal.DietId,
                                                   mealFood.FoodId
                                                   )));
        }
Ejemplo n.º 2
0
        public async Task <IActionResult> AddMealFoodAsync(Guid id, [FromBody] AddMealFoodCommand command)
        {
            command.MealId = id;

            return(await CreateCommandResponse(command));
        }