Facebook
From Emerald Mockingbird, 3 Years ago, written in C#.
Embed
Download Paste or View Raw
Hits: 51
  1.  [HttpPost]
  2.         public async Task<ActionResult> Post(RecipeDto recipeDto)
  3.         {
  4.             var recipe = new Recipe();
  5.             var ingredientRecipes = new List<IngredientRecipe>();
  6.  
  7.             foreach (var ingredient in recipeDto.ListOfIngredients)
  8.             {
  9.                 var ingr = mapper.Map<IngredientRecipe>(ingredient);
  10.  
  11.                 ingr.Ingredient.Id = await context.Ingredients
  12.                     .Where(x => x.Id == ingredient.Ingredient.Id)
  13.                     .Select(x => x.Id)
  14.                     .FirstOrDefaultAsync();
  15.                
  16.                 ingredientRecipes.Add(ingr);
  17.             }
  18.  
  19.             recipe.ListOfIngredients = ingredientRecipes;
  20.             context.Recipes.Add(recipe);
  21.  
  22.             await context.SaveChangesAsync();
  23.            
  24.             return new OkResult();
  25.         }