public async Task <GetRecipeDto> AddRecipe(AddRecipeDto newRecipe) { Recipe recipe = _mapper.Map <Recipe>(newRecipe); recipe.Instructions = newRecipe.Instructions.Select(i => new InstructionStep() { Recipe = recipe, Text = i.Text } ).ToList(); recipe.Ingredients = newRecipe.Ingredients.Select(i => new RecipeGroceryItem() { Amount = i.Amount, Measurementunit = i.Measurementunit, GroceryItemId = i.GroceryItemId, Recipe = recipe } ).ToList(); recipe.RecipeCategory = await _context.RecipeCategories.FirstAsync(c => c.Id == newRecipe.RecipeCategoryId); await _context.Recipes.AddAsync(recipe); await _context.SaveChangesAsync(); return(_mapper.Map <GetRecipeDto>(recipe)); }
public int AddRecipe(AddRecipeDto item) { Recipe recipe = _mapper.Map <Recipe>(item); MockedData.recipes.Add(recipe); return(MockedData.recipes.Find(r => r == recipe).id); }
public int AddRecipe(AddRecipeDto item) { int newRecipeId = _recipeRepository.AddRecipe(item); if (item.fatherRecipeId != 0) { _recipeRepository.AddChildrenRecipe(newRecipeId); } return(newRecipeId); }
public void AddRecipe_MinimalRecipe_NoErrors() { AddRecipeDto addRecipeDto = new AddRecipeDto() { Title = "recipe title", RecipeCategoryId = RecipeCategoryId, }; Assert.DoesNotThrowAsync(async() => { await _recipeRepository.AddRecipe(addRecipeDto); }); }
public void AddRecipe_MaximalRecipe_NoErrors() { List <AddRecipeGroceryItemDto> groceryItems = new List <AddRecipeGroceryItemDto>() { new AddRecipeGroceryItemDto() { GroceryItemId = GroceryItemId, Amount = 1.0, Measurementunit = MeasurementUnit.Kg } }; List <AddInstructionStepDto> instructionSteps = new List <AddInstructionStepDto>() { new AddInstructionStepDto() { Text = "step 1" }, new AddInstructionStepDto() { Text = "step 2" } }; AddRecipeDto addRecipeDto = new AddRecipeDto() { Title = "recipe title", RecipeCategoryId = RecipeCategoryId, Amount = 1.0, PortionUnit = PortionUnit.Bun, Time = 1, Vegetarian = true, Ingredients = groceryItems, Instructions = instructionSteps, Source = "source link", Comment = "comment" }; Assert.DoesNotThrowAsync(async() => { await _recipeRepository.AddRecipe(addRecipeDto); }); }
public int AddRecipe([FromBody] AddRecipeDto recipe) { return(_recipeBl.AddRecipe(recipe)); }
public IHttpActionResult AddRecipe([FromBody] AddRecipeDto recipe) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } try { using (var db = new ApplicationDbContext()) { List <Ingredient> mustHaveIngredients; if (recipe.MustHaveIngredients == null) { mustHaveIngredients = null; } else { mustHaveIngredients = db.Ingredients.Where(x => recipe.MustHaveIngredients.Contains(x.Id)).ToList(); } List <Ingredient> optionalIngredients; if (recipe.OptionalIngredients == null) { optionalIngredients = null; } else { optionalIngredients = db.Ingredients.Where(x => recipe.OptionalIngredients.Contains(x.Id)).ToList(); } List <Category> categories; if (recipe.Categories == null) { categories = null; } else { categories = db.Categories.Where(x => recipe.Categories.Contains(x.Id)).ToList(); } var rcp = new Recipe() { Name = recipe.Name, Description = recipe.Description, ImageUrl = recipe.ImageUrl, AddedBy = User.Identity.Name }; rcp.TimeToMake = recipe.TimeToMake; if (categories != null) { foreach (var cat in categories) { rcp.Categories.Add(cat); } } if (mustHaveIngredients != null) { foreach (var ing in mustHaveIngredients) { rcp.Ingredients.Add(new IngredientRecipe() { Ingredient = ing, Recipe = rcp, Optional = false }); } } if (optionalIngredients != null) { foreach (var ing in optionalIngredients) { rcp.Ingredients.Add(new IngredientRecipe() { Ingredient = ing, Recipe = rcp, Optional = true, }); } } db.Recipes.Add(rcp); db.SaveChanges(); recipe.Id = rcp.Id; // recipe.DateAdded = rcp.DateAdded; return(Ok(recipe)); } } catch (Exception e) { return(InternalServerError(e)); } }
public async Task <IActionResult> AddRecipe(AddRecipeDto newRecipe) { return(Ok(await _recipeRepository.AddRecipe(newRecipe))); }