public async Task <NotificationResult> CreateShoplistFromRecipesAsync(CreateShoplistFromRecipeIdCommand command) { var ingredientList = new List <InsertShoplistIngredientCommand>(); foreach (var recipeId in command.RecipeId) { ingredientList.AddRange( (await _recipeRepository.GetByIdAsync(recipeId)) .RecipeIngredients.Select(ri => new InsertShoplistIngredientCommand() { Amount = ri.Amount, AmountTypeId = ri.AmountTypeId, IngredientId = ri.IngredientId }).AsEnumerable()); } //soh agrupar e BOA BeginTransaction(); var result = await _handler.CreateShoplistWithIngredients(new InsertShoplistWithIngredientsCommand() { CreationDate = DateTime.Now, ShoplistsIngredients = ingredientList.GroupBy(i => new { i.IngredientId, i.AmountTypeId }, i => i.Amount ).Select(si => new InsertShoplistIngredientCommand() { IngredientId = si.Key.IngredientId, AmountTypeId = si.Key.AmountTypeId, Amount = si.Sum() }).ToList() }); return(Commit(result)); }
public async Task <IActionResult> CreateShoplistFromRecipes(CreateShoplistFromRecipeIdCommand command) { var result = await _service.CreateShoplistFromRecipesAsync(command); return(Ok(result)); }