public ActionResult <IngredientDto> AddIngredient(IngredientForCreationDto ingredientForCreation) { var ingredient = _mapper.Map <Ingredient>(ingredientForCreation); _ingredientRepository.AddIngredient(ingredient); _ingredientRepository.Save(); var ingredientDto = _mapper.Map <IngredientDto>(ingredient); return(CreatedAtRoute("GetIngredient", new { ingredientDto.IngredientId }, ingredientDto)); }
public void CreateIngredient_WithNameAndDescription_ReturnsCreatedResult() { // Arrange ingredientsServiceMock = new Mock <IIngredientsService>(); ingredientsServiceMock .Setup(m => m.Insert(It.IsAny <IngredientDto>())) .Returns(1); ingredientsController = new IngredientsController(ingredientsServiceMock.Object, mapper); var ingredientForCreation = new IngredientForCreationDto { Name = "Cooking is fun", Description = "You should cook the meal" }; // Act var result = ingredientsController.CreateIngredient(ingredientForCreation); // Assert Assert.IsTrue(result.GetType() == typeof(CreatedAtRouteResult)); }
public ActionResult <IngredientDto> CreateIngredient(int recipeStepId, [FromBody] IngredientForCreationDto ingredient) { if (!_homeBrewRepository.RecipeStepExists(recipeStepId)) { return(NotFound()); } var finalIngredient = _mapper.Map <Entities.Ingredient>(ingredient); _homeBrewRepository.AddIngredientForRecipeStep(recipeStepId, finalIngredient); _homeBrewRepository.Save(); var createdIngredientToReturn = _mapper.Map <Models.IngredientDto>(finalIngredient); return(CreatedAtRoute( "GetIngredient", new { recipeStepId, id = finalIngredient.Id }, createdIngredientToReturn)); }
public IActionResult CreateIngredient(IngredientForCreationDto ingredientForCreation) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } var ingredientDto = mapper.Map <IngredientDto>(ingredientForCreation); var idOfNewIngredient = ingredientsService.Insert(ingredientDto); if (idOfNewIngredient > 0) { ingredientDto.Id = idOfNewIngredient; return(CreatedAtRoute("GetIngredient", new { Id = idOfNewIngredient }, ingredientDto)); } else { return(StatusCode(StatusCodes.Status500InternalServerError)); } }
public void CreateIngredient_SomethinWentWrongWhileInserting_Returns500InternalServerError() { // Arrange ingredientsServiceMock = new Mock <IIngredientsService>(); ingredientsServiceMock .Setup(m => m.Insert(It.IsAny <IngredientDto>())) .Returns(0); ingredientsController = new IngredientsController(ingredientsServiceMock.Object, mapper); var ingredientForCreation = new IngredientForCreationDto { Name = "Black Pepper", Description = "Black Pepper is spicy and thus healthy" }; // Act var result = ingredientsController.CreateIngredient(ingredientForCreation); // Assert Assert.IsTrue(result.GetType() == typeof(StatusCodeResult)); Assert.IsTrue(((StatusCodeResult)result).StatusCode == StatusCodes.Status500InternalServerError); }