public IActionResult UpdateRecipe(int categoryId, int id,
                                          [FromBody] RecipeForUpdateDto recipe)
        {
            if (recipe == null)
            {
                return(BadRequest());
            }

            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            var category = RecipesDataStore.Current.Categories.FirstOrDefault(c => c.Id == categoryId);

            if (category == null)
            {
                return(NotFound());
            }

            var recipeFromStore = category.Recipes.FirstOrDefault(r =>
                                                                  r.Id == id);

            if (recipeFromStore == null)
            {
                return(NotFound());
            }

            recipeFromStore.Name        = recipe.Name;
            recipeFromStore.Description = recipe.Description;
            recipeFromStore.Ingedients  = recipe.Ingedients;
            recipeFromStore.Preparation = recipe.Preparation;

            return(NoContent());
        }
        public IActionResult UpdateRecipe(int recipeId, RecipeForUpdateDto recipe)
        {
            var recipeFromRepo = _recipeRepository.GetRecipe(recipeId);

            if (recipeFromRepo == null)
            {
                return(NotFound());
            }

            var validationResults = new RecipeForUpdateDtoValidator().Validate(recipe);

            validationResults.AddToModelState(ModelState, null);

            if (!ModelState.IsValid)
            {
                return(BadRequest(new ValidationProblemDetails(ModelState)));
                //return ValidationProblem();
            }

            _mapper.Map(recipe, recipeFromRepo);
            _recipeRepository.UpdateRecipe(recipeFromRepo);

            _recipeRepository.Save();

            return(NoContent());
        }
        public IActionResult PartiallyUpdateRecipe(int categoryId, int id,
                                                   [FromBody] JsonPatchDocument <RecipeForUpdateDto> patchDoc)
        {
            if (patchDoc == null)
            {
                return(BadRequest());
            }

            var category = RecipesDataStore.Current.Categories.FirstOrDefault(c => c.Id == categoryId);

            if (category == null)
            {
                return(NotFound());
            }

            var recipeFromStore = category.Recipes.FirstOrDefault(r => r.Id == id);

            if (recipeFromStore == null)
            {
                return(NotFound());
            }

            var recipe =
                new RecipeForUpdateDto()
            {
                Name        = recipeFromStore.Name,
                Description = recipeFromStore.Description,
                Ingedients  = recipeFromStore.Ingedients,
                Preparation = recipeFromStore.Preparation
            };

            patchDoc.ApplyTo(recipe, ModelState);

            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            TryValidateModel(recipe);

            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            recipeFromStore.Name        = recipe.Name;
            recipeFromStore.Description = recipe.Description;
            recipeFromStore.Ingedients  = recipe.Ingedients;
            recipeFromStore.Preparation = recipe.Preparation;

            return(NoContent());
        }
Exemple #4
0
        public async Task <IActionResult> UpdateRecipe(int id, RecipeForUpdateDto recipeForUpdateDto)
        {
            var recipe = await _repo.GetRecipe(id);

            var updateRecipe = _mapper.Map(recipeForUpdateDto, recipe);

            if (await _repo.SaveAll())
            {
                return(NoContent());
            }

            throw new Exception($"the update fail for recipe {id}");
        }
        public ActionResult UpdateRecipe(int id,
                                         [FromBody] RecipeForUpdateDto recipe,
                                         [FromHeader(Name = ExtendedControllerBase.ACCEPT)] string mediaTypes)
        {
            var splitMediaTypes = mediaTypes.Split(',');

            if (!MediaTypeHeaderValue.TryParseList(splitMediaTypes,
                                                   out IList <MediaTypeHeaderValue> parsedMediaTypes))
            {
                return(BadRequest());
            }

            if (!_homeBrewRepository.WaterProfileExists(recipe.WaterProfileId))
            {
                ModelState.AddModelError(
                    "Description",
                    "The water profile ID for the recipe must exist.");
            }

            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            var includeSteps = true;
            var recipeEntity = _homeBrewRepository.GetRecipe(id, includeSteps);

            if (recipeEntity == null)
            {
                return(NotFound());
            }

            _mapper.Map(recipe, recipeEntity);
            _homeBrewRepository.UpdateRecipe(recipeEntity);
            _homeBrewRepository.Save();

            if (parsedMediaTypes.Any(pmt => pmt.SubTypeWithoutSuffix.EndsWith(this.HATEOAS, StringComparison.InvariantCultureIgnoreCase)))
            {
                var links = CreateLinksForRecipe(recipeEntity.Id, includeSteps);

                var recipeToReturn         = _mapper.Map <RecipeDto>(recipeEntity);
                var linkedResourceToReturn = recipeToReturn.ShapeData(null)
                                             as IDictionary <string, object>;

                linkedResourceToReturn.Add(this.LINKS, links);

                return(Ok(linkedResourceToReturn));
            }

            return(NoContent());
        }
