public async Task <IActionResult> AddNewRecipe([FromBody] NewRecipeDto recipeToAdd) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } var user = await _userRepo.GetUserByUserId(recipeToAdd.UserId); Recipe recipe = new Recipe() { User = user, UserId = recipeToAdd.UserId, Title = recipeToAdd.Title, Description = recipeToAdd.Description, DateCreated = DateTime.Now, Steps = recipeToAdd.Steps }; var successful = await _recipeRepo.AddRecipe(recipe); if (successful) { return(Ok()); } return(StatusCode(500)); }
public RecipeDto Create(NewRecipeDto recipeDto) { RecipeDto newRecipeDto; using (var context = new FlavorNetworkContext()) { var recipe = new Recipe(); recipe.Name = recipeDto.Name; foreach (var ingredientDto in recipeDto.Ingredients) { var ingredient = new RecipeIngredient(); ingredient.Recipe = recipe; ingredient.Ingredient = context.Ingredients.SingleOrDefault(i => i.Id == ingredientDto); recipe.RecipeIngredients.Add(ingredient); } recipe.Cuisine = context.Cuisines.SingleOrDefault(c => c.Id == recipeDto.Cuisine); context.Recipes.Add(recipe); context.SaveChanges(); newRecipeDto = RecipeAssembler.Map(recipe); } return(newRecipeDto); }
public async Task <bool> Update(Recipe recipe, NewRecipeDto newRecipe) { var findRecipe = await _dataContext.Recipe.FirstOrDefaultAsync(x => x.Id.Equals(recipe.Id)); if (findRecipe == null) { throw new InvalidOperationException($"Item {recipe.Id} was not found"); } findRecipe.Description = newRecipe.Description; findRecipe.Title = newRecipe.Title; findRecipe.Time = newRecipe.Time; findRecipe.Servings = newRecipe.Servings; findRecipe.Image = newRecipe.Image; var products = _dataContext.Product.Where(x => x.FkRecipe.Equals(recipe.Id)).ToList(); foreach (var item in products) { _dataContext.Product.Remove(item); } foreach (var item in newRecipe.Products) { var product = new Product { Name = item.Name, Quantity = item.Quantity, FkRecipe = recipe.Id }; await _dataContext.Product.AddAsync(product); } _dataContext.Update(findRecipe); await _dataContext.SaveChangesAsync(); return(true); }
/// <summary>Posts new recipe to Cookbook.API</summary> public static async Task SendNewRecipeAsync(NewRecipeDto recipe) { var result = await _client.PostAsJsonAsync("api/recipes", recipe); if (!result.IsSuccessStatusCode) { throw new Exception(result.ReasonPhrase); } }
public IActionResult AddRecipe(NewRecipeDto recipe) { if (_repo.AddRecipe(recipe.Title, recipe.Description, recipe.ParentAncestryPath) > 0) { return(new OkResult()); } else { return(new UnprocessableEntityResult()); } }
public async Task <Recipe> Create(NewRecipeDto newItem) { if (newItem == null) { throw new ArgumentNullException(); } var recipe = _mapper.Map <Recipe>(newItem); await _repository.Add(recipe); //var recipeDto = _mapper.Map<RecipeDto>(newItem); return(recipe); }
public void AddRecipe_ReturnsUnprocessableEntityResult_IfAddWasUnsuccessful() { //Arrange var repoMock = new Mock <IRecipesRepository>(); repoMock.Setup(repo => repo.AddRecipe(It.IsAny <string>(), It.IsAny <string>(), It.IsAny <string>())).Returns(0); var controller = new RecipesController(repoMock.Object); var newRecipeDto = new NewRecipeDto(); //Act var result = controller.AddRecipe(newRecipeDto); //Assert Assert.IsType <UnprocessableEntityResult>(result); }
public void AddRecipe_ReturnsOkResult_IfUpdateWasSuccessful() { //Arrange var repoMock = new Mock <IRecipesRepository>(); var newRecipeId = 5; repoMock.Setup(repo => repo.AddRecipe(It.IsAny <string>(), It.IsAny <string>(), It.IsAny <string>())).Returns(newRecipeId); var controller = new RecipesController(repoMock.Object); var newRecipeDto = new NewRecipeDto(); //Act var result = controller.AddRecipe(newRecipeDto); //Assert Assert.IsType <OkResult>(result); }
public async Task Update(int id, NewRecipeDto recipe) { if (recipe == null) { throw new ArgumentNullException(); } var itemToUpdate = await _repository.GetById(id); if (itemToUpdate == null) { throw new InvalidOperationException($"Recipe with {id} id was not found"); } await _repository.Update(itemToUpdate, recipe); }
/// <inheritdoc /> public void CreateNewRecipe(NewRecipeDto newRecipeDto) { var newIngredients = new List <Ingredient>(); foreach (var newIngredientDto in newRecipeDto.Ingredients) { var unit = SimpleCrudHelper.Find <Unit>(newIngredientDto.Unit.UnitId); var article = SimpleCrudHelper.Find <Article>(newIngredientDto.Article.ArticleId); newIngredients.Add(Context.Ingredients.Add(new Ingredient(article, newIngredientDto.Quantity, unit)).Entity); } var newRecipe = new Recipe(newRecipeDto.Name, newRecipeDto.NumberOfDays, newIngredients); Context.Recipes.Add(newRecipe); Context.SaveChanges(); }
public async Task <IActionResult> AddRecipe([FromBody] NewRecipeDto newRecipeDto) { var createRecipe = await _recipeService.Create(newRecipeDto); return(Ok(createRecipe)); }
public async Task <IActionResult> Update(int id, [FromBody] NewRecipeDto newRecipe) { await _recipeService.Update(id, newRecipe); return(NoContent()); }