Exemple #1
0
        public async Task <Guid> AddMealAsync(MealCreationVM mealVM, Guid userId)
        {
            var dbMeal = _mapper.Map <Meal>(mealVM);

            dbMeal.CreatorId = userId;

            if (!await _mealRepository.AddAsync(dbMeal))
            {
                throw new DataAccessException(
                          nameof(_mealRepository.AddAsync) +
                          " failed for argument: " +
                          JsonConvert.SerializeObject(dbMeal)
                          );
            }

            if (!await _mealRepository.AddMealMealIngredientsAsync(GetMealMealIngredients(dbMeal.Id, mealVM.IngredientsIdsWithQuantity)))
            {
                throw new DataAccessException(
                          nameof(_mealRepository.AddMealMealIngredientsAsync) +
                          " failed for argument: " +
                          JsonConvert.SerializeObject(GetMealMealIngredients(dbMeal.Id, mealVM.IngredientsIdsWithQuantity))
                          );
            }

            await _userRepository.IncrementCreatedMealsCountAsync(userId);

            var userTask            = _userRepository.GetUserByIdAsync(userId);
            var logNewMealAddedTask = _activityService.LogNewMealAddedAsync(userId, dbMeal.Id);

            await Task.WhenAll(logNewMealAddedTask, userTask);

            await _achievementService.CheckForNumberOfMealAdditionsByUserAsync(userTask.Result);

            return(dbMeal.Id);
        }