diff --git a/demo/src/main/java/com/example/demo/repository/RecipeIngredientRepo.java b/demo/src/main/java/com/example/demo/repository/RecipeIngredientRepo.java index e97f96a..db21dd6 100644 --- a/demo/src/main/java/com/example/demo/repository/RecipeIngredientRepo.java +++ b/demo/src/main/java/com/example/demo/repository/RecipeIngredientRepo.java @@ -1,5 +1,7 @@ package com.example.demo.repository; +import java.util.List; + import org.springframework.data.jpa.repository.JpaRepository; import com.example.demo.entity.Recipe; diff --git a/demo/src/main/java/com/example/demo/service/Impl/RecipeServiceImpl.java b/demo/src/main/java/com/example/demo/service/Impl/RecipeServiceImpl.java index 6b922d2..dd549bc 100644 --- a/demo/src/main/java/com/example/demo/service/Impl/RecipeServiceImpl.java +++ b/demo/src/main/java/com/example/demo/service/Impl/RecipeServiceImpl.java @@ -139,14 +139,14 @@ public class RecipeServiceImpl implements RecipeService { @org.springframework.transaction.annotation.Transactional(readOnly = true) public RecipeDto getRecipeById(Integer recipeId) { Recipe recipe = recipeRepo.findById(recipeId) - .orElseThrow(() -> new NotFoundException("Recipe not found")); + .orElseThrow(() -> new NotFoundException("Recipe", "id", recipeId)); // Start with base fields RecipeDto dto = convertToDto(recipe); // Overwrite lists using repo results (no mutation of Recipe.steps / recipeIngredients) dto.setSteps( - stepRepo.findByRecipeId(recipeId).stream() + stepRepo.findById(recipeId).stream() .map(s -> new com.example.demo.dto.StepDto(s.getStepNumber(), s.getInstruction())) .collect(java.util.stream.Collectors.toList()) ); @@ -170,7 +170,7 @@ public class RecipeServiceImpl implements RecipeService { public RecipeDto updateRecipe(RecipeDto recipeDto, Integer id) { Recipe recipe = recipeRepo.findById(id) - .orElseThrow(() -> new NotFoundException("Recipe not found")); + .orElseThrow(() -> new NotFoundException("Recipe", "id", recipeDto)); List ingredientDtos = recipeDto.getIngredients() == null ? Collections.emptyList() : recipeDto.getIngredients(); @@ -205,7 +205,7 @@ public class RecipeServiceImpl implements RecipeService { @Transactional public void deleteRecipe(Integer id) { if (!recipeRepo.existsById(id)) { - throw new NotFoundException("Recipe not found"); + throw new NotFoundException("Recipe not found", null, id); } recipeRepo.deleteById(id); } @@ -215,7 +215,7 @@ public class RecipeServiceImpl implements RecipeService { private User resolveUser(UserDto incoming, User existing) { if (incoming != null && incoming.getId() != null) { return userRepo.findById(incoming.getId()) - .orElseThrow(() -> new NotFoundException("User not found: " + incoming.getId())); + .orElseThrow(() -> new NotFoundException("User not found: " + incoming.getId(), null, existing)); } if (existing != null) return existing; @@ -261,28 +261,5 @@ public class RecipeServiceImpl implements RecipeService { recipe.getRecipeIngredients().add(ri); } } - /* - private Ingredient attachRecipeIngredients(Recipe recipe, List ingredientDtos) { - for (RecipeIngredientDto rid : ingredientDtos) { - if (rid == null) continue; - if (rid.getIngredientName() == null || rid.getIngredientName().isBlank()) continue; - Ingredient ing = ingredientRepo.findByNameIgnoreCase(rid.getIngredientName().trim()) - .orElseGet(() -> { - Ingredient i = new Ingredient(); - i.setName(rid.getIngredientName().trim()); - return ingredientRepo.save(i); - }); - - RecipeIngredient ri = new RecipeIngredient(); - ri.setRecipe(recipe); - ri.setIngredient(ing); - ri.setQuantity(rid.getQuantity() == null ? BigDecimal.ZERO : rid.getQuantity()); - ri.setUnit(rid.getUnit()); - ri.setNotes(rid.getNotes()); - - recipe.getRecipeIngredients().add(ri); - } - return null; - }*/ } \ No newline at end of file