Exemple #6
0
        public IActionResult UpdateRecipe(int recipeId, RecipeForUpdateDto recipe)
        {
            var recipeFromRepo = _recipeRepository.GetRecipe(recipeId);

            if (recipeFromRepo == null)
            {
                return(NotFound());
            }

            _mapper.Map(recipe, recipeFromRepo);
            _recipeRepository.UpdateRecipe(recipeFromRepo);

            _recipeRepository.Save();

            return(NoContent());
        }
Exemple #7
0
        public void FullyUpdateRecipe_InvalidRecipeId_ReturnsNotFound()
        {
            // Arrange
            int invalidRecipeId    = 2;
            var recipeForUpdateDto = new RecipeForUpdateDto();

            recipesServiceMock = new Mock <IRecipesService>();
            recipesServiceMock
            .Setup(m => m.GetRecipe(invalidRecipeId))
            .Returns(null as RecipeDto);
            recipesController = new RecipesController(recipesServiceMock.Object, mapper);

            // Act
            var result = recipesController.FullyUpdateRecipe(invalidRecipeId, recipeForUpdateDto);

            // Assert
            Assert.IsTrue(result.GetType() == typeof(NotFoundResult));
        }
Exemple #8
0
        public void FullyUpdateRecipe_ValidRecipeIdAndValidData_ReturnsNoContent()
        {
            // Arrange
            int validRecipeId      = 1;
            var recipeForUpdateDto = new RecipeForUpdateDto();

            recipesServiceMock = new Mock <IRecipesService>();
            recipesServiceMock
            .Setup(m => m.GetRecipe(validRecipeId))
            .Returns(new RecipeDto());
            recipesController = new RecipesController(recipesServiceMock.Object, mapper);

            // Act
            var result = recipesController.FullyUpdateRecipe(validRecipeId, recipeForUpdateDto);

            // Assert
            Assert.IsTrue(result.GetType() == typeof(NoContentResult));
        }
        [HttpPut("{id}")] // Aktualizacja wartości
        // http://localhost:5000/api/users/{userId}/recipes/{id}
        public async Task <IActionResult> UpdateRecipe(int userId, int id, RecipeForUpdateDto recipeForUpdateDto)
        {
            if (userId != int.Parse(User.FindFirst(ClaimTypes.NameIdentifier).Value))
            {
                return(Unauthorized());
            }
            // tylko zalogowany użytkownik może edytować swój profil

            var recipeFromRepo = await _repository.GetRecipe(id);

            _mapper.Map(recipeForUpdateDto, recipeFromRepo);

            if (await _repository.SaveAll())
            {
                return(NoContent());
            }

            throw new Exception($"Updating recipe {id} failed on save");
        }
Exemple #10
0
        public void FullyUpdateRecipe_ValidRecipeIdAndInvalidData_ReturnsBadRequest()
        {
            // Arrange
            int validRecipeId      = 1;
            var recipeForUpdateDto = new RecipeForUpdateDto();

            recipesServiceMock = new Mock <IRecipesService>();
            recipesServiceMock
            .Setup(m => m.GetRecipe(validRecipeId))
            .Returns(new RecipeDto());
            recipesController = new RecipesController(recipesServiceMock.Object, mapper);
            recipesController.ModelState.AddModelError("Some Key", "Some Error Message");

            // Act
            var result = recipesController.FullyUpdateRecipe(validRecipeId, recipeForUpdateDto);

            // Assert
            Assert.IsTrue(result.GetType() == typeof(BadRequestObjectResult));
        }
Exemple #11
0
        public IActionResult FullyUpdateRecipe(int id, [FromBody] RecipeForUpdateDto recipeForUpdate)
        {
            var recipeToUpdate = recipesService.GetRecipe(id);

            if (recipeToUpdate == null)
            {
                return(NotFound());
            }

            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            var recipeDtoWithNewValues = mapper.Map <RecipeDto>(recipeForUpdate);

            recipesService.FullyUpdateRecipe(recipeToUpdate, recipeDtoWithNewValues);

            return(NoContent());
        }
        public async Task <IActionResult> UpdateRecipe(int recipeId, RecipeForUpdateDto recipeForUpdateDto)
        {
            var userId = int.Parse(User.FindFirst(ClaimTypes.NameIdentifier).Value);

            var recipeFromRepo = await _repo.GetRecipe(recipeId);

            if (userId != recipeFromRepo.UserId)
            {
                return(Unauthorized());
            }

            _mapper.Map(recipeForUpdateDto, recipeFromRepo);

            recipeForUpdateDto.DateAdded = DateTime.Now;

            if (await _repo.SaveAllChanges())
            {
                return(NoContent());
            }

            throw new Exception($"Updating recipe {recipeId} failed on save");
        }